Skip to content

Commit 4132a13

Browse files
committed
chore: format script code.
1 parent bd08290 commit 4132a13

24 files changed

+471
-298
lines changed

.husky/pre-commit

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
npx --no-install lint-staged

.lintstagedrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"**/*.{mjs,css,json,prettierrc,lintstagedrc}": ["prettier --write"]
3+
}

.prettierignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package.json
2+
coverage
3+
dist
4+
build
5+
docs

.prettierrc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all",
4+
"printWidth": 120,
5+
"overrides": [
6+
{ "files": ".prettierrc", "options": { "parser": "json" } },
7+
{ "files": ".lintstagedrc", "options": { "parser": "json" } }
8+
]
9+
}

package.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
"homepage": "https://jaywcjlove.github.io/reference",
88
"private": false,
99
"scripts": {
10+
"prepare": "husky install",
1011
"build": "node scripts/build.mjs",
11-
"start": "node scripts/watch.mjs"
12+
"start": "node scripts/watch.mjs",
13+
"prettier": "prettier --write '**/*.{mjs,css,json,prettierrc,lintstagedrc}'"
1214
},
1315
"repository": {
1416
"type": "git",
@@ -22,10 +24,16 @@
2224
"@wcj/markdown-to-html": "^2.1.1",
2325
"chokidar": "^3.5.3",
2426
"fs-extra": "^10.1.0",
27+
"husky": "^8.0.1",
28+
"lint-staged": "^13.0.3",
29+
"prettier": "^2.7.1",
2530
"recursive-readdir-files": "^2.3.0",
2631
"rehype-autolink-headings": "^6.1.1",
2732
"rehype-document": "^6.1.0",
2833
"rehype-slug": "^5.0.1",
2934
"remark-gemoji": "^7.0.1"
35+
},
36+
"engines": {
37+
"node": ">=16.0.0"
3038
}
3139
}

scripts/build.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { run } from './index.mjs';
22

3-
run()
3+
run();

scripts/create.mjs

+22-21
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import markdown from '@wcj/markdown-to-html';
22
import rehypeDocument from 'rehype-document';
33
import remarkGemoji from 'remark-gemoji';
44
import rehypeRaw from 'rehype-raw';
5-
import rehypeAttrs from 'rehype-attr';
6-
import rehypeKatex from 'rehype-katex';
75
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
86
import rehypeSlug from 'rehype-slug';
97
import { htmlTagAddAttri } from './nodes/htmlTagAddAttri.mjs';
@@ -23,37 +21,41 @@ export function create(str = '', options = {}) {
2321
let title = str.match(/[^===]+(?=[===])/g) || [];
2422
let description = str.match(/\n==={1,}\n+([\s\S]*?)\n/g) || [];
2523
title = title[0] || '';
26-
description = (description[0] || '').replace(/^\n[=\n]+/, '').replace(/\[([\s\S]*?)?\]\(([\s\S]*?)?\)/g, '$1').replace(/\n/, '');
27-
const subTitle = options.filename && !options.isHome ? `${options.filename} cheatsheet & `: ''
24+
description = (description[0] || '')
25+
.replace(/^\n[=\n]+/, '')
26+
.replace(/\[([\s\S]*?)?\]\(([\s\S]*?)?\)/g, '$1')
27+
.replace(/\n/, '');
28+
const subTitle = options.filename && !options.isHome ? `${options.filename} cheatsheet & ` : '';
2829
const mdOptions = {
2930
showLineNumbers: false,
3031
hastNode: false,
3132
remarkPlugins: [remarkGemoji],
3233
rehypePlugins: [
3334
rehypeSlug,
3435
rehypeAutolinkHeadings,
35-
[rehypeDocument, {
36+
[
37+
rehypeDocument,
38+
{
3639
title: `${title ? `${title} & ` : ''} ${subTitle} Quick Reference`,
37-
css: [ ...options.css ],
38-
link: [
39-
{rel: 'icon', href: favicon, type: 'image/svg+xml'}
40-
],
40+
css: [...options.css],
41+
link: [{ rel: 'icon', href: favicon, type: 'image/svg+xml' }],
4142
meta: [
4243
{ description: `${description}为开发人员分享快速参考备忘单。` },
43-
{ keywords: `Quick,Reference,cheatsheet,${!options.isHome && options.filename || ''}` }
44-
]
45-
}]
44+
{ keywords: `Quick,Reference,cheatsheet,${(!options.isHome && options.filename) || ''}` },
45+
],
46+
},
47+
],
4648
],
4749
filterPlugins: (type, plugins = []) => {
4850
if (type === 'rehype') {
49-
const dt = plugins.filter(plug => {
51+
const dt = plugins.filter((plug) => {
5052
return /(rehypeRaw)/.test(plug.name) ? false : true;
5153
});
5254
// 放在 rehypeDocument 前面
53-
dt.unshift(rehypeRaw)
55+
dt.unshift(rehypeRaw);
5456
return dt;
5557
}
56-
return plugins
58+
return plugins;
5759
},
5860
rewrite: (node, index, parent) => {
5961
rehypePreviewHTML(node, parent);
@@ -64,10 +66,10 @@ export function create(str = '', options = {}) {
6466
rehypeUrls(node);
6567
if (node.children) {
6668
if (node.type === 'element' && node.tagName === 'body') {
67-
const tocsData = getTocsTree([ ...node.children ]);
69+
const tocsData = getTocsTree([...node.children]);
6870
if (!options.isHome) {
6971
const tocsMenus = getTocsTitleNode([...tocsData]);
70-
node.children = addTocsInWarp([...tocsData], getTocsTitleNodeWarpper(tocsMenus))
72+
node.children = addTocsInWarp([...tocsData], getTocsTitleNodeWarpper(tocsMenus));
7173
} else {
7274
node.children = tocsData;
7375
}
@@ -76,9 +78,8 @@ export function create(str = '', options = {}) {
7678
node.children.push(anchorPoint());
7779
}
7880
}
79-
}
80-
}
81-
81+
},
82+
};
8283

8384
return markdown(str, mdOptions);
84-
}
85+
}

scripts/index.mjs

+13-8
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@ export const CSS_OUTPUT_PATH = path.resolve(OUTOUT, 'style/style.css');
1111
export async function createHTML(files = [], num = 0) {
1212
const dataFile = files[num];
1313
if (!dataFile) {
14-
console.log(' \n done!\n')
14+
console.log(' \n done!\n');
1515
return;
1616
}
1717
++num;
18-
const githubURL = `https://github.com/jaywcjlove/reference/blob/main/${path.relative(process.cwd(), dataFile.path).replace(path.sep, '/')}`;
19-
18+
const githubURL = `https://github.com/jaywcjlove/reference/blob/main/${path
19+
.relative(process.cwd(), dataFile.path)
20+
.replace(path.sep, '/')}`;
21+
2022
const mdstr = await fs.readFile(dataFile.path);
2123
const htmlPath = path.relative(DOCS, dataFile.path);
22-
const outputHTMLPath = path.resolve(OUTOUT, 'docs', htmlPath).replace(/README.md$/i, 'index.html').replace(/.md$/, '.html');
24+
const outputHTMLPath = path
25+
.resolve(OUTOUT, 'docs', htmlPath)
26+
.replace(/README.md$/i, 'index.html')
27+
.replace(/.md$/, '.html');
2328

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

@@ -28,18 +33,18 @@ export async function createHTML(files = [], num = 0) {
2833
isHome: /README.md$/.test(path.relative(process.cwd(), dataFile.path)),
2934
githubURL,
3035
homePath: path.relative(path.dirname(outputHTMLPath), path.resolve(OUTOUT, 'index.html')),
31-
css: [path.relative(path.dirname(outputHTMLPath), CSS_OUTPUT_PATH)]
36+
css: [path.relative(path.dirname(outputHTMLPath), CSS_OUTPUT_PATH)],
3237
});
3338
await fs.writeFile(outputHTMLPath, html);
34-
console.log(`♻️ \x1b[32;1m ${path.relative(OUTOUT, outputHTMLPath)} \x1b[0m`)
35-
createHTML(files, num)
39+
console.log(`♻️ \x1b[32;1m ${path.relative(OUTOUT, outputHTMLPath)} \x1b[0m`);
40+
createHTML(files, num);
3641
}
3742

3843
export async function run() {
3944
await fs.ensureDir(OUTOUT);
4045
await fs.emptyDir(OUTOUT);
4146
await fs.ensureDir(path.dirname(CSS_OUTPUT_PATH));
42-
await fs.copyFile(CSSPATH, CSS_OUTPUT_PATH)
47+
await fs.copyFile(CSSPATH, CSS_OUTPUT_PATH);
4348
const files = await recursiveReaddirFiles(process.cwd(), {
4449
ignored: /\/(node_modules|\.git)/,
4550
exclude: /(\.json|\.mjs|CONTRIBUTING\.md)$/,

scripts/nodes/header.mjs

+24-27
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@ import { github, editor } from './logo.mjs';
33
import { getSVGNode } from '../utils/getSVGNode.mjs';
44
import { darkMode } from '../utils/darkMode.mjs';
55

6-
const ICONS_PATH = path.resolve(process.cwd(), 'scripts/assets/quickreference.svg')
6+
const ICONS_PATH = path.resolve(process.cwd(), 'scripts/assets/quickreference.svg');
77
export function header({ homePath, githubURL = '' }) {
8-
const svgNode = getSVGNode(ICONS_PATH)
8+
const svgNode = getSVGNode(ICONS_PATH);
99
const data = [
1010
{
1111
menu: true,
1212
href: githubURL,
1313
target: '__blank',
1414
label: '编辑',
15-
children: [editor]
15+
children: [editor],
1616
},
1717
...darkMode(),
1818
{
1919
menu: true,
2020
href: 'https://github.com/jaywcjlove/reference',
2121
target: '__blank',
22-
children: [github]
23-
}
24-
]
22+
children: [github],
23+
},
24+
];
2525
return {
2626
type: 'element',
2727
tagName: 'nav',
@@ -51,10 +51,8 @@ export function header({ homePath, githubURL = '' }) {
5151
properties: {
5252
class: ['title'],
5353
},
54-
children: [
55-
{ type: 'text', value: 'Quick Reference' }
56-
]
57-
}
54+
children: [{ type: 'text', value: 'Quick Reference' }],
55+
},
5856
],
5957
},
6058
{
@@ -75,29 +73,28 @@ export function header({ homePath, githubURL = '' }) {
7573
type: 'element',
7674
tagName: 'span',
7775
properties: {},
78-
children: label ? [
79-
{ type: 'text', value: label }
80-
] : []
81-
}
82-
]
83-
}
76+
children: label ? [{ type: 'text', value: label }] : [],
77+
},
78+
],
79+
};
8480
if (label) {
85-
childs.children = [...children, {
86-
type: 'element',
87-
tagName: 'span',
88-
properties: {},
89-
children: [
90-
{ type: 'text', value: label }
91-
]
92-
}];
81+
childs.children = [
82+
...children,
83+
{
84+
type: 'element',
85+
tagName: 'span',
86+
properties: {},
87+
children: [{ type: 'text', value: label }],
88+
},
89+
];
9390
} else {
9491
childs.children = children;
9592
}
96-
return childs
93+
return childs;
9794
}),
9895
},
9996
],
100-
}
97+
},
10198
],
10299
};
103-
}
100+
}

scripts/nodes/htmlTagAddAttri.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ export function htmlTagAddAttri(node, { isHome }) {
55
if (node && node.tagName === 'body' && isHome) {
66
node.properties.class = ['home'];
77
}
8-
}
8+
}

0 commit comments

Comments
 (0)