fix(utils): allow any non-boundary characters in Markdown heading ID (#7604)

This commit is contained in:
Joshua Chen 2022-06-16 18:32:42 +08:00 committed by GitHub
parent 0114f00069
commit 89e146f596
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 4 deletions

View file

@ -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
@ -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 {