Skip to content

Commit

Permalink
fix: add v2 retrocompatible support for quoted admonitions (#9570)
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber authored Nov 21, 2023
1 parent 2c0bf8a commit 7c32fc3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
51 changes: 51 additions & 0 deletions packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,57 @@ before
\t\t:::
after
`);
});

it('transforms directives in quotes', () => {
expect(
admonitionTitleToDirectiveLabel(
`
before
> :::caution There be dragons
>
> This is the admonition content
>
> :::
>
>> :::caution There be dragons
>>
>> This is the admonition content
>>
>> :::
> > :::caution There be dragons
> >
> > This is the admonition content
> >
> > :::
after
`,
directives,
),
).toBe(`
before
> :::caution[There be dragons]
>
> This is the admonition content
>
> :::
>
>> :::caution[There be dragons]
>>
>> This is the admonition content
>>
>> :::
> > :::caution[There be dragons]
> >
> > This is the admonition content
> >
> > :::
after
`);
});
Expand Down
6 changes: 4 additions & 2 deletions packages/docusaurus-utils/src/markdownUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,16 @@ export function admonitionTitleToDirectiveLabel(

const directiveNameGroup = `(${admonitionContainerDirectives.join('|')})`;
const regexp = new RegExp(
`^(?<indentation>( +|\t+))?(?<directive>:{3,}${directiveNameGroup}) +(?<title>.*)$`,
`^(?<quote>(> ?)*)(?<indentation>( +|\t+))?(?<directive>:{3,}${directiveNameGroup}) +(?<title>.*)$`,
'gm',
);

return content.replaceAll(regexp, (substring, ...args: any[]) => {
const groups = args.at(-1);

return `${groups.indentation ?? ''}${groups.directive}[${groups.title}]`;
return `${groups.quote ?? ''}${groups.indentation ?? ''}${
groups.directive
}[${groups.title}]`;
});
}

Expand Down
14 changes: 14 additions & 0 deletions website/_dogfooding/_docs tests/tests/admonitions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ See admonition title v2 compat syntax bug: https://github.com/facebook/docusauru

:::

## Quoted admonitions

> :::caution There be dragons
>
> This is the admonition content
>
> :::
>
> > :::caution There be dragons
> >
> > This is the admonition content
> >
> > :::
## Official admonitions

Admonitions that are [officially documented](/docs/markdown-features/admonitions)
Expand Down

0 comments on commit 7c32fc3

Please sign in to comment.