diff --git a/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/containerDirectives.md b/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/containerDirectives.md new file mode 100644 index 000000000000..2a416f0ca72b --- /dev/null +++ b/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/containerDirectives.md @@ -0,0 +1,19 @@ +:::danger + +Take care of snowstorms... + +::: + +:::unusedDirective + +unused directive content + +::: + +:::NotAContainerDirective with a phrase after + +::: + +Phrase before :::NotAContainerDirective + +::: diff --git a/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/directives.md b/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/directives.md deleted file mode 100644 index 708d435b2466..000000000000 --- a/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/directives.md +++ /dev/null @@ -1,22 +0,0 @@ -Test directives - -:base :base - -Text:base - -Text\:base - -Text: base - -::base - -:::base - -::::info **Weather** -On nice days, you can enjoy skiing in the mountains. - -:::danger *Storms* -Take care of snowstorms... -::: - -:::: diff --git a/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/leafDirectives.md b/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/leafDirectives.md new file mode 100644 index 000000000000..73fd60c77b67 --- /dev/null +++ b/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/leafDirectives.md @@ -0,0 +1,5 @@ +::unusedLeafDirective + +Leaf directive in a phrase ::NotALeafDirective + +::NotALeafDirective with a phrase after diff --git a/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/textDirectives.md b/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/textDirectives.md new file mode 100644 index 000000000000..0bc5f857cd00 --- /dev/null +++ b/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/textDirectives.md @@ -0,0 +1,11 @@ +Simple: textDirective1 + +```sh +Simple: textDirective1 +``` + +Simple:textDirective2 + +```sh +Simple:textDirective1 +``` diff --git a/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__snapshots__/index.test.ts.snap index e72f9cbc61ce..2db29bba1a0e 100644 --- a/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__snapshots__/index.test.ts.snap @@ -1,14 +1,28 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`directives remark plugin default behavior for custom keyword 1`] = ` -"
Test directives
- -Text
-Text:base
-Text: base
- -::::info Weather -On nice days, you can enjoy skiing in the mountains.
:::danger Storms -Take care of snowstorms...
::::
" +"Take care of snowstorms...
unused directive content
:::NotAContainerDirective with a phrase after
+:::
+Phrase before :::NotAContainerDirective
+:::
" +`; + +exports[`directives remark plugin default behavior for custom keyword 2`] = ` +[ + [ + "Unused Directives found: ", + [ + { + "name": "danger", + "type": "containerDirective", + }, + { + "name": "unusedDirective", + "type": "containerDirective", + }, + ], + ], +] `; diff --git a/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/index.test.ts b/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/index.test.ts index 205ecf6aa058..612dfccb6e3e 100644 --- a/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/index.test.ts +++ b/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/index.test.ts @@ -9,8 +9,8 @@ import path from 'path'; import remark2rehype from 'remark-rehype'; import stringify from 'rehype-stringify'; import vfile from 'to-vfile'; -import preprocessor from '../../../preprocessor'; import plugin from '../index'; +import admonition from '../../admonitions'; const processFixture = async (name: string) => { const {remark} = await import('remark'); @@ -18,32 +18,28 @@ const processFixture = async (name: string) => { const filePath = path.join(__dirname, '__fixtures__', `${name}.md`); const file = await vfile.read(filePath); - const fileContentPreprocessed = preprocessor({ - fileContent: file.toString(), - filePath, - markdownConfig: { - mermaid: false, - mdx1Compat: { - admonitions: false, - comments: false, - headingIds: false, - }, - }, - }); const result = await remark() .use(directives) + .use(admonition) .use(plugin) .use(remark2rehype) .use(stringify) - .process(fileContentPreprocessed); + .process(file); return result.value; }; describe('directives remark plugin', () => { it('default behavior for custom keyword', async () => { - const result = await processFixture('directives'); + const consoleMock = jest + .spyOn(console, 'warn') + .mockImplementation(() => {}); + + const result = await processFixture('containerDirectives'); + expect(result).toMatchSnapshot(); + + expect(consoleMock.mock.calls).toMatchSnapshot(); }); }); diff --git a/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/index.ts b/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/index.ts index 391c7189fdbb..46e22cf66aab 100644 --- a/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/index.ts +++ b/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/index.ts @@ -7,6 +7,12 @@ import visit from 'unist-util-visit'; // @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721 import type {Transformer, Processor, Parent} from 'unified'; +// import type { +// ContainerDirective, +// LeafDirective, +// TextDirective, +// // @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721 +// } from 'mdast-util-directive'; // TODO as of April 2023, no way to import/re-export this ESM type easily :/ // This might change soon, likely after TS 5.2 @@ -20,6 +26,7 @@ const plugin: Plugin = function plugin(this: Processor): Transformer { name: string | null; type: string; }> = []; + const directiveTypes = [ 'containerDirective', 'leafDirective', @@ -35,16 +42,17 @@ const plugin: Plugin = function plugin(this: Processor): Transformer { // path: ` ${filePath}:${node.position.start.line}:${node.position.start.column}`, }); - if (node.children) { - node.children.forEach((child: any) => directiveVisitor(child)); - } + // if (node.children) { + // node.children.forEach((child: any) => directiveVisitor(child)); + // } } }; - visit