Skip to content

Commit 9a70701

Browse files
committed
feat: collect tags
1 parent 2b2b50a commit 9a70701

File tree

6 files changed

+48
-13
lines changed

6 files changed

+48
-13
lines changed

docs/测试/测试标签.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--
22
abbrlink: a80c431d
3-
tags: ['自定义标签1', '自定义标签2']
3+
tags: ['自定义标签1', '自定义标签2', '自定义标签1']
44
-->
55

66
该页面用来测试自定义标签。

packages/crd-scripts/src/conf/getPrerenderRoutes.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,65 @@ const getDirTree = (cmd) => {
88
extensions: /\.md/,
99
prerender: true,
1010
}
11+
const tagsArr = []
1112
const dirTree = dirs.map(path => DirectoryTree({
1213
path,
1314
options: otherProps,
15+
tagsArr
1416
}))
15-
return dirTree
17+
18+
return {
19+
dirTree,
20+
// duplicate total tags from document.
21+
tagsArr: Array.from(new Set(tagsArr))
22+
}
1623
}
1724

1825
// eg: ['docs/quick_start.md', 'a']
1926
// output: ['/quick_start', '/a/b', '/a/b/c']
2027
const getPrerenderRoutes = (dirTree) => {
2128
const dpCloneDirTree = JSON.parse(JSON.stringify(dirTree))
22-
const result = getPrerenderRoute(dpCloneDirTree)
29+
const result = recursiveDirTree(dpCloneDirTree)
2330
result.push('/404')
2431
return result
2532
}
2633

27-
function getPrerenderRoute(data) {
28-
const arr = []
29-
return recursive(data, '', arr)
34+
function recursiveDirTree(data) {
35+
return recursive(
36+
data,
37+
'',
38+
[]
39+
)
3040
}
3141

32-
function recursive(data, routePath, arr) {
42+
function recursive(
43+
data,
44+
routePath,
45+
prerenderRouteArr,
46+
) {
3347
data.forEach((item) => {
3448
const { mdconf } = item || {}
35-
const { abbrlink } = mdconf || {}
49+
const { abbrlink, tags } = mdconf || {}
3650
const composeRouteName = `${routePath}/${item.name}`.replace(/.md$/, '')
3751

3852
if (item.type === 'directory') {
3953
if (item.children && item.children.length > 0) {
40-
item.children = recursive(item.children, composeRouteName, arr)
54+
item.children = recursive(
55+
item.children,
56+
composeRouteName,
57+
prerenderRouteArr,
58+
)
4159
} else {
4260
item.children = []
4361
}
4462
} else if (item.type === 'file') {
4563
const prerenderRouteName = abbrlink
4664
? `/${abbrlink}`
4765
: composeRouteName
48-
arr.push(prerenderRouteName)
66+
prerenderRouteArr.push(prerenderRouteName)
4967
}
5068
})
51-
return arr
69+
return prerenderRouteArr
5270
}
5371

5472
module.exports = {

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ function safeReadDirSync(path) {
5151
function directoryTree({
5252
path,
5353
options,
54+
tagsArr = []
5455
}) {
5556
const name = PATH.basename(path)
5657
const item = { name }
@@ -78,6 +79,7 @@ function directoryTree({
7879
const contentStr = fs.readFileSync(path).toString()
7980
if (!contentStr) return
8081
const contentMatch = contentStr.match(/^<!--([^>]*)-->/)
82+
/** generate abbrlink in FrontMatter */
8183
if (options.generate) {
8284
const randomId = generateRandomId(8)
8385
if (!contentMatch) {
@@ -96,7 +98,13 @@ function directoryTree({
9698
}
9799
}
98100

99-
item.mdconf = contentMatch ? YAML.parse(contentMatch[1]) : {}
101+
const yamlParse = contentMatch ? YAML.parse(contentMatch[1]) : {}
102+
103+
if (Array.isArray(yamlParse.tags)) {
104+
tagsArr.push(...yamlParse.tags)
105+
}
106+
107+
item.mdconf = yamlParse
100108
try {
101109
// see https://stackoverflow.com/questions/2390199/finding-the-date-time-a-file-was-first-added-to-a-git-repository/2390382#2390382
102110
const result = execSync(`git log --format=%aD ${path} | tail -1`)
@@ -131,6 +139,7 @@ function directoryTree({
131139
directoryTree({
132140
path: PATH.join(path, child),
133141
options,
142+
tagsArr
134143
}),
135144
)
136145
.filter(e => !!e)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ const HtmlWebpackPlugin = require('html-webpack-plugin')
66
const { defaultHTMLPath } = require('crd-utils')
77
const FriendlyErrorsWebpackPlugin = require('@nuxtjs/friendly-errors-webpack-plugin')
88
const { getDocsConfig } = require('crd-utils')
9+
const { getDirTree } = require('./getPrerenderRoutes')
910
const config = require('./webpack.config')
1011
const paths = require('./path')
1112

1213
module.exports = function (cmd) {
1314
const docsConfig = getDocsConfig()
15+
const { tagsArr } = getDirTree(cmd)
16+
1417
config.mode = 'development'
1518
config.devtool = 'eval-source-map'
1619
config.entry = [
@@ -113,6 +116,7 @@ module.exports = function (cmd) {
113116
config.plugins = config.plugins.concat([
114117
new webpack.DefinePlugin({
115118
env: JSON.stringify('dev'),
119+
tagsArr: JSON.stringify(tagsArr)
116120
}),
117121
new webpack.HotModuleReplacementPlugin(),
118122
new HtmlWebpackPlugin({

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const Renderer = PrerenderSPAPlugin.PuppeteerRenderer
1818

1919
module.exports = function (cmd) {
2020
const docsConfig = getDocsConfig()
21-
const dirTree = getDirTree(cmd)
21+
const { dirTree, tagsArr } = getDirTree(cmd)
2222
const routes = getPrerenderRoutes(dirTree)
2323

2424
config.mode = 'production'
@@ -124,6 +124,7 @@ module.exports = function (cmd) {
124124
config.plugins = config.plugins.concat([
125125
new webpack.DefinePlugin({
126126
env: JSON.stringify('prod'),
127+
tagsArr: JSON.stringify(tagsArr)
127128
}),
128129
new HtmlWebpackPlugin({
129130
inject: true,

packages/crd-seed/component/Tags/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import styles from './index.less'
22

33
const Tags = ({}) => {
44
const { user, repo } = DOCSCONFIG || {}
5+
6+
console.log('tagsArr', tagsArr)
7+
58
return (
69
<div className={styles.tags}>
710
<div className={styles['tags-title']}>tags</div>

0 commit comments

Comments
 (0)