Skip to content

Commit

Permalink
Fixed usage in mdx code blocks and added a test page
Browse files Browse the repository at this point in the history
  • Loading branch information
anatolykopyl committed Dec 30, 2023
1 parent 1ebd01d commit a5535b3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
9 changes: 6 additions & 3 deletions packages/docusaurus-mdx-loader/src/remark/toc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -100,17 +101,19 @@ 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}`;

PartialComponentToHeadingsName[partialName] = localName;

importNode.value = `${importNode.value}\n${tocImport}`;
importsCount += 1;
}
}

Expand Down
7 changes: 7 additions & 0 deletions website/_dogfooding/_pages tests/_anotherPagePartial.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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"
<PagePartial />
```

---

Now let's import `../README.md`:

```mdx-code-block
import Readme from "../README.md"
<Readme />
```

### Other tests

Expand All @@ -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)
12 changes: 12 additions & 0 deletions website/_dogfooding/_pages tests/partials-tests.mdx
Original file line number Diff line number Diff line change
@@ -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

<PagePartial />

<AnotherPagePartial />

0 comments on commit a5535b3

Please sign in to comment.