Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs-infra] Fix code removal in table of content #39165

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/markdown/parseMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,9 @@ function createRender(context) {
}

// Remove links to avoid nested links in the TOCs
let headingText = headingHtml.replace(/<a\b[^>]*>/i, '').replace(/<\/a>/i, '');
let headingText = headingHtml.replace(/<a\b[^>]*>/gi, '').replace(/<\/a>/gi, '');
// Remove `code` tags
headingText = headingText.replace(/<code\b[^>]*>/i, '').replace(/<\/code>/i, '');
headingText = headingText.replace(/<code\b[^>]*>/gi, '').replace(/<\/code>/gi, '');

// Standardizes the hash from the default location (en) to different locations
// Need english.md file parsed first
Expand Down
47 changes: 47 additions & 0 deletions packages/markdown/parseMarkdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getHeaders,
getCodeblock,
renderMarkdown,
createRender,
} from './parseMarkdown';

describe('parseMarkdown', () => {
Expand Down Expand Up @@ -276,4 +277,50 @@ authors:
);
});
});

describe('createRender', () => {
it('should collect headers correctly', () => {
const context = { toc: [], headingHashes: {} };
const render = createRender(context);

expect(
render(
[
'# Accordion',
'## Basic features 🧪',
'## Using `slots` and `slotProps`',
'### Specific example',
].join('\n'),
),
).to.equal(
[
`<h1>Accordion</h1>`,
`<h2 id="basic-features">Basic features 🧪<a aria-labelledby="basic-features" class="anchor-link" href="#basic-features" tabindex="-1"><svg><use xlink:href="#anchor-link-icon" /></svg></a><button title="Post a comment" class="comment-link" data-feedback-hash="basic-features"><svg><use xlink:href="#comment-link-icon" /></svg></button></h2>`,
`<h2 id="using-slots-and-slotprops">Using <code>slots</code> and <code>slotProps</code><a aria-labelledby="using-slots-and-slotprops" class="anchor-link" href="#using-slots-and-slotprops" tabindex="-1"><svg><use xlink:href="#anchor-link-icon" /></svg></a><button title="Post a comment" class="comment-link" data-feedback-hash="using-slots-and-slotprops"><svg><use xlink:href="#comment-link-icon" /></svg></button></h2>`,
`<h3 id="specific-example">Specific example<a aria-labelledby="specific-example" class="anchor-link" href="#specific-example" tabindex="-1"><svg><use xlink:href="#anchor-link-icon" /></svg></a><button title="Post a comment" class="comment-link" data-feedback-hash="specific-example"><svg><use xlink:href="#comment-link-icon" /></svg></button></h3>`,
].join(''),
);

expect(context.toc).to.deep.equal([
{
children: [],
hash: 'basic-features',
level: 2,
text: 'Basic features 🧪',
},
{
children: [
{
hash: 'specific-example',
level: 3,
text: 'Specific example',
},
],
hash: 'using-slots-and-slotprops',
level: 2,
text: 'Using slots and slotProps',
},
]);
});
});
});