create util class
This commit is contained in:
parent
0eb54fb638
commit
08f828b955
69
lib/utils.js
69
lib/utils.js
@ -8,14 +8,16 @@ import fs from "fs";
|
||||
|
||||
const dirTree = require("directory-tree");
|
||||
|
||||
export class Util {
|
||||
_counter = 0;
|
||||
|
||||
export function getContent(slug) {
|
||||
getContent(slug) {
|
||||
let currentFilePath = toFilePath(slug)
|
||||
if (currentFilePath === undefined || currentFilePath == null) return null
|
||||
return Node.readFileSync(currentFilePath)
|
||||
}
|
||||
}
|
||||
|
||||
export function getShortSummary(slug) {
|
||||
getShortSummary(slug) {
|
||||
const content = getContent(slug)
|
||||
if (content === undefined || content === null) {
|
||||
return
|
||||
@ -25,14 +27,13 @@ export function getShortSummary(slug) {
|
||||
.parse(content)
|
||||
let plainText = toString(tree)
|
||||
return plainText.split(" ").splice(0, 40).join(" ")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export function getAllMarkdownFiles() {
|
||||
getAllMarkdownFiles() {
|
||||
return Node.getFiles(Node.getMarkdownFolder())
|
||||
}
|
||||
}
|
||||
|
||||
export function getSinglePost(slug) {
|
||||
getSinglePost(slug) {
|
||||
// List of filenames that will provide existing links to wikilink
|
||||
let currentFilePath = toFilePath(slug)
|
||||
//console.log("currentFilePath: ", currentFilePath)
|
||||
@ -50,15 +51,14 @@ export function getSinglePost(slug) {
|
||||
data: htmlContent,
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
const cachedSlugMap = getSlugHashMap()
|
||||
|
||||
export function toFilePath(slug) {
|
||||
toFilePath(slug) {
|
||||
const cachedSlugMap = getSlugHashMap()
|
||||
return cachedSlugMap[slug]
|
||||
}
|
||||
}
|
||||
|
||||
export function getSlugHashMap() {
|
||||
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
|
||||
@ -82,10 +82,9 @@ export function getSlugHashMap() {
|
||||
slugMap['/'] = Node.getMarkdownFolder() + indexFile
|
||||
|
||||
return slugMap
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export function toSlug(filePath) {
|
||||
toSlug(filePath) {
|
||||
const markdownFolder = Node.getMarkdownFolder()
|
||||
const isFile = Node.isFile(filePath)
|
||||
const isMarkdownFolder = filePath.includes(markdownFolder)
|
||||
@ -101,11 +100,9 @@ export function toSlug(filePath) {
|
||||
return '/'
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
export function constructGraphData() {
|
||||
constructGraphData() {
|
||||
const filepath = path.join(process.cwd(), "graph-data.json");
|
||||
if (Node.isFile(filepath)) {
|
||||
const data = fs.readFileSync(filepath);
|
||||
@ -145,10 +142,9 @@ export function constructGraphData() {
|
||||
fs.writeFileSync(filepath, JSON.stringify(data), "utf-8");
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export function getLocalGraphData(currentNodeId) {
|
||||
getLocalGraphData(currentNodeId) {
|
||||
const { nodes, edges } = constructGraphData()
|
||||
|
||||
const newNodes = nodes.map(aNode => (
|
||||
@ -210,25 +206,23 @@ export function getLocalGraphData(currentNodeId) {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export function getAllSlugs() {
|
||||
getAllSlugs() {
|
||||
//console.log("\n\nAll Posts are scanning")
|
||||
// Get file names under /posts
|
||||
const markdownFolder = Node.getMarkdownFolder()
|
||||
const markdownFiles = Node.getFiles(markdownFolder)
|
||||
const filePaths = markdownFiles.filter(file => !(file.endsWith("index") || file.endsWith("sidebar")))
|
||||
return filePaths.map(f => toSlug(f))
|
||||
}
|
||||
}
|
||||
|
||||
export function getDirectoryData() {
|
||||
getDirectoryData() {
|
||||
const filteredDirectory = dirTree(Node.getMarkdownFolder(), { extensions: /\.md/, exclude: [/\.git/, /\.obsidian/] });
|
||||
return convertObject(filteredDirectory)
|
||||
}
|
||||
}
|
||||
|
||||
let _counter = 0;
|
||||
|
||||
export function convertObject(thisObject) {
|
||||
convertObject(thisObject) {
|
||||
const children = []
|
||||
|
||||
let routerPath = getAllSlugs().find(slug => {
|
||||
@ -252,9 +246,9 @@ export function convertObject(thisObject) {
|
||||
} else {
|
||||
return newObject
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function flat(array) {
|
||||
flat(array) {
|
||||
var result = [];
|
||||
array.forEach(function(a) {
|
||||
result.push(a);
|
||||
@ -263,8 +257,9 @@ function flat(array) {
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
getFlattenArray(thisObject) {
|
||||
return flat(thisObject.children)
|
||||
}
|
||||
}
|
||||
|
||||
export function getFlattenArray(thisObject) {
|
||||
return flat(thisObject.children)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user