mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-27 13:38:33 +02:00
refactor: remove a lot of implicit anys (#7468)
This commit is contained in:
parent
0c8e57de67
commit
3666a2ede5
23 changed files with 148 additions and 163 deletions
|
@ -75,50 +75,6 @@ exports[`toc remark plugin exports even with existing name 1`] = `
|
|||
"
|
||||
`;
|
||||
|
||||
exports[`toc remark plugin exports with custom name 1`] = `
|
||||
"export const customName = [
|
||||
{
|
||||
value: 'Endi',
|
||||
id: 'endi',
|
||||
level: 3
|
||||
},
|
||||
{
|
||||
value: 'Endi',
|
||||
id: 'endi-1',
|
||||
level: 2
|
||||
},
|
||||
{
|
||||
value: 'Yangshun',
|
||||
id: 'yangshun',
|
||||
level: 3
|
||||
},
|
||||
{
|
||||
value: 'I ♥ unicode.',
|
||||
id: 'i--unicode',
|
||||
level: 2
|
||||
}
|
||||
];
|
||||
|
||||
### Endi
|
||||
|
||||
\`\`\`md
|
||||
## This is ignored
|
||||
\`\`\`
|
||||
|
||||
## Endi
|
||||
|
||||
Lorem ipsum
|
||||
|
||||
### Yangshun
|
||||
|
||||
Some content here
|
||||
|
||||
## I ♥ unicode.
|
||||
|
||||
export const c = 1;
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`toc remark plugin handles empty headings 1`] = `
|
||||
"export const toc = [];
|
||||
|
||||
|
|
|
@ -12,13 +12,13 @@ import vfile from 'to-vfile';
|
|||
import plugin from '../index';
|
||||
import headings from '../../headings/index';
|
||||
|
||||
const processFixture = async (name, options?) => {
|
||||
const processFixture = async (name: string) => {
|
||||
const filePath = path.join(__dirname, '__fixtures__', `${name}.md`);
|
||||
const file = await vfile.read(filePath);
|
||||
const result = await remark()
|
||||
.use(headings)
|
||||
.use(mdx)
|
||||
.use(plugin, options)
|
||||
.use(plugin)
|
||||
.process(file);
|
||||
|
||||
return result.toString();
|
||||
|
@ -45,14 +45,6 @@ describe('toc remark plugin', () => {
|
|||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('exports with custom name', async () => {
|
||||
const options = {
|
||||
name: 'customName',
|
||||
};
|
||||
const result = await processFixture('just-content', options);
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('inserts below imports', async () => {
|
||||
const result = await processFixture('insert-below-imports');
|
||||
expect(result).toMatchSnapshot();
|
||||
|
|
|
@ -23,15 +23,13 @@ const parseOptions: ParserOptions = {
|
|||
sourceType: 'module',
|
||||
};
|
||||
|
||||
const name = 'toc';
|
||||
|
||||
const isImport = (child: Node): child is Literal => child.type === 'import';
|
||||
const hasImports = (index: number) => index > -1;
|
||||
const isExport = (child: Node): child is Literal => child.type === 'export';
|
||||
|
||||
type PluginOptions = {
|
||||
name?: string;
|
||||
};
|
||||
|
||||
const isTarget = (child: Literal, name: string) => {
|
||||
const isTarget = (child: Literal) => {
|
||||
let found = false;
|
||||
const ast = parse(child.value, parseOptions);
|
||||
traverse(ast, {
|
||||
|
@ -44,14 +42,14 @@ const isTarget = (child: Literal, name: string) => {
|
|||
return found;
|
||||
};
|
||||
|
||||
const getOrCreateExistingTargetIndex = (children: Node[], name: string) => {
|
||||
const getOrCreateExistingTargetIndex = (children: Node[]) => {
|
||||
let importsIndex = -1;
|
||||
let targetIndex = -1;
|
||||
|
||||
children.forEach((child, index) => {
|
||||
if (isImport(child)) {
|
||||
importsIndex = index;
|
||||
} else if (isExport(child) && isTarget(child, name)) {
|
||||
} else if (isExport(child) && isTarget(child)) {
|
||||
targetIndex = index;
|
||||
}
|
||||
});
|
||||
|
@ -70,9 +68,7 @@ const getOrCreateExistingTargetIndex = (children: Node[], name: string) => {
|
|||
return targetIndex;
|
||||
};
|
||||
|
||||
export default function plugin(options: PluginOptions = {}): Transformer {
|
||||
const name = options.name || 'toc';
|
||||
|
||||
export default function plugin(): Transformer {
|
||||
return (root) => {
|
||||
const headings: TOCItem[] = [];
|
||||
|
||||
|
@ -91,7 +87,7 @@ export default function plugin(options: PluginOptions = {}): Transformer {
|
|||
});
|
||||
});
|
||||
const {children} = root as Parent<Literal>;
|
||||
const targetIndex = getOrCreateExistingTargetIndex(children, name);
|
||||
const targetIndex = getOrCreateExistingTargetIndex(children);
|
||||
|
||||
if (headings.length) {
|
||||
children[targetIndex]!.value = `export const ${name} = ${stringifyObject(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue