Skip to content

Commit

Permalink
chore: format script code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 28, 2022
1 parent bd08290 commit 4132a13
Show file tree
Hide file tree
Showing 24 changed files with 471 additions and 298 deletions.
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx --no-install lint-staged
3 changes: 3 additions & 0 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"**/*.{mjs,css,json,prettierrc,lintstagedrc}": ["prettier --write"]
}
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package.json
coverage
dist
build
docs
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"singleQuote": true,
"trailingComma": "all",
"printWidth": 120,
"overrides": [
{ "files": ".prettierrc", "options": { "parser": "json" } },
{ "files": ".lintstagedrc", "options": { "parser": "json" } }
]
}
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
"homepage": "https://jaywcjlove.github.io/reference",
"private": false,
"scripts": {
"prepare": "husky install",
"build": "node scripts/build.mjs",
"start": "node scripts/watch.mjs"
"start": "node scripts/watch.mjs",
"prettier": "prettier --write '**/*.{mjs,css,json,prettierrc,lintstagedrc}'"
},
"repository": {
"type": "git",
Expand All @@ -22,10 +24,16 @@
"@wcj/markdown-to-html": "^2.1.1",
"chokidar": "^3.5.3",
"fs-extra": "^10.1.0",
"husky": "^8.0.1",
"lint-staged": "^13.0.3",
"prettier": "^2.7.1",
"recursive-readdir-files": "^2.3.0",
"rehype-autolink-headings": "^6.1.1",
"rehype-document": "^6.1.0",
"rehype-slug": "^5.0.1",
"remark-gemoji": "^7.0.1"
},
"engines": {
"node": ">=16.0.0"
}
}
2 changes: 1 addition & 1 deletion scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { run } from './index.mjs';

run()
run();
43 changes: 22 additions & 21 deletions scripts/create.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import markdown from '@wcj/markdown-to-html';
import rehypeDocument from 'rehype-document';
import remarkGemoji from 'remark-gemoji';
import rehypeRaw from 'rehype-raw';
import rehypeAttrs from 'rehype-attr';
import rehypeKatex from 'rehype-katex';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
import rehypeSlug from 'rehype-slug';
import { htmlTagAddAttri } from './nodes/htmlTagAddAttri.mjs';
Expand All @@ -23,37 +21,41 @@ export function create(str = '', options = {}) {
let title = str.match(/[^===]+(?=[===])/g) || [];
let description = str.match(/\n==={1,}\n+([\s\S]*?)\n/g) || [];
title = title[0] || '';
description = (description[0] || '').replace(/^\n[=\n]+/, '').replace(/\[([\s\S]*?)?\]\(([\s\S]*?)?\)/g, '$1').replace(/\n/, '');
const subTitle = options.filename && !options.isHome ? `${options.filename} cheatsheet & `: ''
description = (description[0] || '')
.replace(/^\n[=\n]+/, '')
.replace(/\[([\s\S]*?)?\]\(([\s\S]*?)?\)/g, '$1')
.replace(/\n/, '');
const subTitle = options.filename && !options.isHome ? `${options.filename} cheatsheet & ` : '';
const mdOptions = {
showLineNumbers: false,
hastNode: false,
remarkPlugins: [remarkGemoji],
rehypePlugins: [
rehypeSlug,
rehypeAutolinkHeadings,
[rehypeDocument, {
[
rehypeDocument,
{
title: `${title ? `${title} & ` : ''} ${subTitle} Quick Reference`,
css: [ ...options.css ],
link: [
{rel: 'icon', href: favicon, type: 'image/svg+xml'}
],
css: [...options.css],
link: [{ rel: 'icon', href: favicon, type: 'image/svg+xml' }],
meta: [
{ description: `${description}为开发人员分享快速参考备忘单。` },
{ keywords: `Quick,Reference,cheatsheet,${!options.isHome && options.filename || ''}` }
]
}]
{ keywords: `Quick,Reference,cheatsheet,${(!options.isHome && options.filename) || ''}` },
],
},
],
],
filterPlugins: (type, plugins = []) => {
if (type === 'rehype') {
const dt = plugins.filter(plug => {
const dt = plugins.filter((plug) => {
return /(rehypeRaw)/.test(plug.name) ? false : true;
});
// 放在 rehypeDocument 前面
dt.unshift(rehypeRaw)
dt.unshift(rehypeRaw);
return dt;
}
return plugins
return plugins;
},
rewrite: (node, index, parent) => {
rehypePreviewHTML(node, parent);
Expand All @@ -64,10 +66,10 @@ export function create(str = '', options = {}) {
rehypeUrls(node);
if (node.children) {
if (node.type === 'element' && node.tagName === 'body') {
const tocsData = getTocsTree([ ...node.children ]);
const tocsData = getTocsTree([...node.children]);
if (!options.isHome) {
const tocsMenus = getTocsTitleNode([...tocsData]);
node.children = addTocsInWarp([...tocsData], getTocsTitleNodeWarpper(tocsMenus))
node.children = addTocsInWarp([...tocsData], getTocsTitleNodeWarpper(tocsMenus));
} else {
node.children = tocsData;
}
Expand All @@ -76,9 +78,8 @@ export function create(str = '', options = {}) {
node.children.push(anchorPoint());
}
}
}
}

},
};

return markdown(str, mdOptions);
}
}
21 changes: 13 additions & 8 deletions scripts/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ export const CSS_OUTPUT_PATH = path.resolve(OUTOUT, 'style/style.css');
export async function createHTML(files = [], num = 0) {
const dataFile = files[num];
if (!dataFile) {
console.log(' \n done!\n')
console.log(' \n done!\n');
return;
}
++num;
const githubURL = `https://github.com/jaywcjlove/reference/blob/main/${path.relative(process.cwd(), dataFile.path).replace(path.sep, '/')}`;

const githubURL = `https://github.com/jaywcjlove/reference/blob/main/${path
.relative(process.cwd(), dataFile.path)
.replace(path.sep, '/')}`;

const mdstr = await fs.readFile(dataFile.path);
const htmlPath = path.relative(DOCS, dataFile.path);
const outputHTMLPath = path.resolve(OUTOUT, 'docs', htmlPath).replace(/README.md$/i, 'index.html').replace(/.md$/, '.html');
const outputHTMLPath = path
.resolve(OUTOUT, 'docs', htmlPath)
.replace(/README.md$/i, 'index.html')
.replace(/.md$/, '.html');

await fs.ensureDir(path.dirname(outputHTMLPath));

Expand All @@ -28,18 +33,18 @@ export async function createHTML(files = [], num = 0) {
isHome: /README.md$/.test(path.relative(process.cwd(), dataFile.path)),
githubURL,
homePath: path.relative(path.dirname(outputHTMLPath), path.resolve(OUTOUT, 'index.html')),
css: [path.relative(path.dirname(outputHTMLPath), CSS_OUTPUT_PATH)]
css: [path.relative(path.dirname(outputHTMLPath), CSS_OUTPUT_PATH)],
});
await fs.writeFile(outputHTMLPath, html);
console.log(`♻️ \x1b[32;1m ${path.relative(OUTOUT, outputHTMLPath)} \x1b[0m`)
createHTML(files, num)
console.log(`♻️ \x1b[32;1m ${path.relative(OUTOUT, outputHTMLPath)} \x1b[0m`);
createHTML(files, num);
}

export async function run() {
await fs.ensureDir(OUTOUT);
await fs.emptyDir(OUTOUT);
await fs.ensureDir(path.dirname(CSS_OUTPUT_PATH));
await fs.copyFile(CSSPATH, CSS_OUTPUT_PATH)
await fs.copyFile(CSSPATH, CSS_OUTPUT_PATH);
const files = await recursiveReaddirFiles(process.cwd(), {
ignored: /\/(node_modules|\.git)/,
exclude: /(\.json|\.mjs|CONTRIBUTING\.md)$/,
Expand Down
51 changes: 24 additions & 27 deletions scripts/nodes/header.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ import { github, editor } from './logo.mjs';
import { getSVGNode } from '../utils/getSVGNode.mjs';
import { darkMode } from '../utils/darkMode.mjs';

const ICONS_PATH = path.resolve(process.cwd(), 'scripts/assets/quickreference.svg')
const ICONS_PATH = path.resolve(process.cwd(), 'scripts/assets/quickreference.svg');
export function header({ homePath, githubURL = '' }) {
const svgNode = getSVGNode(ICONS_PATH)
const svgNode = getSVGNode(ICONS_PATH);
const data = [
{
menu: true,
href: githubURL,
target: '__blank',
label: '编辑',
children: [editor]
children: [editor],
},
...darkMode(),
{
menu: true,
href: 'https://github.com/jaywcjlove/reference',
target: '__blank',
children: [github]
}
]
children: [github],
},
];
return {
type: 'element',
tagName: 'nav',
Expand Down Expand Up @@ -51,10 +51,8 @@ export function header({ homePath, githubURL = '' }) {
properties: {
class: ['title'],
},
children: [
{ type: 'text', value: 'Quick Reference' }
]
}
children: [{ type: 'text', value: 'Quick Reference' }],
},
],
},
{
Expand All @@ -75,29 +73,28 @@ export function header({ homePath, githubURL = '' }) {
type: 'element',
tagName: 'span',
properties: {},
children: label ? [
{ type: 'text', value: label }
] : []
}
]
}
children: label ? [{ type: 'text', value: label }] : [],
},
],
};
if (label) {
childs.children = [...children, {
type: 'element',
tagName: 'span',
properties: {},
children: [
{ type: 'text', value: label }
]
}];
childs.children = [
...children,
{
type: 'element',
tagName: 'span',
properties: {},
children: [{ type: 'text', value: label }],
},
];
} else {
childs.children = children;
}
return childs
return childs;
}),
},
],
}
},
],
};
}
}
2 changes: 1 addition & 1 deletion scripts/nodes/htmlTagAddAttri.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export function htmlTagAddAttri(node, { isHome }) {
if (node && node.tagName === 'body' && isHome) {
node.properties.class = ['home'];
}
}
}
Loading

0 comments on commit 4132a13

Please sign in to comment.