-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...rus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/directives.md
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
...usaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__snapshots__/index.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`directives remark plugin default behavior for custom keyword 1`] = ` | ||
"<p>Test directives</p> | ||
<p><div></div></p> | ||
<p>Text<div></div></p> | ||
<p>Text: base</p> | ||
<div></div> | ||
<div><p>::::info <strong>Weather</strong> | ||
On nice days, you can enjoy skiing in the mountains.</p><p>:::danger <em>Storms</em> | ||
Take care of snowstorms...</p></div> | ||
<p>::::</p>" | ||
`; |
49 changes: 49 additions & 0 deletions
49
packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
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'; | ||
|
||
const processFixture = async (name: string) => { | ||
const {remark} = await import('remark'); | ||
const {default: directives} = await import('remark-directive'); | ||
|
||
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(plugin) | ||
.use(remark2rehype) | ||
.use(stringify) | ||
.process(fileContentPreprocessed); | ||
|
||
return result.value; | ||
}; | ||
|
||
describe('directives remark plugin', () => { | ||
it('default behavior for custom keyword', async () => { | ||
const result = await processFixture('directives'); | ||
expect(result).toMatchSnapshot(); | ||
}); | ||
}); |
74 changes: 74 additions & 0 deletions
74
packages/docusaurus-mdx-loader/src/remark/unusedDirectives/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
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) => { | ||
const unusedDirectives: Array<{ | ||
name: string; | ||
type: string; | ||
}> = []; | ||
const directiveTypes = [ | ||
'containerDirective', | ||
'leafDirective', | ||
'textDirective', | ||
]; | ||
|
||
const directiveVisitor = ( | ||
node: ContainerDirective | LeafDirective | TextDirective, | ||
) => { | ||
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}`, | ||
}); | ||
} | ||
}; | ||
|
||
// 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); | ||
|
||
console.log('Unused Directives:', unusedDirectives); | ||
}; | ||
}; | ||
|
||
export default plugin; |