@@ -19,6 +19,26 @@ marked.Lexer.prototype.lex = function lex(src) {
1919} ;
2020
2121const renderer = new marked . Renderer ( ) ;
22+ renderer . heading = ( text , level ) => {
23+ // Small title. No need for an anchor.
24+ // It's reducing the risk of duplicated id and it's fewer elements in the DOM.
25+ if ( level >= 4 ) {
26+ return `<h${ level } >${ text } </h${ level } >` ;
27+ }
28+
29+ // eslint-disable-next-line no-underscore-dangle
30+ const hash = textToHash ( text , global . __MARKED_UNIQUE__ ) ;
31+
32+ return [
33+ `<h${ level } >` ,
34+ `<a class="anchor-link" id="${ hash } "></a>` ,
35+ text ,
36+ `<a class="anchor-link-style" aria-hidden="true" aria-label="anchor" href="#${ hash } ">` ,
37+ '<svg><use xlink:href="#anchor-link-icon" /></svg>' ,
38+ '</a>' ,
39+ `</h${ level } >` ,
40+ ] . join ( '' ) ;
41+ } ;
2242
2343const externs = [
2444 'https://material.io/' ,
@@ -284,28 +304,6 @@ function MarkdownElement(props) {
284304 // eslint-disable-next-line no-underscore-dangle
285305 global . __MARKED_USER_LANGUAGE__ = userLanguage ;
286306
287- // need to reset on every render to make textToHash concurrent-safe
288- const headingIdCache = { } ;
289- renderer . heading = ( headingText , level ) => {
290- // Small title. No need for an anchor.
291- // It's reducing the risk of duplicated id and it's fewer elements in the DOM.
292- if ( level >= 4 ) {
293- return `<h${ level } >${ headingText } </h${ level } >` ;
294- }
295-
296- const hash = textToHash ( headingText , headingIdCache ) ;
297-
298- return [
299- `<h${ level } >` ,
300- `<a class="anchor-link" id="${ hash } "></a>` ,
301- headingText ,
302- `<a class="anchor-link-style" aria-hidden="true" aria-label="anchor" href="#${ hash } ">` ,
303- '<svg><use xlink:href="#anchor-link-icon" /></svg>' ,
304- '</a>' ,
305- `</h${ level } >` ,
306- ] . join ( '' ) ;
307- } ;
308-
309307 /* eslint-disable react/no-danger */
310308 return (
311309 < div
0 commit comments