Fix issues: some document cannot be map from slug

This commit is contained in:
Tuan Cao 2022-04-29 10:38:42 +07:00
parent fdf0e419bb
commit 1a11bdb2b0

View File

@ -34,7 +34,7 @@ export function getSinglePost(slug) {
// List of filenames that will provide existing links to wikilink
let currentFilePath = slug !== "index" ? toFilePath(slug) : Node.getMarkdownFolder() + "/index.md"
let currentFilePath = toFilePath(slug)
//console.log("currentFilePath: ", currentFilePath)
var fileContent = Node.readFileSync(currentFilePath)
@ -52,25 +52,35 @@ export function getSinglePost(slug) {
}
const cachedSlugMap = getSlugHashMap()
export function toFilePath(slug) {
// Construct file name from slug of /notes/abcxyz
let filePath;
return cachedSlugMap[slug]
}
if (slug === '/') {
filePath = Node.getMarkdownFolder() + "/index.md"
} else {
filePath = Node.getMarkdownFolder() + slug
.replaceAll('__', '/')
.replaceAll('++++', ' ')
.replaceAll('ambersand', '&')
+ ".md";
}
export function getSlugHashMap() {
// This is to solve problem of converting between slug and filepath,
// where previously if I convert a slug to a file path sometime
// it does not always resolve to correct filepath, converting function is not bi-directional
// and not conflict-free, other solution was considered (hash file name into a hash, but this
// is not SEO-friendly and make url look ugly ==> I chose this
if (Node.isFile(filePath)) {
return filePath
} else {
return null
}
const slugMap = new Map()
getAllMarkdownFiles().map(aFile => {
const aSlug = toSlug(aFile);
// if (slugMap.has(aSlug)) {
// slugMap[aSlug].push(aFile)
// } else {
// slugMap[aSlug] = [aFile]
// }
// Note: [Future improvement] Resolve conflict
slugMap[aSlug] = aFile
})
slugMap['index'] = Node.getMarkdownFolder() + "/index.md"
slugMap['/'] = Node.getMarkdownFolder() + "/index.md"
return slugMap
}