From 1a11bdb2b034f180142d5f450c4724c8c53ca7f5 Mon Sep 17 00:00:00 2001 From: Tuan Cao Date: Fri, 29 Apr 2022 10:38:42 +0700 Subject: [PATCH] Fix issues: some document cannot be map from slug --- lib/utils.js | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 5418a8a..c812ba9 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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 }