Skip to content

Commit

Permalink
wip: parse tree from root
Browse files Browse the repository at this point in the history
  • Loading branch information
OzakIOne committed Oct 12, 2023
1 parent 1c2b05b commit 2f6cddb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 35 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

exports[`directives remark plugin default behavior for custom keyword 1`] = `
"<p>Test directives</p>
<p><div></div></p>
<p><div></div> <div></div></p>
<p>Text<div></div></p>
<p>Text:base</p>
<p>Text: base</p>
<div></div>
<div><p>::::info <strong>Weather</strong>
Expand Down
44 changes: 11 additions & 33 deletions packages/docusaurus-mdx-loader/src/remark/unusedDirectives/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,16 @@ 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
// import type {Plugin} from 'unified';
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 = [
Expand All @@ -33,41 +26,26 @@ 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,
type: node.type,
// 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<Parent>(root, 'containerDirective', directiveVisitor);
visit<Parent>(root, 'leafDirective', directiveVisitor);
visit<Parent>(root, 'textDirective', directiveVisitor);
visit<Parent>(tree, 'root', directiveVisitor);

console.log('Unused Directives:', unusedDirectives);
if (unusedDirectives.length > 0) {
console.warn('unusedDirectives', unusedDirectives);
}
};
};

Expand Down

0 comments on commit 2f6cddb

Please sign in to comment.