Skip to content

Commit 8a73c11

Browse files
committed
change structure of files, for diff contentTypes
1 parent c703cd8 commit 8a73c11

File tree

3 files changed

+38
-31
lines changed

3 files changed

+38
-31
lines changed

src/utils/contentMDtoAssetJS.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,29 @@ const parseChaptersArray = markdownArray => {
4949
const chaptersArray = []
5050
let chapter = {}
5151
let subchapterName = ''
52+
let subchapterIndex = 0
5253

5354
markdownArray.forEach((elem, index, array) => {
5455
const { type, text } = elem
5556
const { type: nextType } = array[index + 1] || {}
5657
const isEndOfChapter = nextType === 'h2' || !array[index + 1]
5758
if (type === 'h2') {
58-
chapter = { title: text }
59+
chapter = { title: text, subchapters: {} }
5960
}
6061
if (type === 'h3') {
6162
subchapterName = text
6263
}
6364
if (type === 'p') {
64-
const subchapter = {
65-
[subchapterName]: parseParagraph(text)
65+
subchapterIndex++
66+
const subchapterId = prefixedIndex(subchapterIndex)
67+
chapter.subchapters[subchapterId] = {
68+
title: subchapterName,
69+
content: parseParagraph(text)
6670
}
67-
chapter = {...chapter, ...subchapter }
6871
}
6972
if (isEndOfChapter) {
7073
chaptersArray.push(chapter)
74+
subchapterIndex = 0
7175
}
7276
})
7377

src/utils/createMapOfMedia.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,22 @@ const { writeFileSyncRecursive } = require('./fsUtils')
1010
const baseDir = './content'
1111

1212
const { getSubDirsOfDir, getFileMapOfDir } = require('./fsUtils')
13-
// types of files, like: /audio, /images, etc.
13+
// types of files, like: /audio, /images, etc.
1414
const mediaTypes = getSubDirsOfDir(baseDir)
1515

16-
const mediaMaps = []
17-
16+
//each mediaType has separate file
1817
mediaTypes.forEach(mediaType => {
19-
// subchapters (semantic types of files, like: /words, /phrases, etc. )
20-
const subchapters = getSubDirsOfDir(path.join(baseDir, mediaType))
21-
subchapters.forEach(subchapter => {
22-
const mapFiles = getFileMapOfDir(path.join(path.join(baseDir, mediaType, subchapter)))
23-
mediaMaps.push({ mediaType, subchapter, mapFiles })
18+
// contentType (semantic types of files, like: /words, /phrases, etc. )
19+
let fileContent = ''
20+
const contentTypes = getSubDirsOfDir(path.join(baseDir, mediaType))
21+
contentTypes.forEach(contentType => {
22+
const mapFiles = getFileMapOfDir(
23+
path.join(path.join(baseDir, mediaType, contentType))
24+
)
25+
fileContent += `"${contentType}": {${mapFiles}},`
2426
})
25-
})
26-
27-
mediaMaps.forEach(elem => {
28-
const { mediaType, subchapter } = elem
29-
const beforeContent = `// autogenerated from /content/${mediaType}/${subchapter}
30-
// via /src/utils/createMapOfMedia.js
31-
export default `
32-
33-
const jsString = JSON.stringify(elem.mapFiles, null, "\t").replace(/"(require\(.+\))"/g, '$1').replace(/\\"/g, '"')
34-
const filePath = `${path.join('./assets', mediaType, subchapter)}.js`
35-
writeFileSyncRecursive(filePath, beforeContent + jsString, 'utf-8')
36-
})
27+
fileContent = `export default { ${fileContent}}`
28+
const filePath = path.join('./assets', mediaType, 'index.js')
3729

38-
console.log('maps of media updated')
30+
writeFileSyncRecursive(filePath, fileContent, 'utf-8')
31+
})

src/utils/fsUtils.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,28 @@ const path = require('path')
44
const isFile = path => fs.statSync(path).isFile()
55
const isDirectory = path => fs.statSync(path).isDirectory()
66

7-
const getSubDirsOfDir = (dir) => fs.readdirSync(dir).filter(elem => isDirectory(path.join(dir, elem)))
7+
const getSubDirsOfDir = dir =>
8+
fs.readdirSync(dir).filter(elem => isDirectory(path.join(dir, elem)))
89

9-
const getFileMapOfDir = (dir) => fs.readdirSync(dir)
10+
/**
11+
*
12+
* @returns
13+
'005-001': require('../../content/audios/phrases/005-001.mp3'),
14+
'005-002': require('../../content/audios/phrases/005-002.mp3'),
15+
'005-003': require('../../content/audios/phrases/005-003.mp3'),
16+
*/
17+
const getFileMapOfDir = dir =>
18+
fs
19+
.readdirSync(dir)
1020
.filter(elem => isFile(path.join(dir, elem)))
1121
.reduce((prev, elem) => {
1222
const fileId = elem.replace(/\.[^.]+$/, '')
1323
const filePath = path.join('../../', dir, elem)
14-
return {...prev, [fileId]: `require("${filePath}")` }
15-
}, {})
24+
return prev + `"${fileId}": require("${filePath}"), `
25+
}, '')
1626

17-
const getFilesOfDir = (dir) => fs.readdirSync(dir)
18-
.filter(elem => isFile(path.join(dir, elem)))
27+
const getFilesOfDir = dir =>
28+
fs.readdirSync(dir).filter(elem => isFile(path.join(dir, elem)))
1929

2030
const writeFileSyncRecursive = (filename, content, charset) => {
2131
const folders = filename.split(path.sep).slice(0, -1)

0 commit comments

Comments
 (0)