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