Skip to content

Commit 8cc8674

Browse files
committed
fix: #147
1 parent c813a60 commit 8cc8674

File tree

4 files changed

+39
-47
lines changed

4 files changed

+39
-47
lines changed

docs/更新日志.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22

33
`create-react-doc` 严格遵循 [Semantic Versioning 2.0.0](http://semver.org/lang/zh-CN/) 语义化版本规范。
44

5+
### 1.3.3
6+
7+
`2021-06-24`
8+
9+
- **Fix**
10+
11+
- 🐞 修复编译预渲染时, 缺少多层级目录文件生成的问题。[issue](https://github.com/MuYunyun/create-react-doc/issues/147)
12+
513
### 1.3.0
614

7-
`2020-06-09`
15+
`2021-06-09`
816

917
- **Feature**
1018

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
const fs = require('fs')
2-
const { getDocsConfig } = require('crd-utils')
31
const DirectoryTree = require('./node-directory-tree')
42

53
// eg: ['docs/quick_start.md', 'a']
64
// output: ['/quick_start', '/a/b', '/a/b/c']
75
const getPrerenderRoutes = (cmd) => {
86
const dir = cmd.markdownPaths
9-
console.log('dir123', dir)
107
const dirs = Array.isArray(dir) ? dir : [dir]
118
const otherProps = {
129
mdconf: true,
@@ -17,41 +14,32 @@ const getPrerenderRoutes = (cmd) => {
1714
path,
1815
options: otherProps,
1916
}))
20-
console.log('dirTree123', dirTree)
21-
const docsConfig = getDocsConfig()
22-
const menu = docsConfig && Array.isArray(docsConfig.menu)
23-
? docsConfig.menu
24-
: []
25-
const result = ['/README', '/404']
26-
dfs(menu, result, '', true)
27-
// console.log('✅ result', result)
17+
const result = getPrerenderRoute(dirTree)
18+
result.push('/404')
19+
// console.log('✅ prerender', result)
2820
return result
2921
}
3022

31-
const dfs = (arr, result, prefix, isRoot) => {
32-
for (let i = 0; i < arr.length; i++) {
33-
const source = `${prefix}${arr[i]}`
34-
// console.log('source', source)
35-
const stats = fs.statSync(source)
36-
const isFile = stats.isFile()
37-
const isDirectory = stats.isDirectory()
38-
39-
if (isDirectory) {
40-
const dirArr = fs.readdirSync(source)
41-
dfs(dirArr, result, `${source}/`, false)
42-
}
23+
function getPrerenderRoute(data) {
24+
const arr = []
25+
return recursive(data, '', arr)
26+
}
4327

44-
if (isFile && arr[i].indexOf('.md') > -1) {
45-
if (isRoot) {
46-
// eg: 'a/b.md'
47-
const splitArr = arr[i].split('/')
48-
const lastSplitValue = splitArr[splitArr.length - 1]
49-
result.push(`/${lastSplitValue.split('.md')[0]}`)
28+
function recursive(data, routePath, arr) {
29+
data.forEach((item) => {
30+
const routePropsCurrent = `${routePath}/${item.name}`.replace(/.md$/, '')
31+
if (item.type === 'directory') {
32+
if (item.children && item.children.length > 0) {
33+
// eslint-disable-next-line no-unused-vars
34+
item.children = recursive(item.children, routePropsCurrent, arr)
5035
} else {
51-
result.push(`/${prefix}${arr[i].split('.md')[0]}`)
36+
item.children = []
5237
}
38+
} else if (item.type === 'file') {
39+
arr.push(routePropsCurrent)
5340
}
54-
}
41+
})
42+
return arr
5543
}
5644

5745
module.exports = getPrerenderRoutes

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ function directoryTree({
5252
options,
5353
}) {
5454
const name = PATH.basename(path)
55-
const item = { path, name }
55+
const item = { name }
56+
if (!options.prerender) {
57+
item.path = path
58+
}
5659
let stats
5760
try {
5861
stats = fs.statSync(path)
@@ -69,9 +72,10 @@ function directoryTree({
6972
// Skip if it does not match the extension regex
7073
if (options && options.extensions && !options.extensions.test(ext)) { return null }
7174

72-
if (options && options.mdconf && !options.prerender) {
75+
if (options && options.mdconf) {
76+
item.type = constants.FILE
7377
const contentStr = fs.readFileSync(path).toString()
74-
if (contentStr) {
78+
if (contentStr && !options.prerender) {
7579
const contentMatch = contentStr.match(/^<!--(\s?[^>]*)-->/)
7680
item.relative = item.path.replace(process.cwd(), '')
7781
item.mdconf = contentMatch ? YAML.parse(contentMatch[1]) : {}
@@ -97,11 +101,9 @@ function directoryTree({
97101
// eslint-disable-next-line no-console
98102
console.log(`error: ${error.message}`)
99103
}
104+
item.size = stats.size // File size in bytes
105+
item.extension = ext
100106
}
101-
102-
item.size = stats.size // File size in bytes
103-
item.extension = ext
104-
item.type = constants.FILE
105107
}
106108
} else if (stats.isDirectory()) {
107109
const dirData = safeReadDirSync(path)
@@ -115,9 +117,9 @@ function directoryTree({
115117
}),
116118
)
117119
.filter(e => !!e)
120+
item.type = constants.DIRECTORY
118121
if (!options.prerender) {
119122
item.size = item.children.reduce((prev, cur) => prev + cur.size, 0)
120-
item.type = constants.DIRECTORY
121123
}
122124
} else {
123125
return null // Or set item.size = 0 for devices, FIFO and sockets ?

packages/scripts/src/conf/rawTreeReplaceLoader.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,23 @@ module.exports = function (source) {
5656
let content = typeof source === 'string' ? JSON.parse(source) : source
5757
// It's said loader resuls are flagged as cacheable. See https://webpack.js.org/api/loaders/#thiscacheable.
5858
// if (this.cacheable) this.cacheable()
59-
// Todo: https://webpack.js.org/api/loaders/#pitching-loader
6059
if (directoryTrees && (!include || include.test(this.resourcePath))) {
6160
const dirs = Array.isArray(dir) ? dir : [dir]
62-
// todo: white some logic about route relative in getRelativePath
6361
const dirTree = dirs.map(path => DirectoryTree({
6462
path,
6563
options: otherProps,
6664
}))
67-
// if (Array.isArray(dirTree)) {
68-
// console.log('dirTree', dirTree[3] && dirTree[3].children)
69-
// }
70-
// content = dirTree
7165

7266
const filemd = getAllWatchPath(dirTree)
7367
filemd.forEach((fileItem) => {
7468
this.addDependency(fileItem)
7569
})
70+
// replace full file path with relative path
7671
content = getRelativePath(
7772
dirTree,
7873
relativePath,
7974
dirs,
8075
)
81-
// console.log('content', content)
8276
}
8377
content = JSON.stringify(content)
8478
.replace(/\u2028/g, '\\u2028')

0 commit comments

Comments
 (0)