@@ -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+
95103interface 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 */
110120export 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 : {
0 commit comments