Skip to content

Commit

Permalink
[docs] Fix heading fragment id mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Apr 12, 2020
1 parent 7c610f0 commit 0762f98
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
42 changes: 22 additions & 20 deletions docs/src/modules/components/MarkdownElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,6 @@ marked.Lexer.prototype.lex = function lex(src) {
};

const renderer = new marked.Renderer();
renderer.heading = (text, level) => {
// Small title. No need for an anchor.
// It's reducing the risk of duplicated id and it's fewer elements in the DOM.
if (level >= 4) {
return `<h${level}>${text}</h${level}>`;
}

// eslint-disable-next-line no-underscore-dangle
const hash = textToHash(text, global.__MARKED_UNIQUE__);

return [
`<h${level}>`,
`<a class="anchor-link" id="${hash}"></a>`,
text,
`<a class="anchor-link-style" aria-hidden="true" aria-label="anchor" href="#${hash}">`,
'<svg><use xlink:href="#anchor-link-icon" /></svg>',
'</a>',
`</h${level}>`,
].join('');
};

const externs = [
'https://material.io/',
Expand Down Expand Up @@ -304,6 +284,28 @@ function MarkdownElement(props) {
// eslint-disable-next-line no-underscore-dangle
global.__MARKED_USER_LANGUAGE__ = userLanguage;

// need to reset on every render to make textToHash concurrent-safe
const headingIdCache = {};
renderer.heading = (headingText, level) => {
// Small title. No need for an anchor.
// It's reducing the risk of duplicated id and it's fewer elements in the DOM.
if (level >= 4) {
return `<h${level}>${headingText}</h${level}>`;
}

const hash = textToHash(headingText, headingIdCache);

return [
`<h${level}>`,
`<a class="anchor-link" id="${hash}"></a>`,
headingText,
`<a class="anchor-link-style" aria-hidden="true" aria-label="anchor" href="#${hash}">`,
'<svg><use xlink:href="#anchor-link-icon" /></svg>',
'</a>',
`</h${level}>`,
].join('');
};

/* eslint-disable react/no-danger */
return (
<div
Expand Down
3 changes: 0 additions & 3 deletions docs/src/modules/components/useMarkdownDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ ${headers.components
`);
}

// eslint-disable-next-line no-underscore-dangle
global.__MARKED_UNIQUE__ = {};

const element = (
<React.Fragment>
<svg style={{ display: 'none' }} xmlns="http://www.w3.org/2000/svg">
Expand Down
6 changes: 6 additions & 0 deletions docs/src/modules/utils/textToHash.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ function makeUnique(hash, unique, i = 1) {
return makeUnique(hash, unique, i + 1);
}

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

0 comments on commit 0762f98

Please sign in to comment.