From 2f6cddb8d5924447fc8665fc90eb259fdb29411f Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Thu, 12 Oct 2023 13:36:07 +0200 Subject: [PATCH] wip: parse tree from root --- .../__tests__/__fixtures__/directives.md | 4 +- .../__snapshots__/index.test.ts.snap | 3 +- .../src/remark/unusedDirectives/index.ts | 44 +++++-------------- 3 files changed, 16 insertions(+), 35 deletions(-) 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 index 2ca41c7e9e9e..708d435b2466 100644 --- a/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/directives.md +++ b/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/directives.md @@ -1,9 +1,11 @@ Test directives -:base +:base :base Text:base +Text\:base + Text: base ::base 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 0c4fd3d8244a..e72f9cbc61ce 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 @@ -2,8 +2,9 @@ exports[`directives remark plugin default behavior for custom keyword 1`] = ` "
Test directives
- +Text
+Text:base
Text: base
::::info Weather
diff --git a/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/index.ts b/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/index.ts
index c0845c49638d..391c7189fdbb 100644
--- a/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/index.ts
+++ b/packages/docusaurus-mdx-loader/src/remark/unusedDirectives/index.ts
@@ -8,13 +8,6 @@ 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
// See https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1517839391
@@ -22,9 +15,9 @@ import type {
type Plugin = any; // TODO fix this asap
const plugin: Plugin = function plugin(this: Processor): Transformer {
- return (root) => {
+ return (tree) => {
const unusedDirectives: Array<{
- name: string;
+ name: string | null;
type: string;
}> = [];
const directiveTypes = [
@@ -33,9 +26,7 @@ const plugin: Plugin = function plugin(this: Processor): Transformer {
'textDirective',
];
- const directiveVisitor = (
- node: ContainerDirective | LeafDirective | TextDirective,
- ) => {
+ const directiveVisitor = (node: any) => {
if (directiveTypes.includes(node.type)) {
unusedDirectives.push({
name: node.name,
@@ -43,31 +34,18 @@ const plugin: Plugin = function plugin(this: Processor): Transformer {
// start: node.position.start.line,
// path: ` ${filePath}:${node.position.start.line}:${node.position.start.column}`,
});
+
+ if (node.children) {
+ node.children.forEach((child: any) => directiveVisitor(child));
+ }
}
};
- // const directiveVisitor = (
- // node: ContainerDirective | LeafDirective | TextDirective,
- // ) => {
- // // Convert the directive to plain text and add it to the
- // // unusedDirectives array
-
- // unusedDirectives.push(directiveText);
-
- // // Remove the directive from the tree
- // if (parent) {
- // const index = parent.children.indexOf(node);
- // if (index !== -1) {
- // parent.children.splice(index, 1);
- // }
- // }
- // };
-
- visit