From a9f836b296ee7a00d2f0808e542db755cdcc62be Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Thu, 19 Mar 2026 22:46:50 +0000 Subject: [PATCH] fix: ignore html tags in inline code syntax In Markdown, any special characters between inline code should be displayed literally. Before if XML tags were shown inside inline code, they were removed because they were HTML tags, but this is wrong. XML tags should only be removed if they're outside of inline code blocks. --- .../src/__tests__/markdownUtils.test.ts | 24 +++++++++++++++++++ .../docusaurus-utils/src/markdownUtils.ts | 6 +++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts index 71be6442e082..cb5c4960223d 100644 --- a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts @@ -184,6 +184,30 @@ describe('createExcerpt', () => { `), ).toBe('Lorem ipsum dolor sit amet, consectetur adipiscing elit.'); }); + + it('creates excerpt with XML tag inside inline code', () => { + expect( + createExcerpt(dedent` + # Markdown Regular Title + + This paragraph includes a link to the \`\` documentation. + `), + ).toBe( + 'This paragraph includes a link to the <metadata> documentation.', + ); + }); + + it('creates excerpt with XML tag inside inline code with hyperlink', () => { + expect( + createExcerpt(dedent` + # Markdown Regular Title + + This paragraph includes a link to the [\`\`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/metadata) documentation. + `), + ).toBe( + 'This paragraph includes a link to the <metadata> documentation.', + ); + }); }); describe('parseMarkdownContentTitle', () => { diff --git a/packages/docusaurus-utils/src/markdownUtils.ts b/packages/docusaurus-utils/src/markdownUtils.ts index cef01cb70c1c..309f47c846a7 100644 --- a/packages/docusaurus-utils/src/markdownUtils.ts +++ b/packages/docusaurus-utils/src/markdownUtils.ts @@ -128,6 +128,10 @@ export function createExcerpt(fileString: string): string | undefined { } const cleanedLine = fileLine + // Remove inline code. + .replace(/`(?.+?)`/g, (_match, p1) => { + return p1.replaceAll('<', '<').replaceAll('>', '>'); + }) // Remove HTML tags. .replace(/<[^>]*>/g, '') // Remove Title headers @@ -144,8 +148,6 @@ export function createExcerpt(fileString: string): string | undefined { .replace(/\[\^.+?\](?:: .*$)?/g, '') // Remove inline links. .replace(/\[(?.*?)\][[(].*?[\])]/g, '$1') - // Remove inline code. - .replace(/`(?.+?)`/g, '$1') // Remove blockquotes. .replace(/^\s{0,3}>\s?/g, '') // Remove admonition definition.