Skip to content

Commit f1464e3

Browse files
committed
feat: support generate uuid
1 parent ea8842c commit f1464e3

File tree

8 files changed

+125
-58
lines changed

8 files changed

+125
-58
lines changed

docs/数学公式.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
<!--
2+
title: 数学公式
3+
date: 2019-05-12 13:23:44
4+
-->
5+
16
## 数学公式
27

38
支持在 `$$``$$` 之间书写 latex 数学公式即能显示在网页上。

packages/crd-scripts/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const initCache = require('./src/utils/initCache')
44
const Servers = require('./src/server')
55
const Build = require('./src/build')
66
const Deploy = require('./src/deploy')
7+
const Generate = require('./src/generate')
78
const paths = require('./src/conf/path')
89

910
module.exports = {
@@ -13,5 +14,6 @@ module.exports = {
1314
Servers,
1415
Build,
1516
Deploy,
17+
Generate,
1618
paths,
1719
}

packages/crd-scripts/src/conf/node-directory-tree.js

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const fs = require('fs')
33
const PATH = require('path')
44
const YAML = require('yamljs')
55
const { execSync } = require('child_process')
6+
const { replaceFileContent } = require('crd-utils')
67
const { getDigitFromDir, timeFormat } = require('../utils')
78

89
const constants = {
@@ -37,14 +38,14 @@ function safeReadDirSync(path) {
3738
return dirData
3839
}
3940

40-
/**
41-
* build directory Tree, fork from https://github.com/mihneadb/node-directory-tree
41+
/** build directory Tree, fork from https://github.com/mihneadb/node-directory-tree
4242
* path: path for file
4343
* options: {
4444
* exclude: RegExp|RegExp[] - A RegExp or an array of RegExp to test for exclusion of directories.
4545
* extensions : RegExp - A RegExp to test for exclusion of files with the matching extension.
46-
* mdconf: boolean
47-
* prerender: boolean use for prerender
46+
* mdconf: Boolean.
47+
* prerender: Boolean. Used for prerender.
48+
* generate: Boolean. Used for generating info in front-matter.
4849
* }
4950
*/
5051
function directoryTree({
@@ -75,37 +76,41 @@ function directoryTree({
7576
if (options && options.mdconf) {
7677
item.type = constants.FILE
7778
const contentStr = fs.readFileSync(path).toString()
78-
if (contentStr) {
79-
const contentMatch = contentStr.match(/^<!--([^>]*)-->/)
80-
item.mdconf = contentMatch ? YAML.parse(contentMatch[1]) : {}
81-
try {
82-
// see https://stackoverflow.com/questions/2390199/finding-the-date-time-a-file-was-first-added-to-a-git-repository/2390382#2390382
83-
const result = execSync(`git log --format=%aD ${path} | tail -1`)
84-
item.birthtime =
85-
Buffer.isBuffer(result) && timeFormat(new Date(result))
86-
} catch (error) {
87-
// eslint-disable-next-line no-console
88-
console.log(`error: ${error.message}`)
89-
}
90-
try {
91-
// see https://stackoverflow.com/questions/22497597/get-the-last-modification-data-of-a-file-in-git-repo
92-
const result = execSync(`git log -1 --pretty="format:%ci" ${path}`)
93-
item.mtime = Buffer.isBuffer(result) && timeFormat(new Date(result))
94-
} catch (error) {
95-
// eslint-disable-next-line no-console
96-
console.log(`error: ${error.message}`)
97-
}
98-
item.size = stats.size // File size in bytes
99-
item.extension = ext
100-
if (!options.prerender) {
101-
item.relative = item.path.replace(process.cwd(), '')
102-
item.isEmpty = contentMatch
103-
? !String.prototype.trim.call(contentStr.replace(contentMatch[0], ''))
104-
: true
105-
const uglifyContent = contentStr.replace(/\s/g, '')
106-
item.content = uglifyContent
79+
if (!contentStr) return
80+
const contentMatch = contentStr.match(/^<!--([^>]*)-->/)
81+
if (options.generate) {
82+
if (contentMatch && contentMatch[1].indexOf('abbrlink') === -1) {
83+
replaceFileContent(path, contentMatch[1], `\nabbrlink: 290a4219${contentMatch[1]}`)
84+
console.log('✅ replaceFileContent success')
10785
}
10886
}
87+
88+
item.mdconf = contentMatch ? YAML.parse(contentMatch[1]) : {}
89+
try {
90+
// see https://stackoverflow.com/questions/2390199/finding-the-date-time-a-file-was-first-added-to-a-git-repository/2390382#2390382
91+
const result = execSync(`git log --format=%aD ${path} | tail -1`)
92+
item.birthtime =
93+
Buffer.isBuffer(result) && timeFormat(new Date(result))
94+
} catch (error) {
95+
console.log(`❎ error: ${error.message}`)
96+
}
97+
try {
98+
// see https://stackoverflow.com/questions/22497597/get-the-last-modification-data-of-a-file-in-git-repo
99+
const result = execSync(`git log -1 --pretty="format:%ci" ${path}`)
100+
item.mtime = Buffer.isBuffer(result) && timeFormat(new Date(result))
101+
} catch (error) {
102+
console.log(`❎ error: ${error.message}`)
103+
}
104+
item.size = stats.size // File size in bytes
105+
item.extension = ext
106+
if (!options.prerender) {
107+
item.relative = item.path.replace(process.cwd(), '')
108+
item.isEmpty = contentMatch
109+
? !String.prototype.trim.call(contentStr.replace(contentMatch[0], ''))
110+
: true
111+
const uglifyContent = contentStr.replace(/\s/g, '')
112+
item.content = uglifyContent
113+
}
109114
}
110115
} else if (stats.isDirectory()) {
111116
const dirData = safeReadDirSync(path)

packages/crd-scripts/src/conf/webpack.config.prod.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const CopyMarkdownImageWebpackPlugin = require('copy-markdown-image-webpack-plug
77
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
88
const PrerenderSPAPlugin = require('crd-prerender-spa-plugin')
99
const { generateSiteMap } = require('crd-generator-sitemap')
10-
// const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
11-
// const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
10+
// const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
11+
// const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
1212
const fs = require('fs-extra')
1313
const { defaultHTMLPath, docsBuildDist } = require('crd-utils')
1414
const { getDocsConfig } = require('crd-utils')
@@ -171,13 +171,13 @@ module.exports = function (cmd) {
171171
await fs.remove(`${docsBuildDist}/${docsConfig.repo}`)
172172
// move README as root index.html
173173
await fs.copy(`${docsBuildDist}/README/index.html`, `${docsBuildDist}/index.html`)
174-
console.log('generate prerender file success!')
174+
console.log('generate prerender file success!')
175175
if (docsConfig.seo) {
176176
if (docsConfig.seo.google) {
177177
fs.writeFileSync(`${docsBuildDist}/sitemap.xml`, generateSiteMap(routes))
178178
}
179179
}
180-
console.log('generate sitemap file success!')
180+
console.log('generate sitemap file success!')
181181
}
182182
},
183183
// The actual renderer to use. (Feel free to write your own)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const fs = require('fs')
2+
const { docsConfig } = require('crd-utils')
3+
const DirectoryTree = require('./conf/node-directory-tree')
4+
5+
module.exports = function generate(program) {
6+
if (!fs.existsSync(docsConfig)) {
7+
console.log('❎ please check config.yml in root dir!\n')
8+
return
9+
}
10+
11+
const dir = program.markdownPaths
12+
const dirs = Array.isArray(dir) ? dir : [dir]
13+
const otherProps = {
14+
mdconf: true,
15+
extensions: /\.md/,
16+
generate: true
17+
}
18+
dirs.map(path => DirectoryTree({
19+
path,
20+
options: otherProps,
21+
}))
22+
console.log('✅ generate success!')
23+
}

packages/crd-utils/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,29 @@ const getDocsConfig = () => {
1616
return yaml.safeLoad(fs.readFileSync(docsConfig))
1717
}
1818

19+
const replaceFileContent = (path, source, target) => {
20+
fs.readFile(path, (err, data) => {
21+
if (err) {
22+
console.log(`❎ readFileContent error in ${path}`)
23+
return
24+
}
25+
console.log(`✅ readFileContent success in ${path}`)
26+
const replaceResult = data.toString().replace(source, target)
27+
fs.writeFile(path, replaceResult, (err) => {
28+
if (err) {
29+
console.log(`❎ writeFileContent error in ${path}`)
30+
return
31+
}
32+
})
33+
console.log(`✅ writeFileContent success in ${path}`)
34+
})
35+
}
36+
1937
module.exports = {
2038
resolveApp,
2139
resolveTool,
2240
getDocsConfig,
41+
replaceFileContent,
2342
// common paths
2443
docsGitIgnore: resolveApp('.gitignore'),
2544
docsBase: resolveApp(''),

packages/create-react-doc/README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212

1313
# react-doc
1414

15-
react-doc 是配合 [Create React Doc](https://github.com/MuYunyun/create-react-doc) 使用的脚手架。
15+
react-doc 是配合 [Create React Doc](https://github.com/MuYunyun/create-react-doc) 使用的脚手架,其内置如下命令
1616

1717
```bash
18-
$ react-doc doc // 在当前文件夹下创建名为 doc 的文档站点
19-
$ react-doc start // 启动项目
20-
$ react-doc build // 项目打包
21-
$ react-doc deploy // 手动发布项目
18+
$ react-doc doc // 在当前文件夹下创建名为 doc 的文档站点
19+
$ react-doc start // 启动项目
20+
$ react-doc build // 项目打包
21+
$ react-doc deploy // 手动发布项目
22+
$ react-doc theme // 创建新主题包
23+
$ react-doc generate // 用于自动生成 Front-matter 中相关字段信息
2224
```
2325

2426
# 调试
2527

26-
进入该目录, 执行 yarn link, 即可在全局使用 react-doc xxx 命令。
28+
进入此包目录, 执行 yarn link, 即可在全局使用 react-doc xxx 命令。

packages/create-react-doc/index.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
initTheme,
88
initCache,
99
Deploy,
10+
Generate,
1011
Servers,
1112
Build,
1213
} = require('crd-scripts')
@@ -17,10 +18,11 @@ const pkg = require('./package.json')
1718
program
1819
.version(pkg.version, '-v, --version')
1920
.description('Markdown doc site generator for React.')
20-
.option('start', 'Documents generated.')
21+
.option('start', 'Start the documents site in local env.')
2122
.option('build', 'Build the documents generated.')
2223
.option('deploy', 'Deploy site to gh-pages.')
2324
.option('theme', 'Create a new theme')
25+
.option('generate', 'Generate info in front-matter')
2426
.option('-o, --output <path>', 'Writes the compiled file to the disk directory.', '.crd-dist')
2527
.option('-p, --port [number]', 'The port.', 3000)
2628
.option('--host [host]', 'The host.', '0.0.0.0')
@@ -32,14 +34,15 @@ program
3234
console.log(' $ react-doc start')
3335
console.log(' $ react-doc build')
3436
console.log(' $ react-doc deploy')
37+
console.log(' $ react-doc theme')
38+
console.log(' $ react-doc generate')
3539
console.log()
3640
})
3741
// the third value in process.argv is the value we want.
3842
.parse(process.argv)
3943

40-
const { start, build, deploy, theme } = program
41-
42-
if (!start && !build && !deploy && !theme) return initProject(program)
44+
const { start, build, deploy, theme, generate } = program
45+
if (!start && !build && !deploy && !theme && !generate) return initProject(program)
4346

4447
if (theme) {
4548
return input({
@@ -62,7 +65,7 @@ program.output = path.join(process.cwd(), program.output)
6265
const docsConfig = getDocsConfig()
6366

6467
// assign all the markdown dir
65-
if (start || build) {
68+
if (start || build || generate) {
6669
fs.existsSync(docsReadme) &&
6770
program.markdownPaths.push(docsReadme)
6871

@@ -84,23 +87,31 @@ if (deploy) {
8487
}
8588

8689
// no point markdown paths
87-
if (program.markdownPaths.length === 0) return console.log('Please specify the directory in config.yml.'.red)
90+
if (program.markdownPaths.length === 0) {
91+
return console.log('❎ Please specify the markdownPaths props in config.yml.'.red)
92+
}
8893

8994
let isExists = true
9095
// judge if files exist.
9196
program.markdownPaths.forEach((item) => {
9297
if (!fs.existsSync(item)) {
93-
console.log(`Error: Directory ${item.yellow} does not exist`.red)
9498
isExists = false
9599
}
96100
})
97101

98-
if (isExists) {
99-
initCache(program, () => {
100-
if (build) {
101-
Build(program)
102-
} else {
103-
Servers(program)
104-
}
105-
})
102+
if (!isExists) {
103+
console.log(`❎ Error: Directory ${item.yellow} does not exist`.red)
104+
return
105+
}
106+
107+
if (generate) {
108+
return Generate(program)
106109
}
110+
111+
initCache(program, () => {
112+
if (build) {
113+
Build(program)
114+
} else {
115+
Servers(program)
116+
}
117+
})

0 commit comments

Comments
 (0)