Skip to content
Closed
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
20 changes: 20 additions & 0 deletions packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,26 @@ 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 \`<metadata>\` documentation.
`),
).toBe('This paragraph includes a link to the &lt;metadata&gt; 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 [\`<metadata>\`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/metadata) documentation.
`),
).toBe('This paragraph includes a link to the &lt;metadata&gt; documentation.');
});
});

describe('parseMarkdownContentTitle', () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/docusaurus-utils/src/markdownUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export function createExcerpt(fileString: string): string | undefined {
}

const cleanedLine = fileLine
// Escape HTML entities inside inline code before removing HTML tags.
.replace(/`(?<text>.+?)`/g, (_, text) =>
`\`${text.replace(/</g, '&lt;').replace(/>/g, '&gt;')}\``,
)
// Remove HTML tags.
.replace(/<[^>]*>/g, '')
// Remove Title headers
Expand All @@ -144,7 +148,7 @@ export function createExcerpt(fileString: string): string | undefined {
.replace(/\[\^.+?\](?:: .*$)?/g, '')
// Remove inline links.
.replace(/\[(?<alt>.*?)\][[(].*?[\])]/g, '$1')
// Remove inline code.
// Remove inline code (now just removes backticks, content already escaped).
.replace(/`(?<text>.+?)`/g, '$1')
// Remove blockquotes.
.replace(/^\s{0,3}>\s?/g, '')
Expand Down