Skip to content

Commit dc81690

Browse files
committed
Update word2md tool to support hyperlinks
1 parent c079ee8 commit dc81690

2 files changed

Lines changed: 53 additions & 6 deletions

File tree

scripts/word2md.js

Lines changed: 29 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/word2md.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,19 @@ module Word {
100100
toggleShowCodes(): void;
101101
}
102102

103+
export interface Hyperlink {
104+
address: string;
105+
textToDisplay: string;
106+
range: Range;
107+
}
108+
109+
export interface Hyperlinks extends Collection<Hyperlink> {
110+
}
111+
103112
export interface Document {
104113
fields: Fields;
105114
paragraphs: Paragraphs;
115+
hyperlinks: Hyperlinks;
106116
builtInDocumentProperties: Collection<any>;
107117
close(saveChanges: boolean): void;
108118
range(): Range;
@@ -195,6 +205,18 @@ function convertDocumentToMarkdown(doc: Word.Document): string {
195205
find.execute(findText, false, false, false, false, false, true, 0, true, replaceText, 2);
196206
}
197207

208+
function fixHyperlinks() {
209+
var count = doc.hyperlinks.count;
210+
for (var i = 0; i < count; i++) {
211+
var hyperlink = doc.hyperlinks.item(i + 1);
212+
var address = hyperlink.address;
213+
if (address && address.length > 0) {
214+
var textToDisplay = hyperlink.textToDisplay;
215+
hyperlink.textToDisplay = "[" + textToDisplay + "](" + address + ")";
216+
}
217+
}
218+
}
219+
198220
function write(s: string) {
199221
result += s;
200222
}
@@ -347,6 +369,8 @@ function convertDocumentToMarkdown(doc: Word.Document): string {
347369
findReplace("^19 REF", {}, "[^&](#^&)", {});
348370
doc.fields.toggleShowCodes();
349371

372+
fixHyperlinks();
373+
350374
writeDocument();
351375

352376
result = result.replace(/\x85/g, "\u2026");

0 commit comments

Comments
 (0)