-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
77 lines (68 loc) · 2.08 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import fs from 'fs'
import path from 'path'
import * as matter from 'gray-matter'
export function getAllPosts(fields = []) {
let mdFiles = []
let mdPath = path.join(process.cwd(), '_posts/posts')
let throughDirectory = (directory) => {
fs.readdirSync(directory).forEach(file => {
const absPath = path.join(directory, file)
// console.log(absPath)
if (fs.statSync(absPath).isDirectory())
return throughDirectory(absPath)
else if(path.extname(absPath) == '.md')
return mdFiles.push(absPath)
});
}
throughDirectory(mdPath)
const mdContent = mdFiles.map(file => {
let mdCont = matter(fs.readFileSync(file, 'utf8'))
delete mdCont.orig
return ({
...mdCont,
...{filename: file.replace(/^.*[\\\/]/, '')}
})
})
mdContent.sort((a, b) => b.filename.localeCompare(a.filename))
// console.log(mdContent)
let tagsLists = {}
let tags = {}
let tagFilters = {}
mdContent.forEach(
file => {
Object.keys(file.data.tags).forEach(tagType => {
let tagsType = file.data.tags[tagType]
if (typeof tagsType === 'string' || tagsType instanceof String){
file.data.tags[tagType] = tagsType.split(',').map(
tag => tag.trim().toLowerCase()
)
}
else{
file.data.tags[tagType] = file.data.tags[tagType].map(
tag => tag.trim().toLowerCase()
)
}
tagsLists[tagType] != undefined ?
tagsLists[tagType].push(...file.data.tags[tagType]) :
tagsLists[tagType] = [...file.data.tags[tagType], 'NONE']
})
}
)
Object.keys(tagsLists).forEach(tagType => {
tagsLists[tagType] = [...new Set(tagsLists[tagType])]
tags[tagType] = tagsLists[tagType].reduce((o, key) => ({ ...o, [key]: false}), {})
tagFilters[tagType] = []
})
// console.log(mdContent)
// console.log(tagsLists)
// console.log(tags)
// console.log(tagFilters)
//
const items = {}
return {
"allPosts" : mdContent,
"tagsLists" : tagsLists,
"tagFilters" : tagFilters,
"tags" : tags
}
}