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
32 changes: 32 additions & 0 deletions packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,38 @@ describe('createExcerpt', () => {
);
});

it('skips H1 heading whose text contains a hash character (e.g. "C#", "F#")', () => {
expect(
createExcerpt(dedent`

# C# Programming Guide

This paragraph should become the description.
`),
).toBe('This paragraph should become the description.');
});

it('skips H1 heading with trailing closing hashes and interior hash', () => {
expect(
createExcerpt(dedent`

# F# Programming Guide #

This paragraph should become the description.
`),
).toBe('This paragraph should become the description.');
});

it('keeps hash characters inside non-heading text', () => {
expect(
createExcerpt(dedent`
The language C# is used with .NET, and F# is a functional alternative.

Nunc porttitor libero nec vulputate venenatis.
`),
).toBe('The language C# is used with .NET, and F# is a functional alternative.');
});

it('creates excerpt for regular content with alternate title', () => {
expect(
createExcerpt(dedent`
Expand Down
11 changes: 8 additions & 3 deletions packages/docusaurus-utils/src/markdownUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,15 @@ export function createExcerpt(fileString: string): string | undefined {
const cleanedLine = fileLine
// Remove HTML tags.
.replace(/<[^>]*>/g, '')
// Remove Title headers
.replace(/^#[^#]+#?/gm, '')
// Remove Title headers (single-# ATX headings are skipped entirely)
// Note: the heading text may legitimately contain "#" (e.g. "C#", "F#"),
// so we must match the whole line instead of using a negated class
.replace(/^#.*$/gm, '')
// Remove Markdown + ATX-style headers
.replace(/^#{1,6}\s*(?<text>[^#]*?)\s*#{0,6}/gm, '$1')
// Note: we must not forbid "#" inside the heading text (e.g. "C#", "F#")
// Only the leading marker and an optional trailing closing sequence are
// stripped, anchored to the end of the line to avoid eating interior "#"
.replace(/^#{1,6}\s*(?<text>.*?)\s*#*$/gm, '$1')
// Remove emphasis.
.replace(/(?<opening>[*_]{1,3})(?<text>.*?)\1/g, '$2')
// Remove strikethroughs.
Expand Down