mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-12 20:09:46 +02:00
use single quotes; use const instead of let (2); use const instead of function
This commit is contained in:

parent
6554a41f6d
commit
0832b24d66
78
src/docs.mts
78
src/docs.mts
@@ -14,30 +14,29 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
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';
|
||||||
import { JSDOM } from "jsdom";
|
import { JSDOM } from 'jsdom';
|
||||||
|
|
||||||
// @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';
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import prettier from "prettier";
|
import prettier from 'prettier';
|
||||||
|
|
||||||
const SOURCE_DOCS_DIR = 'src/docs/';
|
const SOURCE_DOCS_DIR = 'src/docs/';
|
||||||
const FINAL_DOCS_DIR = 'docs/';
|
const FINAL_DOCS_DIR = 'docs/';
|
||||||
const AUTOGENERATED_TEXT =
|
const AUTOGENERATED_TEXT =
|
||||||
"# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT. Please edit the corresponding file in src/docs.";
|
'# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT. Please edit the corresponding file in src/docs.';
|
||||||
|
|
||||||
const verifyOnly = process.argv.includes("--verify");
|
const verifyOnly = process.argv.includes('--verify');
|
||||||
const git = process.argv.includes("--git");
|
const git = process.argv.includes('--git');
|
||||||
|
|
||||||
let filesWereChanged = false;
|
let filesWereChanged = false;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given a source file name and path, return the documentation destination full path and file name
|
* Given a source file name and path, return the documentation destination full path and file name
|
||||||
* Create the destination path if it does not already exist.
|
* Create the destination path if it does not already exist.
|
||||||
@@ -47,7 +46,7 @@ let filesWereChanged = false;
|
|||||||
* @returns {string} name of the file with the path changed from src/docs to docs
|
* @returns {string} name of the file with the path changed from src/docs to docs
|
||||||
*/
|
*/
|
||||||
const prepareOutFile = (file: string): string => {
|
const prepareOutFile = (file: string): string => {
|
||||||
const outFile = join(FINAL_DOCS_DIR, file.replace(SOURCE_DOCS_DIR, ""));
|
const outFile = join(FINAL_DOCS_DIR, file.replace(SOURCE_DOCS_DIR, ''));
|
||||||
mkdirSync(dirname(outFile), { recursive: true });
|
mkdirSync(dirname(outFile), { recursive: true });
|
||||||
return outFile;
|
return outFile;
|
||||||
};
|
};
|
||||||
@@ -61,7 +60,7 @@ const prepareOutFile = (file: string): string => {
|
|||||||
*/
|
*/
|
||||||
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.
|
||||||
@@ -71,19 +70,19 @@ const verifyAndCopy = (file: string, content?: string) => {
|
|||||||
if (verifyOnly) {
|
if (verifyOnly) {
|
||||||
changeMsg = 'to be changed';
|
changeMsg = 'to be changed';
|
||||||
}
|
}
|
||||||
let logMsg = ` File ${changeMsg}: ${outFile}`
|
let logMsg = ` File ${changeMsg}: ${outFile}`;
|
||||||
|
|
||||||
filesWereChanged = true;
|
filesWereChanged = true;
|
||||||
if (!verifyOnly) {
|
if (!verifyOnly) {
|
||||||
writeFileSync(outFile, newBuffer);
|
writeFileSync(outFile, newBuffer);
|
||||||
logMsg += ' ...and copied to /docs'
|
logMsg += ' ...and copied to /docs';
|
||||||
}
|
}
|
||||||
console.log(logMsg);
|
console.log(logMsg);
|
||||||
};
|
};
|
||||||
|
|
||||||
const readSyncedUTF8file = (file: string): string => {
|
const readSyncedUTF8file = (file: string): string => {
|
||||||
return readFileSync(file, "utf8");
|
return readFileSync(file, 'utf8');
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transform a markdown file and write the transformed file to the directory for published documentation
|
* Transform a markdown file and write the transformed file to the directory for published documentation
|
||||||
@@ -99,13 +98,13 @@ const transformMarkdown = (file: string) => {
|
|||||||
const doc = readSyncedUTF8file(file);
|
const doc = readSyncedUTF8file(file);
|
||||||
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' })];
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add the AUTOGENERATED_TEXT to the start of the file
|
// Add the AUTOGENERATED_TEXT to the start of the file
|
||||||
@@ -114,12 +113,12 @@ const transformMarkdown = (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,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -132,39 +131,38 @@ const transformMarkdown = (file: string) => {
|
|||||||
* @param filename {string} name of the HTML file to transform
|
* @param filename {string} name of the HTML file to transform
|
||||||
*/
|
*/
|
||||||
const transformHtml = (filename: string) => {
|
const transformHtml = (filename: string) => {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert the '...auto generated...' comment into an HTML file after the <html> element
|
* Insert the '...auto generated...' comment into an HTML file after the <html> element
|
||||||
*
|
*
|
||||||
* @param fname {string} file name that should have the comment inserted
|
* @param fileName {string} file name that should have the comment inserted
|
||||||
* @returns {string} the contents of the file with the comment inserted
|
* @returns {string} the contents of the file with the comment inserted
|
||||||
*/
|
*/
|
||||||
function insertAutoGeneratedComment(fname: string): string {
|
const insertAutoGeneratedComment = (fileName: string): string => {
|
||||||
const fileContents = readSyncedUTF8file(fname);
|
const fileContents = readSyncedUTF8file(fileName);
|
||||||
const jsdom = new JSDOM(fileContents);
|
const jsdom = new JSDOM(fileContents);
|
||||||
const htmlDoc = jsdom.window.document;
|
const htmlDoc = jsdom.window.document;
|
||||||
const autoGeneratedComment = jsdom.window.document.createComment(AUTOGENERATED_TEXT);
|
const autoGeneratedComment = jsdom.window.document.createComment(AUTOGENERATED_TEXT);
|
||||||
|
|
||||||
let rootElement = htmlDoc.documentElement;
|
const rootElement = htmlDoc.documentElement;
|
||||||
rootElement.prepend(autoGeneratedComment);
|
rootElement.prepend(autoGeneratedComment);
|
||||||
return jsdom.serialize();
|
return jsdom.serialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
let transformedHTML = insertAutoGeneratedComment(filename);
|
const transformedHTML = insertAutoGeneratedComment(filename);
|
||||||
verifyAndCopy(filename, transformedHTML);
|
verifyAndCopy(filename, transformedHTML);
|
||||||
};
|
};
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
console.log("Transforming markdown files...");
|
console.log('Transforming markdown files...');
|
||||||
const mdFiles = await globby(["./src/docs/**/*.md"], { dot: true });
|
const mdFiles = await globby(['./src/docs/**/*.md'], { dot: true });
|
||||||
mdFiles.forEach(transformMarkdown);
|
mdFiles.forEach(transformMarkdown);
|
||||||
|
|
||||||
console.log("Transforming html files...");
|
console.log('Transforming html files...');
|
||||||
const htmlFiles = await globby(["./src/docs/**/*.html"], { dot: true });
|
const htmlFiles = await globby(['./src/docs/**/*.html'], { dot: true });
|
||||||
htmlFiles.forEach(transformHtml);
|
htmlFiles.forEach(transformHtml);
|
||||||
|
|
||||||
console.log("Transforming all other files...");
|
console.log('Transforming all other files...');
|
||||||
const otherFiles = await globby(["src/docs/**", "!**/*.md", "!**/*.html"], { dot: true });
|
const otherFiles = await globby(['src/docs/**', '!**/*.md', '!**/*.html'], { dot: true });
|
||||||
otherFiles.forEach((file) => {
|
otherFiles.forEach((file) => {
|
||||||
verifyAndCopy(file);
|
verifyAndCopy(file);
|
||||||
});
|
});
|
||||||
@@ -176,8 +174,8 @@ const transformHtml = (filename: 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');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
Reference in New Issue
Block a user