Skip to content

Commit 0e3315b

Browse files
committed
Generalize MarkdownRenderer to support link resolution
1 parent 1a008ce commit 0e3315b

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

apps/api-documenter/src/markdown/MarkdownDocumenter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,11 @@ export class MarkdownDocumenter {
164164
const filename: string = path.join(this._outputFolder, this._getFilenameForApiItem(apiItem));
165165
const stringBuilder: StringBuilder = new StringBuilder();
166166

167-
MarkdownRenderer.renderNode(stringBuilder, output);
167+
MarkdownRenderer.renderNode(stringBuilder, output, {
168+
onResolveTargetForCodeDestination: (docLinkTag: DocLinkTag) => {
169+
return '#';
170+
}
171+
});
168172

169173
FileSystem.writeFile(filename, stringBuilder.toString(), {
170174
convertLineEndings: NewlineKind.CrLf
@@ -417,10 +421,6 @@ export class MarkdownDocumenter {
417421
private _writeFunctionLikeTables(output: DocSection, apiFunctionLike: ApiFunctionLikeMixin): void {
418422
const configuration: TSDocConfiguration = this._tsdocConfiguration;
419423

420-
if (apiFunctionLike.name === 'example') {
421-
debugger;
422-
}
423-
424424
const parametersTable: DocTable = new DocTable({ configuration,
425425
headerTitles: [ 'Parameter', 'Type', 'Description' ]
426426
});

apps/api-documenter/src/utils/MarkdownRenderer.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ class SimpleWriter {
9292
}
9393
}
9494

95+
export interface IMarkdownRendererOptions {
96+
/**
97+
* Given a DocLinkTag with a codeDestination property, determine the target link that should be emitted
98+
* in the "[link text](target URL)" Markdown notation. If the link cannot be resolved, undefined is returned.
99+
*/
100+
onResolveTargetForCodeDestination: (docLinkTag: DocLinkTag) => string | undefined;
101+
}
102+
95103
interface IRenderContext {
96104
writer: SimpleWriter;
97105
insideTable: boolean;
@@ -101,6 +109,8 @@ interface IRenderContext {
101109

102110
writingBold: boolean;
103111
writingItalic: boolean;
112+
113+
options: IMarkdownRendererOptions;
104114
}
105115

106116
/**
@@ -109,7 +119,7 @@ interface IRenderContext {
109119
*/
110120
export class MarkdownRenderer {
111121

112-
public static renderNode(stringBuilder: StringBuilder, docNode: DocNode): string {
122+
public static renderNode(stringBuilder: StringBuilder, docNode: DocNode, options: IMarkdownRendererOptions): string {
113123
const writer: SimpleWriter = new SimpleWriter(stringBuilder);
114124

115125
const context: IRenderContext = {
@@ -120,7 +130,9 @@ export class MarkdownRenderer {
120130
italicRequested: false,
121131

122132
writingBold: false,
123-
writingItalic: false
133+
writingItalic: false,
134+
135+
options
124136
};
125137

126138
MarkdownRenderer._writeNode(docNode, context);
@@ -171,11 +183,24 @@ export class MarkdownRenderer {
171183
}
172184
case DocNodeKind.LinkTag: {
173185
const docLinkTag: DocLinkTag = docNode as DocLinkTag;
174-
writer.write('[');
175-
if (docLinkTag.linkText !== undefined) {
176-
writer.write(docLinkTag.linkText.replace(/\s+/g, ' '));
186+
if (docLinkTag.linkText !== undefined && docLinkTag.linkText.length > 0) {
187+
const encodedLinkText: string = MarkdownRenderer._getEscapedText(docLinkTag.linkText.replace(/\s+/g, ' '));
188+
let destination: string | undefined = undefined;
189+
if (docLinkTag.codeDestination) {
190+
destination = context.options.onResolveTargetForCodeDestination(docLinkTag);
191+
} else if (docLinkTag.urlDestination) {
192+
destination = docLinkTag.urlDestination;
193+
}
194+
195+
if (destination !== undefined) {
196+
writer.write('[');
197+
writer.write(encodedLinkText);
198+
writer.write(`](${destination})`);
199+
} else {
200+
writer.write(encodedLinkText);
201+
}
177202
}
178-
writer.write(`](${docLinkTag.urlDestination || ''})`);
203+
179204
break;
180205
}
181206
case DocNodeKind.Paragraph: {

apps/api-documenter/src/utils/test/MarkdownRenderer.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ test('render Markdown from TSDoc', done => {
197197

198198
const outputFilename: string = path.join(outputFolder, 'ActualOutput.md');
199199
const stringBuilder: StringBuilder = new StringBuilder();
200-
MarkdownRenderer.renderNode(stringBuilder, output);
200+
MarkdownRenderer.renderNode(stringBuilder, output, {
201+
onResolveTargetForCodeDestination: (docLinkTag: DocLinkTag) => '#'
202+
});
201203
FileSystem.writeFile(outputFilename, stringBuilder.toString());
202204

203205
FileDiffTest.assertEqual(outputFilename, path.join(__dirname, 'ExpectedOutput.md'));

0 commit comments

Comments
 (0)