Skip to content

Commit

Permalink
fix(utils): allow any non-boundary characters in Markdown heading id
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Jun 12, 2022
1 parent feb9cf0 commit 99aca51
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
47 changes: 47 additions & 0 deletions packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts
Expand Up @@ -833,6 +833,53 @@ describe('parseMarkdownHeadingId', () => {
id: 'id',
});
});

it('does not parse empty id', () => {
expect(parseMarkdownHeadingId('## a {#}')).toEqual({
text: '## a {#}',
id: undefined,
});
});

it('can parse id with more characters', () => {
expect(parseMarkdownHeadingId('## a {#你好}')).toEqual({
text: '## a',
id: '你好',
});

expect(parseMarkdownHeadingId('## a {#2022.1.1}')).toEqual({
text: '## a',
id: '2022.1.1',
});

expect(parseMarkdownHeadingId('## a {#a#b}')).toEqual({
text: '## a',
id: 'a#b',
});
});

// The actual behavior is unspecified, just need to ensure it stays consistent
it('handles unmatched boundaries', () => {
expect(parseMarkdownHeadingId('## a {# a {#bcd}')).toEqual({
text: '## a {# a',
id: 'bcd',
});

expect(parseMarkdownHeadingId('## a {#bcd}}')).toEqual({
text: '## a {#bcd}}',
id: undefined,
});

expect(parseMarkdownHeadingId('## a {#b{cd}')).toEqual({
text: '## a',
id: 'b{cd',
});

expect(parseMarkdownHeadingId('## a {#b{#b}')).toEqual({
text: '## a {#b',
id: 'b',
});
});
});

describe('writeMarkdownHeadingId', () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/docusaurus-utils/src/markdownUtils.ts
Expand Up @@ -14,8 +14,8 @@ import {createSlugger, type Slugger, type SluggerOptions} from './slugger';
// content. Most parsing is still done in MDX through the mdx-loader.

/**
* Parses custom ID from a heading. The ID must be composed of letters,
* underscores, and dashes only.
* Parses custom ID from a heading. The ID can contain any characters except
* `{#` and `}`.
*
* @param heading e.g. `## Some heading {#some-heading}` where the last
* character must be `}` for the ID to be recognized
Expand All @@ -26,9 +26,9 @@ export function parseMarkdownHeadingId(heading: string): {
*/
text: string;
/** The heading ID. e.g. `some-heading` */
id?: string;
id: string | undefined;
} {
const customHeadingIdRegex = /\s*\{#(?<id>[\w-]+)\}$/;
const customHeadingIdRegex = /\s*\{#(?<id>(?:.(?!\{#|\}))*.)\}$/;
const matches = customHeadingIdRegex.exec(heading);
if (matches) {
return {
Expand Down

0 comments on commit 99aca51

Please sign in to comment.