removed duplicate codes
restructured series database
This commit is contained in:
parent
d4e7a5d7e5
commit
6e8b20a85c
14 changed files with 80 additions and 32 deletions
|
@ -54,7 +54,7 @@ interface Map {
|
|||
title: string
|
||||
toc: string // in series home page and ≡ (3 horizontal line) button
|
||||
length: number
|
||||
order: string[]
|
||||
order: string[] // url order
|
||||
tags: string[]
|
||||
}
|
||||
}
|
||||
|
@ -80,6 +80,16 @@ const map: Map = {
|
|||
unsearchable: {},
|
||||
}
|
||||
|
||||
interface SeriesMap {
|
||||
// key: url
|
||||
[key: string]: {
|
||||
index: number
|
||||
url: string
|
||||
}[]
|
||||
}
|
||||
|
||||
const seriesMap: SeriesMap = {}
|
||||
|
||||
// converts file path to url
|
||||
function path2URL(pathToConvert: string): string {
|
||||
return `/${path.relative(markdownPath, pathToConvert)}`
|
||||
|
@ -382,9 +392,37 @@ function recursiveParseSeries(fileOrFolderPath: string) {
|
|||
}
|
||||
|
||||
if (fileOrFolderName.startsWith("0")) {
|
||||
console.log("new series")
|
||||
} else {
|
||||
map.series[urlPath] = { ...postData, order: [], length: 0 }
|
||||
} else {
|
||||
map.posts[urlPath] = postData
|
||||
for (const key of Object.keys(map.series)) {
|
||||
if (urlPath.slice(0, urlPath.lastIndexOf("/")).includes(key)) {
|
||||
const index = parseInt(
|
||||
fileOrFolderName.slice(
|
||||
0,
|
||||
fileOrFolderName.lastIndexOf("_")
|
||||
)
|
||||
)
|
||||
|
||||
if (isNaN(index)) {
|
||||
throw Error(
|
||||
`Invalid series index at: ${fileOrFolderPath}`
|
||||
)
|
||||
}
|
||||
|
||||
const itemToPush = {
|
||||
index: index,
|
||||
url: urlPath,
|
||||
}
|
||||
|
||||
if (seriesMap[key]) {
|
||||
seriesMap[key].push(itemToPush)
|
||||
} else {
|
||||
seriesMap[key] = [itemToPush]
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -438,5 +476,23 @@ for (const tag in map.tags) {
|
|||
map.meta.tags.push(tag)
|
||||
}
|
||||
|
||||
// sort series post
|
||||
for (const seriesURL in seriesMap) {
|
||||
seriesMap[seriesURL].sort((a, b) => {
|
||||
if (a.index < b.index) {
|
||||
return -1
|
||||
}
|
||||
if (a.index > b.index) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
})
|
||||
}
|
||||
|
||||
for (const seriesURL in seriesMap) {
|
||||
map.series[seriesURL].length = seriesMap[seriesURL].length
|
||||
map.series[seriesURL].order = seriesMap[seriesURL].map((item) => item.url)
|
||||
}
|
||||
|
||||
// write to src/data/map.json
|
||||
fs.writeFileSync(mapFilePath, JSON.stringify(map))
|
||||
|
|
4
source/markdown/series/my quotes/0.md
Normal file
4
source/markdown/series/my quotes/0.md
Normal file
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
title: my quotes
|
||||
date: 2021-08-01
|
||||
---
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
title: My Quote NO.31
|
||||
title: My Quote NO.10
|
||||
date: 2021-03-22
|
||||
---
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
title: My Quote NO.6
|
||||
title: My Quote NO.1
|
||||
date: 2021-03-22
|
||||
---
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
title: My Quote NO.7
|
||||
title: My Quote NO.2
|
||||
date: 2021-03-22
|
||||
---
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
title: My Quote NO.11
|
||||
title: My Quote NO.3
|
||||
date: 2021-03-22
|
||||
---
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
title: My Quote NO.14
|
||||
title: My Quote NO.4
|
||||
date: 2021-03-22
|
||||
---
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
title: My Quote NO.16
|
||||
title: My Quote NO.5
|
||||
date: 2021-03-22
|
||||
---
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
title: My Quote NO.20
|
||||
title: My Quote NO.6
|
||||
date: 2021-03-22
|
||||
---
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
title: My Quote NO.26
|
||||
title: My Quote NO.7
|
||||
date: 2021-03-22
|
||||
---
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
title: My Quote NO.28
|
||||
title: My Quote NO.8
|
||||
date: 2021-03-22
|
||||
---
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
title: My Quote NO.30
|
||||
title: My Quote NO.9
|
||||
date: 2021-03-22
|
||||
---
|
||||
|
|
@ -37,9 +37,7 @@ export default class Page extends React.Component<PageProps, PageState> {
|
|||
let _isUnsearchable = false
|
||||
|
||||
// fetch page
|
||||
let fetchedPage = url.startsWith("/posts")
|
||||
? posts.posts[url]
|
||||
: posts.series[url]
|
||||
let fetchedPage = posts.posts[url]
|
||||
if (!fetchedPage) {
|
||||
fetchedPage = posts.unsearchable[url]
|
||||
_isUnsearchable = true
|
||||
|
|
|
@ -72,22 +72,12 @@ export default class PostList extends React.Component<
|
|||
|
||||
postCount++
|
||||
const url: string = posts.date[date][length - i - 1]
|
||||
if (url.startsWith("/posts")) {
|
||||
PostCards.push(
|
||||
<PostCard
|
||||
postData={{ url: url, ...posts.posts[url] }}
|
||||
/>
|
||||
)
|
||||
} else {
|
||||
PostCards.push(
|
||||
<PostCard
|
||||
postData={{
|
||||
url: url,
|
||||
...posts.series[url],
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
PostCards.push(
|
||||
<PostCard
|
||||
key={url}
|
||||
postData={{ url: url, ...posts.posts[url] }}
|
||||
/>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue