Refactor: replace reference to 'posts' folder single function call

This commit is contained in:
Tuan Cao 2022-04-18 08:58:37 +07:00
parent 159e64f409
commit bfea43bb3e
3 changed files with 11 additions and 12 deletions

View File

@ -35,4 +35,7 @@ export const Node = {
return fs.readFileSync(fullPath, "utf8") return fs.readFileSync(fullPath, "utf8")
}, },
getMarkdownFolder: function () {
return path.join(process.cwd(), 'posts')
}
} }

View File

@ -1,4 +1,3 @@
import path from 'path'
import {Node} from "./node" import {Node} from "./node"
import {Transformer} from "./transformer"; import {Transformer} from "./transformer";
import unified from "unified"; import unified from "unified";
@ -6,7 +5,6 @@ import markdown from "remark-parse";
import {toString} from 'mdast-util-to-string' import {toString} from 'mdast-util-to-string'
const dirTree = require("directory-tree"); const dirTree = require("directory-tree");
const postsDirectory = path.join(process.cwd(), 'posts')
export function getContent(slug) { export function getContent(slug) {
@ -29,13 +27,13 @@ export function getShortSummary(slug) {
export function getAllMarkdownFiles() { export function getAllMarkdownFiles() {
return Node.getFiles(postsDirectory) return Node.getFiles(Node.getMarkdownFolder())
} }
export function getSinglePost(slug) { export function getSinglePost(slug) {
console.log("\n\nFile is scanning: ", slug) console.log("\n\nFile is scanning: ", 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) : path.join(process.cwd(), 'posts') + "/index.md" let currentFilePath = slug !== "index" ? toFilePath(slug) : Node.getMarkdownFolder() + "/index.md"
//console.log("currentFilePath: ", currentFilePath) //console.log("currentFilePath: ", currentFilePath)
var fileContent = Node.readFileSync(currentFilePath) var fileContent = Node.readFileSync(currentFilePath)
@ -57,9 +55,9 @@ export function toFilePath(slug) {
let filePath ; let filePath ;
if (slug === '/') { if (slug === '/') {
filePath = path.join(process.cwd(), 'posts') + "/index.md" filePath = Node.getMarkdownFolder() + "/index.md"
} else { } else {
filePath = postsDirectory + slug filePath = Node.getMarkdownFolder() + slug
.replaceAll('__','/') .replaceAll('__','/')
.replaceAll('--',' ') .replaceAll('--',' ')
.replaceAll('ambersand','&') .replaceAll('ambersand','&')
@ -76,8 +74,8 @@ export function toFilePath(slug) {
export function toSlug(filePath) { export function toSlug(filePath) {
if (Node.isFile(filePath) && filePath.includes(postsDirectory)) { if (Node.isFile(filePath) && filePath.includes(Node.getMarkdownFolder())) {
return filePath.replace(postsDirectory, '') return filePath.replace(Node.getMarkdownFolder(), '')
.replaceAll('/','__') .replaceAll('/','__')
.replaceAll(' ','--') .replaceAll(' ','--')
.replaceAll('&','ambersand') .replaceAll('&','ambersand')
@ -175,12 +173,12 @@ export function getGraphData(currentNodeId) {
export function getAllSlugs() { export function getAllSlugs() {
//console.log("\n\nAll Posts are scanning") //console.log("\n\nAll Posts are scanning")
// Get file names under /posts // 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)) return filePaths.map(f => toSlug(f))
} }
export function getDirectoryData() { export function getDirectoryData() {
const filteredDirectory = dirTree(postsDirectory, {extensions: /\.md/}); const filteredDirectory = dirTree(Node.getMarkdownFolder(), {extensions: /\.md/});
return convertObject(filteredDirectory) return convertObject(filteredDirectory)
} }

View File

@ -13,8 +13,6 @@ import MDContent from "../../components/MDContent";
import dynamic from 'next/dynamic' import dynamic from 'next/dynamic'
const DynamicGraph = dynamic( const DynamicGraph = dynamic(
() => import('../../components/Graph'), () => import('../../components/Graph'),
{ loading: () => <p>Loading ...</p>, ssr: false } { loading: () => <p>Loading ...</p>, ssr: false }