refactor: unify export directive style (#6751)

This commit is contained in:
Joshua Chen 2022-02-24 17:25:17 +08:00 committed by GitHub
parent 0c807b3501
commit 0d14470d54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
105 changed files with 315 additions and 510 deletions

View file

@ -23,7 +23,8 @@ declare module '@mdx-js/mdx' {
function createMdxAstCompiler(options?: Options): Processor;
function createCompiler(options?: Options): Processor;
}
function mdx(content: string, options?: mdx.Options): Promise<string>;
export default mdx;
export default function mdx(
content: string,
options?: mdx.Options,
): Promise<string>;
}

View file

@ -8,17 +8,16 @@
/* Based on remark-slug (https://github.com/remarkjs/remark-slug) and gatsby-remark-autolink-headers (https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-remark-autolink-headers) */
import {parseMarkdownHeadingId, createSlugger} from '@docusaurus/utils';
import visit, {type Visitor} from 'unist-util-visit';
import visit from 'unist-util-visit';
import toString from 'mdast-util-to-string';
import type {Transformer} from 'unified';
import type {Parent} from 'unist';
import type {Heading, Text} from 'mdast';
function headings(): Transformer {
const transformer: Transformer = (ast) => {
export default function plugin(): Transformer {
return (root) => {
const slugs = createSlugger();
const visitor: Visitor<Heading> = (headingNode) => {
visit(root, 'heading', (headingNode: Heading) => {
const data = headingNode.data || (headingNode.data = {});
const properties = (data.hProperties || (data.hProperties = {})) as {
id: string;
@ -69,12 +68,6 @@ function headings(): Transformer {
data.id = id;
properties.id = id;
};
visit(ast, 'heading', visitor);
});
};
return transformer;
}
export default headings;

View file

@ -16,7 +16,7 @@ import {toValue} from '../utils';
import type {TOCItem} from '@docusaurus/types';
import type {Node, Parent} from 'unist';
import type {Heading, Literal} from 'mdast';
import type {Plugin, Transformer} from 'unified';
import type {Transformer} from 'unified';
const parseOptions: ParserOptions = {
plugins: ['jsx'],
@ -70,17 +70,17 @@ const getOrCreateExistingTargetIndex = (children: Node[], name: string) => {
return targetIndex;
};
const plugin: Plugin<[PluginOptions?]> = (options = {}) => {
export default function plugin(options: PluginOptions = {}): Transformer {
const name = options.name || 'toc';
const transformer: Transformer = (node) => {
return (root) => {
const headings: TOCItem[] = [];
visit(node, 'heading', (child: Heading, _index, parent) => {
visit(root, 'heading', (child: Heading, _index, parent) => {
const value = toString(child);
// depth:1 headings are titles and not included in the TOC
if (parent !== node || !value || child.depth < 2) {
if (parent !== root || !value || child.depth < 2) {
return;
}
@ -90,7 +90,7 @@ const plugin: Plugin<[PluginOptions?]> = (options = {}) => {
level: child.depth,
});
});
const {children} = node as Parent<Literal>;
const {children} = root as Parent<Literal>;
const targetIndex = getOrCreateExistingTargetIndex(children, name);
if (headings && headings.length) {
@ -99,8 +99,4 @@ const plugin: Plugin<[PluginOptions?]> = (options = {}) => {
)};`;
}
};
return transformer;
};
export default plugin;
}

View file

@ -19,7 +19,7 @@ import fs from 'fs-extra';
import escapeHtml from 'escape-html';
import sizeOf from 'image-size';
import {promisify} from 'util';
import type {Plugin, Transformer} from 'unified';
import type {Transformer} from 'unified';
import type {Image, Literal} from 'mdast';
import logger from '@docusaurus/logger';
@ -147,8 +147,8 @@ async function processImageNode(node: Image, context: Context) {
await toImageRequireNode(node, imagePath, context.filePath);
}
const plugin: Plugin<[PluginOptions]> = (options) => {
const transformer: Transformer = async (root, vfile) => {
export default function plugin(options: PluginOptions): Transformer {
return async (root, vfile) => {
const promises: Promise<void>[] = [];
visit(root, 'image', (node: Image) => {
promises.push(
@ -157,7 +157,4 @@ const plugin: Plugin<[PluginOptions]> = (options) => {
});
await Promise.all(promises);
};
return transformer;
};
export default plugin;
}

View file

@ -18,7 +18,7 @@ import url from 'url';
import fs from 'fs-extra';
import escapeHtml from 'escape-html';
import {stringifyContent} from '../utils';
import type {Plugin, Transformer} from 'unified';
import type {Transformer} from 'unified';
import type {Link, Literal} from 'mdast';
const {
@ -136,15 +136,12 @@ async function processLinkNode(node: Link, context: Context) {
}
}
const plugin: Plugin<[PluginOptions]> = (options) => {
const transformer: Transformer = async (root, vfile) => {
export default function plugin(options: PluginOptions): Transformer {
return async (root, vfile) => {
const promises: Promise<void>[] = [];
visit(root, 'link', (node: Link) => {
promises.push(processLinkNode(node, {...options, filePath: vfile.path!}));
});
await Promise.all(promises);
};
return transformer;
};
export default plugin;
}

View file

@ -15,11 +15,11 @@ import type {Code, Parent} from 'mdast';
// with the markup, but the JSX inside such code blocks should still be
// evaluated as JSX
// See https://github.com/facebook/docusaurus/pull/4278
function plugin(this: Processor): Transformer {
const transformer: Transformer = (root) => {
export default function plugin(this: Processor): Transformer {
return (root) => {
visit(root, 'code', (node: Code, _index, parent) => {
if (node.lang === 'mdx-code-block') {
const newChildren = (this!.parse(node.value) as Parent).children;
const newChildren = (this.parse(node.value) as Parent).children;
// Replace the mdx code block by its content, parsed
parent!.children.splice(
@ -30,8 +30,4 @@ function plugin(this: Processor): Transformer {
}
});
};
return transformer;
}
export default plugin;