From 1700a293c483d38b8adee5e4da10f9263e72f589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lorber?= Date: Sat, 11 Nov 2023 22:23:27 +0100 Subject: [PATCH] fix: v3 admonitions should support v2 title syntax for nested admonitions (#9535) Fix admonition title mdx v1 compat option when admonition is nested --- .../src/__tests__/markdownUtils.test.ts | 67 +++++++++++++++++-- .../docusaurus-utils/src/markdownUtils.ts | 4 +- .../_docs tests/tests/admonitions.mdx | 20 ++++++ 3 files changed, 82 insertions(+), 9 deletions(-) diff --git a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts index 9a1ecbee324a..3134835e262a 100644 --- a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts @@ -1288,17 +1288,23 @@ describe('admonitionTitleToDirectiveLabel', () => { `); }); - it('does not transform left-padded directives', () => { + it('transforms space indented directives', () => { expect( admonitionTitleToDirectiveLabel( dedent` before - :::note Title + :::note 1 space - content + content - ::: + ::: + + :::note 2 spaces + + content + + ::: after `, @@ -1307,16 +1313,63 @@ describe('admonitionTitleToDirectiveLabel', () => { ).toEqual(dedent` before - :::note Title + :::note[1 space] - content + content - ::: + ::: + + :::note[2 spaces] + + content + + ::: after `); }); + it('transforms tab indented directives', () => { + expect( + admonitionTitleToDirectiveLabel( + ` +before + +\t:::note 1 tab + +\tcontent + +\t::: + +\t\t:::note 2 tabs + +\t\tcontent + +\t\t::: + +after + `, + directives, + ), + ).toBe(` +before + +\t:::note[1 tab] + +\tcontent + +\t::: + +\t\t:::note[2 tabs] + +\t\tcontent + +\t\t::: + +after + `); + }); + it('does not transform admonition without title', () => { expect( admonitionTitleToDirectiveLabel( diff --git a/packages/docusaurus-utils/src/markdownUtils.ts b/packages/docusaurus-utils/src/markdownUtils.ts index dc8589ac71c5..62608db49f1d 100644 --- a/packages/docusaurus-utils/src/markdownUtils.ts +++ b/packages/docusaurus-utils/src/markdownUtils.ts @@ -97,14 +97,14 @@ export function admonitionTitleToDirectiveLabel( const directiveNameGroup = `(${admonitionContainerDirectives.join('|')})`; const regexp = new RegExp( - `^(?:{3,}${directiveNameGroup}) +(?.*)$`, + `^(?<indentation>( +|\t+))?(?<directive>:{3,}${directiveNameGroup}) +(?<title>.*)$`, 'gm', ); return content.replaceAll(regexp, (substring, ...args: any[]) => { const groups = args.at(-1); - return `${groups.directive}[${groups.title}]`; + return `${groups.indentation ?? ''}${groups.directive}[${groups.title}]`; }); } diff --git a/website/_dogfooding/_docs tests/tests/admonitions.mdx b/website/_dogfooding/_docs tests/tests/admonitions.mdx index 9c41cffec208..5094cc36a1a9 100644 --- a/website/_dogfooding/_docs tests/tests/admonitions.mdx +++ b/website/_dogfooding/_docs tests/tests/admonitions.mdx @@ -54,6 +54,26 @@ import InfoIcon from "@theme/Admonition/Icon/Info" </Admonition> ``` +## Indented admonitions + +See admonition title v2 compat syntax bug: https://github.com/facebook/docusaurus/issues/9507 + +1. Item 1 + + :::info Important Considerations + + For better experience, try to keep the upgrade experience smooth. + + ::: + +- **Scale-up cluster** + + :::caution Warning + + Scaling up a cluster may cause several minutes of downtime. Please exercise caution. + + ::: + ## Official admonitions Admonitions that are [officially documented](/docs/markdown-features/admonitions)