From ac81765024f9988da8f208b66191bda2fd76db90 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Thu, 17 Nov 2022 05:38:10 -0500 Subject: [PATCH] fix(utils): allow partially backticked markdown h1 contentTitles (#8314) --- .../src/__tests__/markdownUtils.test.ts | 16 +++++++++++++++- packages/docusaurus-utils/src/markdownUtils.ts | 5 +---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts index 42df2a38d4f1..579c47e826a0 100644 --- a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts @@ -195,7 +195,7 @@ describe('parseMarkdownContentTitle', () => { }); }); - it('parses markdown h1 title at the top and unwrap inline code block', () => { + it('parses markdown h1 title inside backticks at the top and unwrap inline code block', () => { const markdown = dedent` # \`Markdown Title\` @@ -209,6 +209,20 @@ describe('parseMarkdownContentTitle', () => { }); }); + it('parses markdown h1 title with interspersed backticks at the top and unwrap inline code block', () => { + const markdown = dedent` + + # Markdown \`Title\` With \`Many\` Backticks! + + Lorem Ipsum + + `; + expect(parseMarkdownContentTitle(markdown)).toEqual({ + content: markdown, + contentTitle: 'Markdown Title With Many Backticks!', + }); + }); + it('parses markdown h1 title and trim content', () => { const markdown = ` diff --git a/packages/docusaurus-utils/src/markdownUtils.ts b/packages/docusaurus-utils/src/markdownUtils.ts index 512a09bf916d..f97638a49d60 100644 --- a/packages/docusaurus-utils/src/markdownUtils.ts +++ b/packages/docusaurus-utils/src/markdownUtils.ts @@ -155,10 +155,7 @@ export function parseFrontMatter(markdownFileContent: string): { } function toTextContentTitle(contentTitle: string): string { - if (contentTitle.startsWith('`') && contentTitle.endsWith('`')) { - return contentTitle.substring(1, contentTitle.length - 1); - } - return contentTitle; + return contentTitle.replace(/`(?[^`]*)`/g, '$'); } type ParseMarkdownContentTitleOptions = {