mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-28 07:03:17 +08:00
eslint (mostly use double quotes)
This commit is contained in:
parent
7e68e06a3a
commit
0c85e8ee53
48
src/docs.mts
48
src/docs.mts
@ -1,26 +1,26 @@
|
|||||||
import { remark } from 'remark';
|
import { remark } from "remark";
|
||||||
import type { Code, Root } from 'mdast';
|
import type { Code, Root } from "mdast";
|
||||||
import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'fs';
|
import { readFileSync, writeFileSync, mkdirSync, existsSync } from "fs";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import flatmap from 'unist-util-flatmap';
|
import flatmap from "unist-util-flatmap";
|
||||||
import { globby } from 'globby';
|
import { globby } from "globby";
|
||||||
import { join, dirname } from 'path';
|
import { join, dirname } from "path";
|
||||||
import { exec } from 'child_process';
|
import { exec } from "child_process";
|
||||||
import prettier from 'prettier';
|
import prettier from "prettier";
|
||||||
|
|
||||||
const verify = process.argv.includes('--verify');
|
const verify = process.argv.includes("--verify");
|
||||||
const git = process.argv.includes('--git');
|
const git = process.argv.includes("--git");
|
||||||
let fileChanged = false;
|
let fileChanged = false;
|
||||||
// Possible Improvement: combine with lint-staged to only copy files that have changed
|
// Possible Improvement: combine with lint-staged to only copy files that have changed
|
||||||
const prepareOutFile = (file: string): string => {
|
const prepareOutFile = (file: string): string => {
|
||||||
const outFile = join('docs', file.replace('src/docs/', ''));
|
const outFile = join("docs", file.replace("src/docs/", ""));
|
||||||
mkdirSync(dirname(outFile), { recursive: true });
|
mkdirSync(dirname(outFile), { recursive: true });
|
||||||
return outFile;
|
return outFile;
|
||||||
};
|
};
|
||||||
|
|
||||||
const verifyAndCopy = (file: string, content?: string) => {
|
const verifyAndCopy = (file: string, content?: string) => {
|
||||||
const outFile = prepareOutFile(file);
|
const outFile = prepareOutFile(file);
|
||||||
const existingBuffer = existsSync(outFile) ? readFileSync(outFile) : Buffer.from('#NEW FILE#');
|
const existingBuffer = existsSync(outFile) ? readFileSync(outFile) : Buffer.from("#NEW FILE#");
|
||||||
const newBuffer = content ? Buffer.from(content) : readFileSync(file);
|
const newBuffer = content ? Buffer.from(content) : readFileSync(file);
|
||||||
if (existingBuffer.equals(newBuffer)) {
|
if (existingBuffer.equals(newBuffer)) {
|
||||||
// Files are same, skip.
|
// Files are same, skip.
|
||||||
@ -34,16 +34,16 @@ const verifyAndCopy = (file: string, content?: string) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const transform = (file: string) => {
|
const transform = (file: string) => {
|
||||||
const doc = readFileSync(file, 'utf8');
|
const doc = readFileSync(file, "utf8");
|
||||||
const ast: Root = remark.parse(doc);
|
const ast: Root = remark.parse(doc);
|
||||||
const out = flatmap(ast, (c: Code) => {
|
const out = flatmap(ast, (c: Code) => {
|
||||||
if (c.type !== 'code' || !c.lang?.startsWith('mermaid')) {
|
if (c.type !== "code" || !c.lang?.startsWith("mermaid")) {
|
||||||
return [c];
|
return [c];
|
||||||
}
|
}
|
||||||
if (c.lang === 'mermaid' || c.lang === 'mmd') {
|
if (c.lang === "mermaid" || c.lang === "mmd") {
|
||||||
c.lang = 'mermaid-example';
|
c.lang = "mermaid-example";
|
||||||
}
|
}
|
||||||
return [c, Object.assign({}, c, { lang: 'mermaid' })];
|
return [c, Object.assign({}, c, { lang: "mermaid" })];
|
||||||
});
|
});
|
||||||
|
|
||||||
const transformed = `# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT. Please edit corresponding file in src/docs.\n${remark.stringify(
|
const transformed = `# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT. Please edit corresponding file in src/docs.\n${remark.stringify(
|
||||||
@ -52,20 +52,20 @@ const transform = (file: string) => {
|
|||||||
verifyAndCopy(
|
verifyAndCopy(
|
||||||
file,
|
file,
|
||||||
prettier.format(transformed, {
|
prettier.format(transformed, {
|
||||||
parser: 'markdown',
|
parser: "markdown",
|
||||||
useTabs: false,
|
useTabs: false,
|
||||||
tabWidth: 2,
|
tabWidth: 2,
|
||||||
endOfLine: 'auto',
|
endOfLine: "auto",
|
||||||
printWidth: 100,
|
printWidth: 100,
|
||||||
singleQuote: true,
|
singleQuote: true
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
const mdFiles = await globby(['./src/docs/**/*.md'], { dot: true });
|
const mdFiles = await globby(["./src/docs/**/*.md"], { dot: true });
|
||||||
mdFiles.forEach(transform);
|
mdFiles.forEach(transform);
|
||||||
const nonMDFiles = await globby(['src/docs/**', '!**/*.md'], { dot: true });
|
const nonMDFiles = await globby(["src/docs/**", "!**/*.md"], { dot: true });
|
||||||
nonMDFiles.forEach((file) => {
|
nonMDFiles.forEach((file) => {
|
||||||
verifyAndCopy(file);
|
verifyAndCopy(file);
|
||||||
});
|
});
|
||||||
@ -77,8 +77,8 @@ const transform = (file: string) => {
|
|||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
if (git) {
|
if (git) {
|
||||||
console.log('Adding changes in docs folder to git');
|
console.log("Adding changes in docs folder to git");
|
||||||
exec('git add docs');
|
exec("git add docs");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user