2024-02-04 05:09:41 +00:00
|
|
|
import { default as Node } from "./node";
|
|
|
|
import { Transformer } from "./transformer";
|
|
|
|
import { unified } from "unified";
|
|
|
|
import markdown from "remark-parse";
|
|
|
|
import { toString } from "mdast-util-to-string";
|
|
|
|
import path from "path";
|
|
|
|
import fs from "fs";
|
2022-04-08 07:44:45 +00:00
|
|
|
|
2024-01-07 18:17:57 +00:00
|
|
|
const dirTree = require("directory-tree");
|
2022-04-16 15:55:20 +00:00
|
|
|
|
2023-12-26 04:42:33 +00:00
|
|
|
class Util {
|
2024-02-04 05:09:41 +00:00
|
|
|
_counter;
|
|
|
|
_cachedSlugMap;
|
|
|
|
_directoryData;
|
2023-12-26 04:42:33 +00:00
|
|
|
|
|
|
|
constructor() {
|
2024-02-04 05:09:41 +00:00
|
|
|
this._counter = 0;
|
|
|
|
this._cachedSlugMap = this.getSlugHashMap();
|
2023-12-26 04:42:33 +00:00
|
|
|
}
|
2022-04-16 15:55:20 +00:00
|
|
|
|
2024-01-07 16:22:37 +00:00
|
|
|
/**
|
|
|
|
* @returns {string | null}
|
|
|
|
* */
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
2023-12-26 04:39:48 +00:00
|
|
|
getContent(slug) {
|
2024-02-04 05:09:41 +00:00
|
|
|
const currentFilePath = this.toFilePath(slug);
|
|
|
|
if (currentFilePath === undefined || currentFilePath == null) return null;
|
|
|
|
return Node.readFileSync(currentFilePath);
|
2023-12-24 02:16:56 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 04:39:48 +00:00
|
|
|
getShortSummary(slug) {
|
2024-02-04 05:09:41 +00:00
|
|
|
const content = this.getContent(slug);
|
2023-12-26 04:39:48 +00:00
|
|
|
if (content === undefined || content === null) {
|
2024-02-04 05:09:41 +00:00
|
|
|
return;
|
2023-12-26 04:39:48 +00:00
|
|
|
}
|
2022-04-18 04:48:36 +00:00
|
|
|
|
2024-02-04 05:09:41 +00:00
|
|
|
const tree = unified().use(markdown).parse(content);
|
|
|
|
const plainText = toString(tree);
|
|
|
|
return plainText.split(" ").splice(0, 40).join(" ");
|
2023-12-24 02:16:56 +00:00
|
|
|
}
|
2020-11-30 11:29:34 +00:00
|
|
|
|
2023-12-26 04:39:48 +00:00
|
|
|
getAllMarkdownFiles() {
|
2024-02-04 05:09:41 +00:00
|
|
|
return Node.getFiles(Node.getMarkdownFolder());
|
2023-12-26 04:39:48 +00:00
|
|
|
}
|
2020-11-28 15:45:01 +00:00
|
|
|
|
2023-12-26 04:39:48 +00:00
|
|
|
getSinglePost(slug) {
|
|
|
|
// List of filenames that will provide existing links to wikilink
|
2024-02-04 05:09:41 +00:00
|
|
|
const currentFilePath = this.toFilePath(slug);
|
2024-01-07 16:22:37 +00:00
|
|
|
// console.log("currentFilePath: ", currentFilePath)
|
2022-04-29 03:38:42 +00:00
|
|
|
|
2024-02-04 05:09:41 +00:00
|
|
|
const fileContent = Node.readFileSync(currentFilePath);
|
2022-04-16 15:55:20 +00:00
|
|
|
|
2024-01-07 16:22:37 +00:00
|
|
|
// const currentFileFrontMatter = Transformer.getFrontMatterData(fileContent)
|
2023-12-26 04:39:48 +00:00
|
|
|
// console.log("===============\n\nFile is scanning: ", slug)
|
2024-02-04 05:09:41 +00:00
|
|
|
const [htmlContent] = Transformer.getHtmlContent(fileContent);
|
2023-12-26 04:39:48 +00:00
|
|
|
// console.log("==================================")
|
2024-01-07 16:22:37 +00:00
|
|
|
// console.log("hrmlcontents and backlinks")
|
2023-12-26 04:39:48 +00:00
|
|
|
return {
|
|
|
|
id: slug,
|
|
|
|
// ...currentFileFrontMatter,
|
2024-02-04 05:09:41 +00:00
|
|
|
data: htmlContent,
|
|
|
|
};
|
2023-12-24 02:16:56 +00:00
|
|
|
}
|
2022-04-16 15:55:20 +00:00
|
|
|
|
2023-12-26 04:39:48 +00:00
|
|
|
toFilePath(slug) {
|
2024-02-04 05:09:41 +00:00
|
|
|
return this._cachedSlugMap[slug];
|
2023-12-26 04:39:48 +00:00
|
|
|
}
|
2022-04-16 15:55:20 +00:00
|
|
|
|
2023-12-26 04:39:48 +00:00
|
|
|
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
|
|
|
|
// and not conflict-free, other solution was considered (hash file name into a hash, but this
|
|
|
|
// is not SEO-friendly and make url look ugly ==> I chose this
|
|
|
|
|
2024-02-04 05:09:41 +00:00
|
|
|
const slugMap = new Map();
|
|
|
|
this.getAllMarkdownFiles().forEach((aFile) => {
|
|
|
|
const aSlug = this.toSlug(aFile);
|
2023-12-26 04:39:48 +00:00
|
|
|
// if (slugMap.has(aSlug)) {
|
|
|
|
// slugMap[aSlug].push(aFile)
|
|
|
|
// } else {
|
|
|
|
// slugMap[aSlug] = [aFile]
|
|
|
|
// }
|
|
|
|
// Note: [Future improvement] Resolve conflict
|
2024-02-04 05:09:41 +00:00
|
|
|
slugMap[aSlug] = aFile;
|
|
|
|
});
|
2022-04-16 15:55:20 +00:00
|
|
|
|
2024-02-04 05:09:41 +00:00
|
|
|
const indexFile = "/🌎 Home.md";
|
|
|
|
slugMap.index = Node.getMarkdownFolder() + indexFile;
|
|
|
|
slugMap["/"] = Node.getMarkdownFolder() + indexFile;
|
2022-04-18 04:48:36 +00:00
|
|
|
|
2024-02-04 05:09:41 +00:00
|
|
|
return slugMap;
|
2023-12-26 04:39:48 +00:00
|
|
|
}
|
2023-12-24 02:16:56 +00:00
|
|
|
|
2023-12-26 04:39:48 +00:00
|
|
|
toSlug(filePath) {
|
2024-02-04 05:09:41 +00:00
|
|
|
const markdownFolder = Node.getMarkdownFolder();
|
|
|
|
const isFile = Node.isFile(filePath);
|
|
|
|
const isMarkdownFolder = filePath.includes(markdownFolder);
|
2023-12-26 04:39:48 +00:00
|
|
|
|
2024-01-07 16:22:37 +00:00
|
|
|
if (isFile && Boolean(isMarkdownFolder)) {
|
2024-02-04 05:09:41 +00:00
|
|
|
return filePath
|
|
|
|
.replace(markdownFolder, "")
|
|
|
|
.replaceAll("/", "_")
|
|
|
|
.replaceAll(" ", "+")
|
|
|
|
.replaceAll("&", "-")
|
|
|
|
.replace(".md", "");
|
2023-12-26 04:39:48 +00:00
|
|
|
} else {
|
2024-01-07 16:22:37 +00:00
|
|
|
// TODO handle this properly
|
2024-02-04 05:09:41 +00:00
|
|
|
return "/";
|
2023-12-26 04:39:48 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-24 02:16:56 +00:00
|
|
|
|
2023-12-26 04:39:48 +00:00
|
|
|
constructGraphData() {
|
2024-02-04 05:09:41 +00:00
|
|
|
const filepath = path.join(process.cwd(), "graph-data.json");
|
2023-12-26 04:39:48 +00:00
|
|
|
if (Node.isFile(filepath)) {
|
2024-02-04 05:09:41 +00:00
|
|
|
const data = fs.readFileSync(filepath);
|
|
|
|
return JSON.parse(String(data));
|
2023-12-26 04:39:48 +00:00
|
|
|
} else {
|
2024-02-04 05:09:41 +00:00
|
|
|
const filePaths = this.getAllMarkdownFiles();
|
|
|
|
const edges = [];
|
|
|
|
const nodes = [];
|
|
|
|
filePaths.forEach((aFilePath) => {
|
|
|
|
// const {currentFilePath} = getFileNames(filename)
|
|
|
|
const aNode = {
|
|
|
|
title: Transformer.parseFileNameFromPath(aFilePath),
|
|
|
|
slug: this.toSlug(aFilePath),
|
|
|
|
shortSummary: this.getShortSummary(this.toSlug(aFilePath)),
|
|
|
|
};
|
|
|
|
nodes.push(aNode);
|
|
|
|
|
|
|
|
// console.log("Constructing graph for node: " + aFilePath )
|
|
|
|
const internalLinks = Transformer.getInternalLinks(aFilePath);
|
|
|
|
internalLinks.forEach((aLink) => {
|
|
|
|
if (aLink.slug === null || aLink.slug.length === 0) return;
|
|
|
|
|
|
|
|
const anEdge = {
|
|
|
|
source: this.toSlug(aFilePath),
|
|
|
|
target: aLink.slug,
|
|
|
|
};
|
|
|
|
edges.push(anEdge);
|
|
|
|
// console.log("Source: " + anEdge.source)
|
|
|
|
// console.log("Target: " + anEdge.target)
|
|
|
|
});
|
|
|
|
// console.log("==============Constructing graph" )
|
|
|
|
});
|
|
|
|
const data = { nodes, edges };
|
|
|
|
fs.writeFileSync(filepath, JSON.stringify(data), "utf-8");
|
|
|
|
return data;
|
2023-12-26 04:39:48 +00:00
|
|
|
}
|
2023-12-24 02:16:56 +00:00
|
|
|
}
|
2020-11-28 15:45:01 +00:00
|
|
|
|
2023-12-26 04:39:48 +00:00
|
|
|
getLocalGraphData(currentNodeId) {
|
2024-02-04 05:09:41 +00:00
|
|
|
const { nodes, edges } = constructGraphData();
|
2022-04-07 09:52:41 +00:00
|
|
|
|
2024-02-04 05:09:41 +00:00
|
|
|
const newNodes = nodes.map((aNode) => ({
|
2023-12-24 02:16:56 +00:00
|
|
|
data: {
|
2024-02-04 05:09:41 +00:00
|
|
|
id: aNode.slug.toString(),
|
|
|
|
label: Transformer.parseFileNameFromPath(this.toFilePath(aNode.slug)),
|
|
|
|
},
|
|
|
|
}));
|
2022-04-15 02:04:28 +00:00
|
|
|
|
2024-02-04 05:09:41 +00:00
|
|
|
const newEdges = edges.map((anEdge) => ({
|
|
|
|
data: {
|
|
|
|
source: anEdge.source,
|
|
|
|
target: anEdge.target,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
|
|
|
const existingNodeIDs = newNodes.map((aNode) => aNode.data.id);
|
|
|
|
currentNodeId = currentNodeId === "index" ? "__index" : currentNodeId;
|
|
|
|
if (
|
|
|
|
currentNodeId != null &&
|
|
|
|
Boolean(existingNodeIDs.includes(currentNodeId))
|
|
|
|
) {
|
2023-12-26 04:39:48 +00:00
|
|
|
const outGoingNodeIds = newEdges
|
2024-02-04 05:09:41 +00:00
|
|
|
.filter((anEdge) => anEdge.data.source === currentNodeId)
|
|
|
|
.map((anEdge) => anEdge.data.target);
|
2022-04-15 04:00:45 +00:00
|
|
|
|
2023-12-26 04:39:48 +00:00
|
|
|
const incomingNodeIds = newEdges
|
2024-02-04 05:09:41 +00:00
|
|
|
.filter((anEdge) => anEdge.data.target === currentNodeId)
|
|
|
|
.map((anEdge) => anEdge.data.source);
|
2022-04-18 04:48:36 +00:00
|
|
|
|
2024-02-04 05:09:41 +00:00
|
|
|
outGoingNodeIds.push(currentNodeId);
|
2022-04-18 04:48:36 +00:00
|
|
|
|
2024-02-04 05:09:41 +00:00
|
|
|
const localNodeIds = incomingNodeIds.concat(
|
|
|
|
outGoingNodeIds.filter((item) => incomingNodeIds.indexOf(item) < 0),
|
|
|
|
);
|
2023-12-26 04:39:48 +00:00
|
|
|
if (localNodeIds.indexOf(currentNodeId) < 0) {
|
2024-02-04 05:09:41 +00:00
|
|
|
localNodeIds.push(currentNodeId);
|
2023-12-26 04:39:48 +00:00
|
|
|
}
|
2022-04-15 04:00:45 +00:00
|
|
|
|
2024-02-04 05:09:41 +00:00
|
|
|
const localNodes = newNodes.filter((aNode) =>
|
|
|
|
localNodeIds.includes(aNode.data.id),
|
|
|
|
);
|
|
|
|
let localEdges = newEdges
|
|
|
|
.filter((edge) => localNodeIds.includes(edge.data.source))
|
|
|
|
.filter((edge) => localNodeIds.includes(edge.data.target));
|
2022-04-15 04:00:45 +00:00
|
|
|
|
2023-12-26 04:39:48 +00:00
|
|
|
// Filter self-reference edges
|
2024-02-04 05:09:41 +00:00
|
|
|
localEdges = localEdges.filter(
|
|
|
|
(edge) => edge.data.source !== edge.data.target,
|
|
|
|
);
|
2022-04-18 09:47:02 +00:00
|
|
|
|
2023-12-26 04:39:48 +00:00
|
|
|
// TODO: Find out why target ==='/' in some case
|
2024-02-04 05:09:41 +00:00
|
|
|
localEdges = localEdges.filter((edge) => edge.data.target !== "/");
|
2023-12-26 04:39:48 +00:00
|
|
|
return {
|
|
|
|
nodes: localNodes,
|
2024-02-04 05:09:41 +00:00
|
|
|
edges: localEdges,
|
|
|
|
};
|
2023-12-26 04:39:48 +00:00
|
|
|
} else {
|
|
|
|
const filteredEdges = newEdges
|
2024-02-04 05:09:41 +00:00
|
|
|
.filter((edge) => existingNodeIDs.includes(edge.data.source))
|
|
|
|
.filter((edge) => existingNodeIDs.includes(edge.data.target));
|
2023-12-26 04:39:48 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
nodes: newNodes,
|
2024-02-04 05:09:41 +00:00
|
|
|
edges: filteredEdges,
|
|
|
|
};
|
2022-04-15 02:04:28 +00:00
|
|
|
}
|
2023-12-26 04:39:48 +00:00
|
|
|
}
|
2022-03-23 03:50:06 +00:00
|
|
|
|
2023-12-26 04:39:48 +00:00
|
|
|
getAllSlugs() {
|
2024-01-07 16:22:37 +00:00
|
|
|
// console.log("\n\nAll Posts are scanning")
|
2023-12-26 04:39:48 +00:00
|
|
|
// Get file names under /posts
|
2024-02-04 05:09:41 +00:00
|
|
|
const markdownFolder = Node.getMarkdownFolder();
|
|
|
|
const markdownFiles = Node.getFiles(markdownFolder);
|
|
|
|
const filePaths = markdownFiles.filter(
|
|
|
|
(file) =>
|
|
|
|
!(Boolean(file.endsWith("index")) || Boolean(file.endsWith("sidebar"))),
|
|
|
|
);
|
|
|
|
return filePaths.map((f) => this.toSlug(f));
|
2023-12-26 04:39:48 +00:00
|
|
|
}
|
2022-04-07 09:52:41 +00:00
|
|
|
|
2024-01-14 05:26:21 +00:00
|
|
|
/** Gets all directories - if cached already, gets cached */
|
2023-12-26 04:39:48 +00:00
|
|
|
getDirectoryData() {
|
2024-02-04 05:09:41 +00:00
|
|
|
if (this._directoryData) return this._directoryData;
|
|
|
|
const filteredDirectory = dirTree(Node.getMarkdownFolder(), {
|
|
|
|
extensions: /\.md/,
|
|
|
|
exclude: [/\.git/, /\.obsidian/],
|
|
|
|
});
|
|
|
|
const convertedDirectoryData = this.convertObject(filteredDirectory);
|
|
|
|
this._directoryData = convertedDirectoryData;
|
|
|
|
return this._directoryData;
|
2023-12-24 02:16:56 +00:00
|
|
|
}
|
2022-03-24 04:15:43 +00:00
|
|
|
|
2023-12-26 04:39:48 +00:00
|
|
|
convertObject(thisObject) {
|
2024-02-04 05:09:41 +00:00
|
|
|
const children = [];
|
|
|
|
const slugs = this.getAllSlugs();
|
2023-12-26 04:39:48 +00:00
|
|
|
|
2024-01-07 18:17:57 +00:00
|
|
|
function findFunc(_this, slug) {
|
2024-02-04 05:09:41 +00:00
|
|
|
const fileName = Transformer.parseFileNameFromPath(
|
|
|
|
_this.toFilePath(slug),
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
Transformer.normalizeFileName(fileName) ===
|
|
|
|
Transformer.normalizeFileName(thisObject.name)
|
|
|
|
);
|
2024-01-07 16:22:37 +00:00
|
|
|
}
|
|
|
|
|
2024-02-04 05:09:41 +00:00
|
|
|
const foundSlugs = slugs.find((slug) => findFunc(this, slug));
|
|
|
|
const blacklist = [null, undefined];
|
|
|
|
|
|
|
|
let routerPath = !blacklist.includes(foundSlugs) ? foundSlugs : null;
|
2024-01-07 16:22:37 +00:00
|
|
|
|
2024-02-04 05:09:41 +00:00
|
|
|
routerPath = !blacklist.includes(routerPath)
|
|
|
|
? "/notes/" + routerPath
|
|
|
|
: null;
|
2024-01-07 16:22:37 +00:00
|
|
|
|
2023-12-26 04:39:48 +00:00
|
|
|
const newObject = {
|
|
|
|
name: thisObject.name,
|
2024-01-07 16:22:37 +00:00
|
|
|
children,
|
2023-12-27 05:36:36 +00:00
|
|
|
id: (this._counter++).toString(),
|
2024-02-04 05:09:41 +00:00
|
|
|
routePath: !blacklist.includes(routerPath) ? routerPath : null,
|
|
|
|
};
|
2023-12-26 04:39:48 +00:00
|
|
|
|
|
|
|
if (thisObject.children != null && thisObject.children.length > 0) {
|
2024-02-04 05:09:41 +00:00
|
|
|
thisObject.children.forEach((aChild) => {
|
|
|
|
const newChild = this.convertObject(aChild);
|
|
|
|
children.push(newChild);
|
|
|
|
});
|
|
|
|
return newObject;
|
2023-12-26 04:39:48 +00:00
|
|
|
} else {
|
2024-02-04 05:09:41 +00:00
|
|
|
return newObject;
|
2023-12-24 02:16:56 +00:00
|
|
|
}
|
2023-12-26 04:39:48 +00:00
|
|
|
}
|
2022-03-24 04:15:43 +00:00
|
|
|
|
2023-12-27 05:36:36 +00:00
|
|
|
flat = (array) => {
|
2024-02-04 05:09:41 +00:00
|
|
|
let result = [];
|
|
|
|
const outerThis = this;
|
2024-01-07 16:22:37 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/space-before-function-paren
|
2024-02-04 05:09:41 +00:00
|
|
|
array.forEach(function (a) {
|
|
|
|
result.push(a);
|
2023-12-26 04:39:48 +00:00
|
|
|
if (Array.isArray(a.children)) {
|
2024-02-04 05:09:41 +00:00
|
|
|
result = result.concat(outerThis.flat(a.children));
|
2023-12-26 04:39:48 +00:00
|
|
|
}
|
2024-02-04 05:09:41 +00:00
|
|
|
});
|
|
|
|
return result;
|
|
|
|
};
|
2024-01-07 16:22:37 +00:00
|
|
|
|
2023-12-26 04:39:48 +00:00
|
|
|
getFlattenArray(thisObject) {
|
2024-02-04 05:09:41 +00:00
|
|
|
return this.flat(thisObject.children);
|
2023-12-26 04:39:48 +00:00
|
|
|
}
|
2023-12-24 02:16:56 +00:00
|
|
|
}
|
2023-12-26 04:39:48 +00:00
|
|
|
|
2024-02-04 05:09:41 +00:00
|
|
|
const util = new Util();
|
|
|
|
export default util;
|