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
2 changes: 1 addition & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@
"*.min.*",
"jest/vendor"
],
"ignoreRegExpList": ["Email", "Urls", "#[\\w-]*"],
"ignoreRegExpList": ["Email", "Urls", "#[\\w-]*", "\\u\\{[0-9A-F]{4}\\}"],
"enableFiletypes": ["mdx"]
}
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 @@ -197,6 +197,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 <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 [\`<metadata>\`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/metadata) documentation.
`),
).toBe('This paragraph includes a link to the <metadata> documentation.');
});
});

describe('parseMarkdownContentTitle', () => {
Expand Down
18 changes: 15 additions & 3 deletions packages/docusaurus-utils/src/markdownUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import type {
// server-side when we infer metadata like `title` and `description` from the
// content. Most parsing is still done in MDX through the mdx-loader.

/**
* Characters that have markup behavior for the Markdown/MDX renderer to
* interpret.
*/
const MARKUP_CHARS = ['_', ':', '*', '<', '>', '~', '!', '[', ']'];

/**
* Hacky temporary escape hatch for Crowdin bad MDX support
* See https://docusaurus.io/docs/i18n/crowdin#mdx
Expand Down Expand Up @@ -143,6 +149,12 @@ export function createExcerpt(fileString: string): string | undefined {
}

const cleanedLine = fileLine
// Unwrap inline code.
.replace(/`(?<text>.+?)`/g, (_, text) => {
return MARKUP_CHARS.reduce((acc, val) => {
return acc.replaceAll(val, `\u{FFFE}${val.codePointAt(0)}\u{FFFF}`);
}, text);
})
// Remove HTML tags.
.replace(/<[^>]*>/g, '')
// Remove Title headers
Expand All @@ -159,8 +171,6 @@ export function createExcerpt(fileString: string): string | undefined {
.replace(/\[\^.+?\](?:: .*$)?/g, '')
// Remove inline links.
.replace(/\[(?<alt>.*?)\][[(].*?[\])]/g, '$1')
// Remove inline code.
.replace(/`(?<text>.+?)`/g, '$1')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand why you moved this to the top: to escape tags before HTML tags get removed

Unfortunately, escaping doesn't work, so one solution that could work would be to use marker tags for < and > found in inline code blocks: you could replace these markers with their former < > values after having removed the HTML tags

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've redone the PR but using the markers approach as you suggested. Please let me know if it's not what you had in mind!

(More details in my commit message!)

// Remove blockquotes.
.replace(/^\s{0,3}>\s?/g, '')
// Remove admonition definition.
Expand All @@ -172,7 +182,9 @@ export function createExcerpt(fileString: string): string | undefined {
.trim();

if (cleanedLine) {
return cleanedLine;
return MARKUP_CHARS.reduce((acc, val) => {
return acc.replaceAll(`\u{FFFE}${val.codePointAt(0)}\u{FFFF}`, val);
}, cleanedLine);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Doc with Complex Description

One <strong>Two</strong> _Three_ `<Four>` `:Five:` <em>Six</em> ![Seven](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX///+/v7+jQ3Y5AAAADklEQVQI12P4AIX8EAgALgAD/aNpbtEAAAAASUVORK5CYII)
1 change: 1 addition & 0 deletions website/_dogfooding/docs-tests-sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const sidebars = {
},
'doc-without-sidebar',
'doc-with-another-sidebar',
'doc-with-complex-description',
'doc-with-last-update',
{
type: 'category',
Expand Down
Loading