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")
},
getMarkdownFolder: function () {
return path.join(process.cwd(), 'posts')
}
}

View File

@ -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)
}

View File

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