Skip to content

Commit a820685

Browse files
committed
extract type of subchapter from [brackets]
1 parent 8a73c11 commit a820685

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/utils/contentMDtoAssetJS.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ const makeArrayFromMarkdown = mdFileContent =>
88
.filter(elem => elem.type !== 'space')
99
.map(elem => {
1010
const { type, depth, tokens = [] } = elem
11-
const { text } = tokens[0] || {}
11+
// only in tokens appears "smartpants"
12+
// oftern tokens.lenght == 1,
13+
// but if text has [brackets] they split tokens into array with length > 1
14+
const text = tokens.map(elem => elem.text).join('')
1215
const joinedType = depth ? type[0] + depth : type[0]
1316
return { type: joinedType, text }
1417
})
@@ -45,10 +48,23 @@ const parseInfoArray = infoArray => {
4548
}, {})
4649
}
4750

51+
const extractSubchapterBrackets = text => {
52+
if (!text) return {}
53+
const bracketsTemplate = RegExp(/\[(.+)\]/)
54+
const bracketsMatch = text.match(bracketsTemplate)
55+
if (!bracketsMatch) return { title: text }
56+
else {
57+
const type = bracketsMatch[1].toLowerCase()
58+
const title = text.replace(bracketsTemplate, '')
59+
return { title, type }
60+
}
61+
}
62+
4863
const parseChaptersArray = markdownArray => {
4964
const chaptersArray = []
5065
let chapter = {}
5166
let subchapterName = ''
67+
let subchapterType = ''
5268
let subchapterIndex = 0
5369

5470
markdownArray.forEach((elem, index, array) => {
@@ -59,19 +75,25 @@ const parseChaptersArray = markdownArray => {
5975
chapter = { title: text, subchapters: {} }
6076
}
6177
if (type === 'h3') {
62-
subchapterName = text
78+
const { type, title } = extractSubchapterBrackets(text)
79+
subchapterName = title
80+
subchapterType = type
6381
}
6482
if (type === 'p') {
6583
subchapterIndex++
6684
const subchapterId = prefixedIndex(subchapterIndex)
67-
chapter.subchapters[subchapterId] = {
85+
const subchapterObject = {
6886
title: subchapterName,
6987
content: parseParagraph(text)
7088
}
89+
if (subchapterType) subchapterObject['type'] = subchapterType
90+
chapter.subchapters[subchapterId] = subchapterObject
7191
}
7292
if (isEndOfChapter) {
7393
chaptersArray.push(chapter)
7494
subchapterIndex = 0
95+
subchapterName = ''
96+
subchapterType = ''
7597
}
7698
})
7799

0 commit comments

Comments
 (0)