From bfea43bb3e6446d167eb38f5b2503901850aa29a Mon Sep 17 00:00:00 2001 From: Tuan Cao Date: Mon, 18 Apr 2022 08:58:37 +0700 Subject: [PATCH] Refactor: replace reference to 'posts' folder single function call --- lib/node.js | 3 +++ lib/utils.js | 18 ++++++++---------- pages/note/[id].js | 2 -- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/node.js b/lib/node.js index 0d55d40..60fe6f3 100644 --- a/lib/node.js +++ b/lib/node.js @@ -35,4 +35,7 @@ export const Node = { return fs.readFileSync(fullPath, "utf8") }, + getMarkdownFolder: function () { + return path.join(process.cwd(), 'posts') + } } \ No newline at end of file diff --git a/lib/utils.js b/lib/utils.js index a75d261..dbf44a4 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,4 +1,3 @@ -import path from 'path' import {Node} from "./node" import {Transformer} from "./transformer"; import unified from "unified"; @@ -6,7 +5,6 @@ import markdown from "remark-parse"; import {toString} from 'mdast-util-to-string' const dirTree = require("directory-tree"); -const postsDirectory = path.join(process.cwd(), 'posts') export function getContent(slug) { @@ -29,13 +27,13 @@ export function getShortSummary(slug) { export function getAllMarkdownFiles() { - return Node.getFiles(postsDirectory) + return Node.getFiles(Node.getMarkdownFolder()) } export function getSinglePost(slug) { console.log("\n\nFile is scanning: ", slug) // List of filenames that will provide existing links to wikilink - let currentFilePath = slug !== "index" ? toFilePath(slug) : path.join(process.cwd(), 'posts') + "/index.md" + let currentFilePath = slug !== "index" ? toFilePath(slug) : Node.getMarkdownFolder() + "/index.md" //console.log("currentFilePath: ", currentFilePath) var fileContent = Node.readFileSync(currentFilePath) @@ -57,9 +55,9 @@ export function toFilePath(slug) { let filePath ; if (slug === '/') { - filePath = path.join(process.cwd(), 'posts') + "/index.md" + filePath = Node.getMarkdownFolder() + "/index.md" } else { - filePath = postsDirectory + slug + filePath = Node.getMarkdownFolder() + slug .replaceAll('__','/') .replaceAll('--',' ') .replaceAll('ambersand','&') @@ -76,8 +74,8 @@ export function toFilePath(slug) { export function toSlug(filePath) { - if (Node.isFile(filePath) && filePath.includes(postsDirectory)) { - return filePath.replace(postsDirectory, '') + if (Node.isFile(filePath) && filePath.includes(Node.getMarkdownFolder())) { + return filePath.replace(Node.getMarkdownFolder(), '') .replaceAll('/','__') .replaceAll(' ','--') .replaceAll('&','ambersand') @@ -175,12 +173,12 @@ export function getGraphData(currentNodeId) { export function getAllSlugs() { //console.log("\n\nAll Posts are scanning") // Get file names under /posts - const filePaths = Node.getFiles(postsDirectory).filter(f => !(f.endsWith("index") || f.endsWith("sidebar"))) + const filePaths = Node.getFiles(Node.getMarkdownFolder()).filter(f => !(f.endsWith("index") || f.endsWith("sidebar"))) return filePaths.map(f => toSlug(f)) } export function getDirectoryData() { - const filteredDirectory = dirTree(postsDirectory, {extensions: /\.md/}); + const filteredDirectory = dirTree(Node.getMarkdownFolder(), {extensions: /\.md/}); return convertObject(filteredDirectory) } diff --git a/pages/note/[id].js b/pages/note/[id].js index ebfb800..a5cf540 100644 --- a/pages/note/[id].js +++ b/pages/note/[id].js @@ -13,8 +13,6 @@ import MDContent from "../../components/MDContent"; import dynamic from 'next/dynamic' - - const DynamicGraph = dynamic( () => import('../../components/Graph'), { loading: () =>

Loading ...

, ssr: false }