Skip to content

Commit 24a3bce

Browse files
authored
chore: Refactor getHeadingText to remove title parameter (#1707)
getHeadingText passes the current accumulated title as the recursion's second parameter and then concatenates the returned string back onto title: title += child.value title += getHeadingText(child.children, title) Because the recursive call receives the already-accumulated title, the recursive result itself includes that prefix, so the outer concatenation duplicates earlier text (e.g. "A" + "AB" -> "AAB").
1 parent 321bfa4 commit 24a3bce

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

build/ast.mjs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ import { remark } from 'remark'
33
import gfm from 'remark-gfm'
44
import FS from 'fs-extra'
55

6-
const getHeadingText = (arr = [], title = '') => {
6+
const getHeadingText = (arr = []) => {
7+
let title = ''
78
arr.forEach(child => {
8-
title += child.value
9+
if (typeof child.value === 'string') {
10+
title += child.value
11+
}
912
if (child.children && Array.isArray(child.children)) {
10-
title += getHeadingText(child.children, title)
13+
title += getHeadingText(child.children)
1114
}
1215
})
13-
return title;
16+
return title
1417
}
1518

1619
const getSoftwareName = (obj, result = { title: '' }) => {
@@ -132,4 +135,4 @@ remark()
132135
FS.outputJsonSync('./dist/awesome-mac.zh.json', dataAST)
133136
console.log(' create file: \x1b[32;1m ./dist/awesome-mac.zh.json \x1b[0m');
134137
})
135-
.processSync(toVFile.readSync('README-zh.md'))
138+
.processSync(toVFile.readSync('README-zh.md'))

0 commit comments

Comments
 (0)