@@ -7,7 +7,7 @@ import { RendererEvent, MarkdownEvent, PageEvent } from "../events";
77import { BindOption , readFile , copySync , isFile } from "../../utils" ;
88import { highlight , isSupportedLanguage } from "../../utils/highlighter" ;
99import type { Theme } from "shiki" ;
10- import { getTextContent } from "../../utils/html" ;
10+ import { escapeHtml , getTextContent } from "../../utils/html" ;
1111
1212/**
1313 * Implements markdown and relativeURL helpers for templates.
@@ -190,14 +190,15 @@ output file :
190190
191191 markedOptions . renderer . heading = ( text , level , _ , slugger ) => {
192192 const slug = slugger . slug ( text ) ;
193- // Prefix the slug with an extra `$ ` to prevent conflicts with TypeDoc's anchors.
193+ // Prefix the slug with an extra `md: ` to prevent conflicts with TypeDoc's anchors.
194194 this . page ! . pageHeadings . push ( {
195195 link : `#md:${ slug } ` ,
196196 text : getTextContent ( text ) ,
197197 level,
198198 } ) ;
199- return `<a id="md:${ slug } " class="tsd-anchor"></a><h${ level } ><a href="#$ $ {slug } " style="color:inherit;text-decoration:none ">${ text } </a></h${ level } >` ;
199+ return `<a id="md:${ slug } " class="tsd-anchor"></a><h${ level } ><a href="#md: $ {slug } ">${ text } </a></h${ level } >` ;
200200 } ;
201+ markedOptions . renderer . code = renderCode ;
201202 }
202203
203204 markedOptions . mangle ??= false ; // See https://github.com/TypeStrong/typedoc/issues/1395
@@ -214,3 +215,33 @@ output file :
214215 event . parsedText = Marked . marked ( event . parsedText ) ;
215216 }
216217}
218+
219+ // Basically a copy/paste of Marked's code, with the addition of the button
220+ // https://github.com/markedjs/marked/blob/v4.3.0/src/Renderer.js#L15-L39
221+ function renderCode (
222+ this : Marked . marked . Renderer ,
223+ code : string ,
224+ infostring : string | undefined ,
225+ escaped : boolean
226+ ) {
227+ const lang = ( infostring || "" ) . match ( / \S * / ) ! [ 0 ] ;
228+ if ( this . options . highlight ) {
229+ const out = this . options . highlight ( code , lang ) ;
230+ if ( out != null && out !== code ) {
231+ escaped = true ;
232+ code = out ;
233+ }
234+ }
235+
236+ code = code . replace ( / \n $ / , "" ) + "\n" ;
237+
238+ if ( ! lang ) {
239+ return `<pre><code>${
240+ escaped ? code : escapeHtml ( code )
241+ } </code><button>Copy</button></pre>\n`;
242+ }
243+
244+ return `<pre><code class="${ this . options . langPrefix + escapeHtml ( lang ) } ">${
245+ escaped ? code : escapeHtml ( code )
246+ } </code><button>Copy</button></pre>\n`;
247+ }
0 commit comments