mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-04 03:42:34 +02:00
chore: clean up ESLint config, enable a few rules (#6514)
* chore: clean up ESLint config, enable a few rules * enable max-len for comments * fix build
This commit is contained in:
parent
b8ccb869f1
commit
aa446b7a9c
167 changed files with 1157 additions and 960 deletions
|
@ -49,9 +49,12 @@ type Options = RemarkAndRehypePluginOptions & {
|
|||
filepath: string;
|
||||
};
|
||||
|
||||
// When this throws, it generally means that there's no metadata file associated with this MDX document
|
||||
// It can happen when using MDX partials (usually starting with _)
|
||||
// That's why it's important to provide the "isMDXPartial" function in config
|
||||
/**
|
||||
* When this throws, it generally means that there's no metadata file associated
|
||||
* with this MDX document. It can happen when using MDX partials (usually
|
||||
* starting with _). That's why it's important to provide the `isMDXPartial`
|
||||
* function in config
|
||||
*/
|
||||
async function readMetadataPath(metadataPath: string) {
|
||||
try {
|
||||
return await readFile(metadataPath, 'utf8');
|
||||
|
@ -62,11 +65,14 @@ async function readMetadataPath(metadataPath: string) {
|
|||
}
|
||||
}
|
||||
|
||||
// Converts assets an object with Webpack require calls code
|
||||
// This is useful for mdx files to reference co-located assets using relative paths
|
||||
// Those assets should enter the Webpack assets pipeline and be hashed
|
||||
// For now, we only handle that for images and paths starting with ./
|
||||
// {image: "./myImage.png"} => {image: require("./myImage.png")}
|
||||
/**
|
||||
* Converts assets an object with Webpack require calls code.
|
||||
* This is useful for mdx files to reference co-located assets using relative
|
||||
* paths. Those assets should enter the Webpack assets pipeline and be hashed.
|
||||
* For now, we only handle that for images and paths starting with `./`:
|
||||
*
|
||||
* `{image: "./myImage.png"}` => `{image: require("./myImage.png")}`
|
||||
*/
|
||||
function createAssetsExportCode(assets: Record<string, unknown>) {
|
||||
if (Object.keys(assets).length === 0) {
|
||||
return 'undefined';
|
||||
|
@ -148,7 +154,7 @@ export default async function mdxLoader(
|
|||
filepath: filePath,
|
||||
};
|
||||
|
||||
let result;
|
||||
let result: string;
|
||||
try {
|
||||
result = await mdx(content, options);
|
||||
} catch (err) {
|
||||
|
@ -156,7 +162,7 @@ export default async function mdxLoader(
|
|||
}
|
||||
|
||||
// MDX partials are MDX files starting with _ or in a folder starting with _
|
||||
// Partial are not expected to have an associated metadata file or front matter
|
||||
// Partial are not expected to have associated metadata files or front matter
|
||||
const isMDXPartial = options.isMDXPartial && options.isMDXPartial(filePath);
|
||||
if (isMDXPartial && hasFrontMatter) {
|
||||
const errorMessage = `Docusaurus MDX partial files should not contain FrontMatter.
|
||||
|
@ -168,9 +174,8 @@ ${JSON.stringify(frontMatter, null, 2)}`;
|
|||
const shouldError = process.env.NODE_ENV === 'test' || process.env.CI;
|
||||
if (shouldError) {
|
||||
return callback(new Error(errorMessage));
|
||||
} else {
|
||||
logger.warn(errorMessage);
|
||||
}
|
||||
logger.warn(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -57,7 +57,7 @@ describe('headings plugin', () => {
|
|||
|
||||
test('should not overwrite `data` on headings', () => {
|
||||
const result = process('# Normal\n', [
|
||||
function () {
|
||||
() => {
|
||||
function transform(tree) {
|
||||
tree.children[0].data = {foo: 'bar'};
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ describe('headings plugin', () => {
|
|||
|
||||
test('should not overwrite `data.hProperties` on headings', () => {
|
||||
const result = process('# Normal\n', [
|
||||
function () {
|
||||
() => {
|
||||
function transform(tree) {
|
||||
tree.children[0].data = {hProperties: {className: ['foo']}};
|
||||
}
|
||||
|
@ -110,7 +110,7 @@ describe('headings plugin', () => {
|
|||
'## Something also',
|
||||
].join('\n\n'),
|
||||
[
|
||||
function () {
|
||||
() => {
|
||||
function transform(tree) {
|
||||
tree.children[1].data = {hProperties: {id: 'here'}};
|
||||
tree.children[3].data = {hProperties: {id: 'something'}};
|
||||
|
|
|
@ -44,7 +44,8 @@ function headings(): Transformer {
|
|||
|
||||
if (parsedHeading.id) {
|
||||
// When there's an id, it is always in the last child node
|
||||
// Sometimes heading is in multiple "parts" (** syntax creates a child node):
|
||||
// Sometimes heading is in multiple "parts" (** syntax creates a child
|
||||
// node):
|
||||
// ## part1 *part2* part3 {#id}
|
||||
const lastNode = headingNode.children[
|
||||
headingNode.children.length - 1
|
||||
|
|
|
@ -46,9 +46,9 @@ export default function search(node: Node): TOCItem[] {
|
|||
});
|
||||
});
|
||||
|
||||
// Keep track of which previous index would be the current heading's direct parent.
|
||||
// Each entry <i> is the last index of the `headings` array at heading level <i>.
|
||||
// We will modify these indices as we iterate through all headings.
|
||||
// Keep track of which previous index would be the current heading's direct
|
||||
// parent. Each entry <i> is the last index of the `headings` array at heading
|
||||
// level <i>. We will modify these indices as we iterate through all headings.
|
||||
// e.g. if an ### H3 was last seen at index 2, then prevIndexForLevel[3] === 2
|
||||
// indices 0 and 1 will remain unused.
|
||||
const prevIndexForLevel = Array(7).fill(-1);
|
||||
|
|
|
@ -116,14 +116,10 @@ async function getImageAbsolutePath(
|
|||
}
|
||||
return imageFilePath;
|
||||
}
|
||||
// We try to convert image urls without protocol to images with require calls
|
||||
// going through webpack ensures that image assets exist at build time
|
||||
else {
|
||||
// relative paths are resolved against the source file's folder
|
||||
const imageFilePath = path.join(path.dirname(filePath), imagePath);
|
||||
await ensureImageFileExist(imageFilePath, filePath);
|
||||
return imageFilePath;
|
||||
}
|
||||
// relative paths are resolved against the source file's folder
|
||||
const imageFilePath = path.join(path.dirname(filePath), imagePath);
|
||||
await ensureImageFileExist(imageFilePath, filePath);
|
||||
return imageFilePath;
|
||||
}
|
||||
|
||||
async function processImageNode(node: Image, context: Context) {
|
||||
|
@ -137,16 +133,16 @@ async function processImageNode(node: Image, context: Context) {
|
|||
|
||||
const parsedUrl = url.parse(node.url);
|
||||
if (parsedUrl.protocol || !parsedUrl.pathname) {
|
||||
// pathname:// is an escape hatch,
|
||||
// in case user does not want his images to be converted to require calls going through webpack loader
|
||||
// we don't have to document this for now,
|
||||
// it's mostly to make next release less risky (2.0.0-alpha.59)
|
||||
// pathname:// is an escape hatch, in case user does not want her images to
|
||||
// be converted to require calls going through webpack loader
|
||||
if (parsedUrl.protocol === 'pathname:') {
|
||||
node.url = node.url.replace('pathname://', '');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// We try to convert image urls without protocol to images with require calls
|
||||
// going through webpack ensures that image assets exist at build time
|
||||
const imagePath = await getImageAbsolutePath(parsedUrl.pathname, context);
|
||||
await toImageRequireNode(node, imagePath, context.filePath);
|
||||
}
|
||||
|
|
|
@ -10,9 +10,10 @@ import type {Transformer, Processor} from 'unified';
|
|||
import type {Code, Parent} from 'mdast';
|
||||
|
||||
// This plugin is mostly to help integrating Docusaurus with translation systems
|
||||
// that do not support well MDX embedded JSX syntax (like Crowdin)
|
||||
// We wrap the JSX syntax in code blocks so that translation tools don't mess-up with the markup
|
||||
// But the JSX inside such code blocks should still be evaluated as JSX
|
||||
// that do not support well MDX embedded JSX syntax (like Crowdin).
|
||||
// We wrap the JSX syntax in code blocks so that translation tools don't mess up
|
||||
// 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) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue