Skip to content

Commit 82f323b

Browse files
committed
Revert "[docs] Fix heading fragment id mismatch"
This reverts commit 0762f98.
1 parent 55afd82 commit 82f323b

File tree

3 files changed

+23
-28
lines changed

3 files changed

+23
-28
lines changed

docs/src/modules/components/MarkdownElement.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@ marked.Lexer.prototype.lex = function lex(src) {
1919
};
2020

2121
const 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

2343
const 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

docs/src/modules/components/useMarkdownDocs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ ${headers.components
8686
`);
8787
}
8888

89+
// eslint-disable-next-line no-underscore-dangle
90+
global.__MARKED_UNIQUE__ = {};
91+
8992
const element = (
9093
<React.Fragment>
9194
<svg style={{ display: 'none' }} xmlns="http://www.w3.org/2000/svg">

docs/src/modules/utils/textToHash.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ function makeUnique(hash, unique, i = 1) {
99
return makeUnique(hash, unique, i + 1);
1010
}
1111

12-
/**
13-
* @param {string} text
14-
* @param {object} unique - cache object, if provided textToHash has side-effects.
15-
* If you use it when rendering a react component be sure
16-
* to always pass a new cache object.
17-
*/
1812
export default function textToHash(text, unique = {}) {
1913
return makeUnique(
2014
encodeURI(

0 commit comments

Comments
 (0)