Skip to content
Open
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
24 changes: 24 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,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 \`<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
9 changes: 7 additions & 2 deletions packages/docusaurus-utils/src/markdownUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ export function createExcerpt(fileString: string): string | undefined {
}

const cleanedLine = fileLine
// Remove inline code (must come before HTML tag removal so that
// XML-like tags inside backticks are not stripped as HTML).
// Angle brackets in code content are escaped to HTML entities so they
// survive the HTML-tag removal step that follows.
.replace(/`(?<text>.+?)`/g, (_match, text: string) =>
text.replace(/</g, '&lt;').replace(/>/g, '&gt;'),
)
// Remove HTML tags.
.replace(/<[^>]*>/g, '')
// Remove Title headers
Expand All @@ -144,8 +151,6 @@ export function createExcerpt(fileString: string): string | undefined {
.replace(/\[\^.+?\](?:: .*$)?/g, '')
// Remove inline links.
.replace(/\[(?<alt>.*?)\][[(].*?[\])]/g, '$1')
// Remove inline code.
.replace(/`(?<text>.+?)`/g, '$1')
// Remove blockquotes.
.replace(/^\s{0,3}>\s?/g, '')
// Remove admonition definition.
Expand Down