mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-10 14:52:29 +02:00
chore(v2): fix several lint warnings, add missing types, cleanup (#3844)
* fix several lint warnings, add missing types, cleanup * fix EnumChangefreq issue * better utilization of EnumChangefreq type * update test snapshot
This commit is contained in:
parent
139b668737
commit
ad31facb32
49 changed files with 228 additions and 171 deletions
|
@ -12,10 +12,10 @@ import glob from 'glob';
|
|||
import Color from 'color';
|
||||
|
||||
import {
|
||||
VersionOneConfig,
|
||||
VersionTwoConfig,
|
||||
ClassicPresetEntries,
|
||||
SidebarEntries,
|
||||
VersionOneConfig,
|
||||
VersionTwoConfig,
|
||||
} from './types';
|
||||
import extractMetadata, {shouldQuotifyFrontMatter} from './frontMatter';
|
||||
import migratePage from './transform';
|
||||
|
@ -229,9 +229,9 @@ export function createConfigFile({
|
|||
const homePageId = siteConfig.headerLinks?.filter((value) => value.doc)[0]
|
||||
.doc;
|
||||
|
||||
const customConfigFields: Record<string, any> = {};
|
||||
const customConfigFields: Record<string, unknown> = {};
|
||||
// add fields that are unknown to v2 to customConfigFields
|
||||
Object.keys(siteConfig).forEach((key: any) => {
|
||||
Object.keys(siteConfig).forEach((key) => {
|
||||
const knownFields = [
|
||||
'title',
|
||||
'tagline',
|
||||
|
@ -289,7 +289,7 @@ export function createConfigFile({
|
|||
v2DocsPath = path.relative(newDir, absoluteDocsPath);
|
||||
}
|
||||
|
||||
const result: VersionTwoConfig = {
|
||||
return {
|
||||
title: siteConfig.title ?? '',
|
||||
tagline: siteConfig.tagline,
|
||||
url: siteConfig.url ?? '',
|
||||
|
@ -330,22 +330,24 @@ export function createConfigFile({
|
|||
: undefined,
|
||||
items: (siteConfig.headerLinks ?? [])
|
||||
.map((link) => {
|
||||
if (link.doc) {
|
||||
const {doc, href, label, page} = link;
|
||||
const position = 'left';
|
||||
if (doc) {
|
||||
return {
|
||||
to: `docs/${link.doc === homePageId ? '' : link.doc}`,
|
||||
label: link.label,
|
||||
position: 'left',
|
||||
to: `docs/${doc === homePageId ? '' : doc}`,
|
||||
label,
|
||||
position,
|
||||
};
|
||||
}
|
||||
if (link.page) {
|
||||
if (page) {
|
||||
return {
|
||||
to: `/${link.page}`,
|
||||
label: link.label,
|
||||
position: 'left',
|
||||
to: `/${page}`,
|
||||
label,
|
||||
position,
|
||||
};
|
||||
}
|
||||
if (link.href) {
|
||||
return {href: link.href, label: link.label, position: 'left'};
|
||||
if (href) {
|
||||
return {href, label, position};
|
||||
}
|
||||
return null;
|
||||
})
|
||||
|
@ -379,7 +381,6 @@ export function createConfigFile({
|
|||
: undefined,
|
||||
},
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
function createClientRedirects(
|
||||
|
@ -476,7 +477,7 @@ function handleVersioning(
|
|||
const loadedVersions: Array<string> = JSON.parse(
|
||||
String(fs.readFileSync(path.join(siteDir, 'versions.json'))),
|
||||
);
|
||||
fs.copyFile(
|
||||
fs.copyFileSync(
|
||||
path.join(siteDir, 'versions.json'),
|
||||
path.join(newDir, 'versions.json'),
|
||||
);
|
||||
|
@ -732,11 +733,10 @@ function migrateLatestDocs(
|
|||
classicPreset: ClassicPresetEntries,
|
||||
): void {
|
||||
if (fs.existsSync(path.join(siteDir, '..', 'docs'))) {
|
||||
const docsPath = path.join(
|
||||
classicPreset.docs.path = path.join(
|
||||
path.relative(newDir, path.join(siteDir, '..')),
|
||||
'docs',
|
||||
);
|
||||
classicPreset.docs.path = docsPath;
|
||||
const files = walk(path.join(siteDir, '..', 'docs'));
|
||||
files.forEach((file) => {
|
||||
const content = String(fs.readFileSync(file));
|
||||
|
@ -797,5 +797,5 @@ export async function migrateMDToMDX(
|
|||
sanitizedFileContent(String(fs.readFileSync(file)), true),
|
||||
);
|
||||
});
|
||||
console.log(`Succesfully migrated ${siteDir} to ${newDir}`);
|
||||
console.log(`Successfully migrated ${siteDir} to ${newDir}`);
|
||||
}
|
||||
|
|
|
@ -20,9 +20,7 @@ const tags = htmlTags.reduce((acc: {[key: string]: boolean}, tag) => {
|
|||
}, {});
|
||||
|
||||
export default function sanitizeMD(code: string): string {
|
||||
const markdownTree = unified()
|
||||
.use(markdown as any)
|
||||
.parse(code);
|
||||
const markdownTree = unified().use(markdown).parse(code);
|
||||
visit(markdownTree, 'code', (node) => {
|
||||
node.value = `\n<!--${node.value}-->\n`;
|
||||
});
|
||||
|
@ -31,12 +29,10 @@ export default function sanitizeMD(code: string): string {
|
|||
});
|
||||
|
||||
const markdownString = unified()
|
||||
.use(remarkStringify as any, {fence: '`', fences: true})
|
||||
.use(remarkStringify, {fence: '`', fences: true})
|
||||
.stringify(markdownTree);
|
||||
|
||||
const htmlTree = unified()
|
||||
.use(parse as any)
|
||||
.parse(markdownString);
|
||||
const htmlTree = unified().use(parse).parse(markdownString);
|
||||
visit(htmlTree, 'element', (node) => {
|
||||
if (!tags[node.tagName as string]) {
|
||||
node.type = 'text';
|
||||
|
|
|
@ -5,7 +5,14 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import jscodeshift from 'jscodeshift';
|
||||
import jscodeshift, {
|
||||
ArrowFunctionExpression,
|
||||
AssignmentExpression,
|
||||
ASTPath,
|
||||
Collection,
|
||||
TemplateElement,
|
||||
VariableDeclarator,
|
||||
} from 'jscodeshift';
|
||||
|
||||
const empty = () =>
|
||||
jscodeshift.arrowFunctionExpression(
|
||||
|
@ -18,16 +25,14 @@ const empty = () =>
|
|||
),
|
||||
);
|
||||
|
||||
const property = (key: string, value: jscodeshift.ArrowFunctionExpression) =>
|
||||
const property = (key: string, value: ArrowFunctionExpression) =>
|
||||
jscodeshift.objectProperty(jscodeshift.identifier(key), value);
|
||||
|
||||
const processCallExpression = (
|
||||
node: jscodeshift.ASTPath<jscodeshift.VariableDeclarator>,
|
||||
) => {
|
||||
const processCallExpression = (node: ASTPath<VariableDeclarator>) => {
|
||||
const args = (node?.value?.init as any)?.arguments[0];
|
||||
if (args.type === 'Literal') {
|
||||
if (args.value.includes('../../core/CompLibrary')) {
|
||||
const newDeclartor = jscodeshift.variableDeclarator(
|
||||
const newDeclarator = jscodeshift.variableDeclarator(
|
||||
node.value.id,
|
||||
jscodeshift.objectExpression([
|
||||
property('Container', empty()),
|
||||
|
@ -35,32 +40,30 @@ const processCallExpression = (
|
|||
property('MarkdownBlock', empty()),
|
||||
]),
|
||||
);
|
||||
jscodeshift(node).replaceWith(newDeclartor);
|
||||
jscodeshift(node).replaceWith(newDeclarator);
|
||||
}
|
||||
}
|
||||
if (args.type === 'TemplateLiteral') {
|
||||
if (
|
||||
args.quasis
|
||||
.map((element: jscodeshift.TemplateElement) => element.value.raw)
|
||||
.map((element: TemplateElement) => element.value.raw)
|
||||
.join('')
|
||||
.match(/\/core\//)
|
||||
) {
|
||||
const newDeclartor = jscodeshift.variableDeclarator(
|
||||
const newDeclarator = jscodeshift.variableDeclarator(
|
||||
node.value.id,
|
||||
empty(),
|
||||
);
|
||||
jscodeshift(node).replaceWith(newDeclartor);
|
||||
jscodeshift(node).replaceWith(newDeclarator);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const processMemberExpression = (
|
||||
node: jscodeshift.ASTPath<jscodeshift.VariableDeclarator>,
|
||||
) => {
|
||||
const processMemberExpression = (node: ASTPath<VariableDeclarator>) => {
|
||||
const args = (node?.value?.init as any)?.object?.arguments[0];
|
||||
if (args.type === 'Literal') {
|
||||
if (args.value === '../../core/CompLibrary.js') {
|
||||
const newDeclartor = jscodeshift.variableDeclarator(
|
||||
const newDeclarator = jscodeshift.variableDeclarator(
|
||||
node.value.id,
|
||||
jscodeshift.objectExpression([
|
||||
property('Container', empty()),
|
||||
|
@ -68,27 +71,27 @@ const processMemberExpression = (
|
|||
property('MarkdownBlock', empty()),
|
||||
]),
|
||||
);
|
||||
jscodeshift(node).replaceWith(newDeclartor);
|
||||
jscodeshift(node).replaceWith(newDeclarator);
|
||||
} else if (args.value.match(/server/)) {
|
||||
const newDeclartor = jscodeshift.variableDeclarator(
|
||||
const newDeclarator = jscodeshift.variableDeclarator(
|
||||
node.value.id,
|
||||
empty(),
|
||||
);
|
||||
jscodeshift(node).replaceWith(newDeclartor);
|
||||
jscodeshift(node).replaceWith(newDeclarator);
|
||||
}
|
||||
}
|
||||
if (args.type === 'TemplateLiteral') {
|
||||
if (
|
||||
args.quasis
|
||||
.map((ele: jscodeshift.TemplateElement) => ele.value.raw)
|
||||
.map((ele: TemplateElement) => ele.value.raw)
|
||||
.join('')
|
||||
.match(/\/core\//)
|
||||
) {
|
||||
const newDeclartor = jscodeshift.variableDeclarator(
|
||||
const newDeclarator = jscodeshift.variableDeclarator(
|
||||
node.value.id,
|
||||
empty(),
|
||||
);
|
||||
jscodeshift(node).replaceWith(newDeclartor);
|
||||
jscodeshift(node).replaceWith(newDeclarator);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -113,7 +116,7 @@ export default function transformer(file: string): string {
|
|||
}
|
||||
|
||||
root
|
||||
.find(jscodeshift.AssignmentExpression, {
|
||||
.find(AssignmentExpression, {
|
||||
operator: '=',
|
||||
left: {
|
||||
type: 'MemberExpression',
|
||||
|
@ -164,10 +167,10 @@ export default function transformer(file: string): string {
|
|||
return root.toSource();
|
||||
}
|
||||
|
||||
function getDefaultImportDeclarators(rootAst: jscodeshift.Collection) {
|
||||
function getDefaultImportDeclarators(rootAst: Collection) {
|
||||
// var ... = require('y')
|
||||
return rootAst
|
||||
.find(jscodeshift.VariableDeclarator, {
|
||||
.find(VariableDeclarator, {
|
||||
init: {
|
||||
callee: {
|
||||
name: 'require',
|
||||
|
@ -179,9 +182,9 @@ function getDefaultImportDeclarators(rootAst: jscodeshift.Collection) {
|
|||
});
|
||||
}
|
||||
|
||||
function getNamedImportDeclarators(rootAst: jscodeshift.Collection) {
|
||||
function getNamedImportDeclarators(rootAst: Collection) {
|
||||
// var ... = require('y').x
|
||||
return rootAst.find(jscodeshift.VariableDeclarator, {
|
||||
return rootAst.find(VariableDeclarator, {
|
||||
init: {
|
||||
object: {
|
||||
callee: {
|
||||
|
@ -192,7 +195,7 @@ function getNamedImportDeclarators(rootAst: jscodeshift.Collection) {
|
|||
});
|
||||
}
|
||||
|
||||
function getImportDeclaratorPaths(variableDeclaration: jscodeshift.Collection) {
|
||||
function getImportDeclaratorPaths(variableDeclaration: Collection) {
|
||||
const defaultImports = getDefaultImportDeclarators(variableDeclaration);
|
||||
|
||||
const namedImports = getNamedImportDeclarators(variableDeclaration);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue