create util class
This commit is contained in:
parent
0eb54fb638
commit
08f828b955
41
lib/utils.js
41
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
|
||||||
@ -27,12 +29,11 @@ export function getShortSummary(slug) {
|
|||||||
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)
|
||||||
@ -52,13 +53,12 @@ export function getSinglePost(slug) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toFilePath(slug) {
|
||||||
const cachedSlugMap = getSlugHashMap()
|
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
|
||||||
@ -84,8 +84,7 @@ export function getSlugHashMap() {
|
|||||||
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)
|
||||||
@ -103,9 +102,7 @@ export function toSlug(filePath) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
@ -147,8 +144,7 @@ export function constructGraphData() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getLocalGraphData(currentNodeId) {
|
||||||
export function getLocalGraphData(currentNodeId) {
|
|
||||||
const { nodes, edges } = constructGraphData()
|
const { nodes, edges } = constructGraphData()
|
||||||
|
|
||||||
const newNodes = nodes.map(aNode => (
|
const newNodes = nodes.map(aNode => (
|
||||||
@ -212,7 +208,7 @@ 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()
|
||||||
@ -221,14 +217,12 @@ export function getAllSlugs() {
|
|||||||
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 => {
|
||||||
@ -254,7 +248,7 @@ export function convertObject(thisObject) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function flat(array) {
|
flat(array) {
|
||||||
var result = [];
|
var result = [];
|
||||||
array.forEach(function(a) {
|
array.forEach(function(a) {
|
||||||
result.push(a);
|
result.push(a);
|
||||||
@ -264,7 +258,8 @@ function flat(array) {
|
|||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
getFlattenArray(thisObject) {
|
||||||
export function getFlattenArray(thisObject) {
|
|
||||||
return flat(thisObject.children)
|
return flat(thisObject.children)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user