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 // 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) //console.log("currentFilePath: ", currentFilePath)
var fileContent = Node.readFileSync(currentFilePath) var fileContent = Node.readFileSync(currentFilePath)
@ -52,25 +52,35 @@ export function getSinglePost(slug) {
} }
const cachedSlugMap = getSlugHashMap()
export function toFilePath(slug) { export function toFilePath(slug) {
// Construct file name from slug of /notes/abcxyz return cachedSlugMap[slug]
let filePath;
if (slug === '/') {
filePath = Node.getMarkdownFolder() + "/index.md"
} else {
filePath = Node.getMarkdownFolder() + slug
.replaceAll('__', '/')
.replaceAll('++++', ' ')
.replaceAll('ambersand', '&')
+ ".md";
} }
if (Node.isFile(filePath)) { export function getSlugHashMap() {
return filePath // This is to solve problem of converting between slug and filepath,
} else { // where previously if I convert a slug to a file path sometime
return null // 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
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
} }