From 3c9c24ae32fec3df2d401f13973cb850e3dd3e72 Mon Sep 17 00:00:00 2001 From: alexandre Date: Tue, 26 Sep 2023 10:26:35 +0200 Subject: [PATCH] [docs-infra] Fix code removal in table of content --- packages/markdown/parseMarkdown.js | 4 +-- packages/markdown/parseMarkdown.test.js | 47 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/packages/markdown/parseMarkdown.js b/packages/markdown/parseMarkdown.js index dd58a4aca927ba..04d8667e8191cd 100644 --- a/packages/markdown/parseMarkdown.js +++ b/packages/markdown/parseMarkdown.js @@ -302,9 +302,9 @@ function createRender(context) { } // Remove links to avoid nested links in the TOCs - let headingText = headingHtml.replace(/]*>/i, '').replace(/<\/a>/i, ''); + let headingText = headingHtml.replace(/]*>/gi, '').replace(/<\/a>/gi, ''); // Remove `code` tags - headingText = headingText.replace(/]*>/i, '').replace(/<\/code>/i, ''); + headingText = headingText.replace(/]*>/gi, '').replace(/<\/code>/gi, ''); // Standardizes the hash from the default location (en) to different locations // Need english.md file parsed first diff --git a/packages/markdown/parseMarkdown.test.js b/packages/markdown/parseMarkdown.test.js index 53bf142babe0d0..20cf39596a17ab 100644 --- a/packages/markdown/parseMarkdown.test.js +++ b/packages/markdown/parseMarkdown.test.js @@ -6,6 +6,7 @@ import { getHeaders, getCodeblock, renderMarkdown, + createRender, } from './parseMarkdown'; describe('parseMarkdown', () => { @@ -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( + [ + `

Accordion

`, + `

Basic features 🧪

`, + `

Using slots and slotProps

`, + `

Specific example

`, + ].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', + }, + ]); + }); + }); });