From a5535b30e1e8c5bc6b95a444ff0d631e46c48e97 Mon Sep 17 00:00:00 2001 From: anatolykopyl Date: Sat, 30 Dec 2023 17:52:21 +0300 Subject: [PATCH] Fixed usage in mdx code blocks and added a test page --- .../docusaurus-mdx-loader/src/remark/toc/index.ts | 9 ++++++--- .../_dogfooding/_pages tests/_anotherPagePartial.md | 7 +++++++ .../_dogfooding/_pages tests/{index.mdx => index.md} | 12 +++++++++--- website/_dogfooding/_pages tests/partials-tests.mdx | 12 ++++++++++++ 4 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 website/_dogfooding/_pages tests/_anotherPagePartial.md rename website/_dogfooding/_pages tests/{index.mdx => index.md} (80%) create mode 100644 website/_dogfooding/_pages tests/partials-tests.mdx diff --git a/packages/docusaurus-mdx-loader/src/remark/toc/index.ts b/packages/docusaurus-mdx-loader/src/remark/toc/index.ts index a1622d76326c..b0545b2aeaad 100644 --- a/packages/docusaurus-mdx-loader/src/remark/toc/index.ts +++ b/packages/docusaurus-mdx-loader/src/remark/toc/index.ts @@ -72,6 +72,7 @@ const removeTags = (input: string) => export default function plugin(): Transformer { return (root) => { + let importsCount = 0; const headings: (TOCItem | string)[] = []; const PartialComponentToHeadingsName = Object.create(null); @@ -100,10 +101,11 @@ export default function plugin(): Transformer { const imports = importNode.value .split('\n') .filter((statement) => markdownExtensionRegex.test(statement)); - for (let i = 0; i < imports.length; i += 1) { - const localName = `${name}${i}`; - const importWords = imports[i]!.split(' '); + for (const importStatement of imports) { + const localName = `${name}${importsCount}`; + + const importWords = importStatement!.split(' '); const partialPath = importWords[importWords.length - 1]; const partialName = importWords[1] as string; const tocImport = `import {${name} as ${localName}} from ${partialPath}`; @@ -111,6 +113,7 @@ export default function plugin(): Transformer { PartialComponentToHeadingsName[partialName] = localName; importNode.value = `${importNode.value}\n${tocImport}`; + importsCount += 1; } } diff --git a/website/_dogfooding/_pages tests/_anotherPagePartial.md b/website/_dogfooding/_pages tests/_anotherPagePartial.md new file mode 100644 index 000000000000..c5bb559a9b60 --- /dev/null +++ b/website/_dogfooding/_pages tests/_anotherPagePartial.md @@ -0,0 +1,7 @@ +### Another page partial content + +This is text coming from another page partial + +#### Foo + +Level 4 headings don't belong in ToC diff --git a/website/_dogfooding/_pages tests/index.mdx b/website/_dogfooding/_pages tests/index.md similarity index 80% rename from website/_dogfooding/_pages tests/index.mdx rename to website/_dogfooding/_pages tests/index.md index 5f36a695035b..2d8cd4771896 100644 --- a/website/_dogfooding/_pages tests/index.mdx +++ b/website/_dogfooding/_pages tests/index.md @@ -1,17 +1,22 @@ -import PagePartial from './_pagePartial.md'; -import Readme from '../README.md'; - ## Page Let's import a MDX partial at `./_pagePartial.md`: +```mdx-code-block +import PagePartial from "./_pagePartial.md" + +``` --- Now let's import `../README.md`: +```mdx-code-block +import Readme from "../README.md" + +``` ### Other tests @@ -26,4 +31,5 @@ Now let's import `../README.md`: - [Tabs tests](/tests/pages/tabs-tests) - [z-index tests](/tests/pages/z-index-tests) - [Head metadata tests](/tests/pages/head-metadata) +- [Partials tests](/tests/pages/partials-tests) - [Embeds](/tests/pages/embeds) diff --git a/website/_dogfooding/_pages tests/partials-tests.mdx b/website/_dogfooding/_pages tests/partials-tests.mdx new file mode 100644 index 000000000000..a9d5da82d829 --- /dev/null +++ b/website/_dogfooding/_pages tests/partials-tests.mdx @@ -0,0 +1,12 @@ +import PagePartial from './_pagePartial.md'; +import AnotherPagePartial from './_anotherPagePartial.md'; + +# Partials tests + +This page consists of multiple files imported into one. Notice how the table of contents works even for imported headings. + +## Imported content + + + +