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.