2023-12-24 02:16:56 +00:00
|
|
|
import { Node } from "./node"
|
|
|
|
import { Transformer } from "./transformer";
|
|
|
|
import { unified } from "unified";
|
2022-04-08 07:44:45 +00:00
|
|
|
import markdown from "remark-parse";
|
2023-12-24 02:16:56 +00:00
|
|
|
import { toString } from 'mdast-util-to-string'
|
2022-04-29 08:35:10 +00:00
|
|
|
import path from "path";
|
|
|
|
import fs from "fs";
|
2022-04-08 07:44:45 +00:00
|
|
|
|
2022-04-16 15:55:20 +00:00
|
|
|
const dirTree = require("directory-tree");
|
|
|
|
|
|
|
|
|
2022-04-17 13:04:58 +00:00
|
|
|
export function getContent(slug) {
|
2023-12-24 02:16:56 +00:00
|
|
|
let currentFilePath = toFilePath(slug)
|
|
|
|
if (currentFilePath === undefined || currentFilePath == null) return null
|
|
|
|
return Node.readFileSync(currentFilePath)
|
2022-04-08 07:44:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-17 13:04:58 +00:00
|
|
|
export function getShortSummary(slug) {
|
2023-12-24 02:16:56 +00:00
|
|
|
const content = getContent(slug)
|
|
|
|
if (content === undefined || content === null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const tree = unified().use(markdown)
|
|
|
|
.parse(content)
|
|
|
|
let plainText = toString(tree)
|
|
|
|
return plainText.split(" ").splice(0, 40).join(" ")
|
2022-04-08 07:44:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 03:50:06 +00:00
|
|
|
|
2022-04-17 13:04:58 +00:00
|
|
|
export function getAllMarkdownFiles() {
|
2023-12-24 02:16:56 +00:00
|
|
|
return Node.getFiles(Node.getMarkdownFolder())
|
2022-04-05 04:47:41 +00:00
|
|
|
}
|
2022-04-18 04:48:36 +00:00
|
|
|
|
2022-04-17 13:04:58 +00:00
|
|
|
export function getSinglePost(slug) {
|
2022-04-18 04:48:36 +00:00
|
|
|
|
2023-12-26 02:48:51 +00:00
|
|
|
const t = new Transformer()
|
2022-04-07 09:52:41 +00:00
|
|
|
|
2023-12-24 02:16:56 +00:00
|
|
|
// List of filenames that will provide existing links to wikilink
|
|
|
|
let currentFilePath = toFilePath(slug)
|
|
|
|
//console.log("currentFilePath: ", currentFilePath)
|
2020-11-30 11:29:34 +00:00
|
|
|
|
2023-12-24 02:16:56 +00:00
|
|
|
var fileContent = Node.readFileSync(currentFilePath)
|
2020-11-28 15:45:01 +00:00
|
|
|
|
2023-12-26 02:48:51 +00:00
|
|
|
//const currentFileFrontMatter = t.getFrontMatterData(fileContent)
|
2023-12-24 02:16:56 +00:00
|
|
|
// console.log("===============\n\nFile is scanning: ", slug)
|
2023-12-26 02:48:51 +00:00
|
|
|
const [htmlContent] = t.getHtmlContent(fileContent)
|
2023-12-24 02:16:56 +00:00
|
|
|
// console.log("==================================")
|
|
|
|
//console.log("hrmlcontents and backlinks")
|
|
|
|
return {
|
|
|
|
id: slug,
|
|
|
|
// ...currentFileFrontMatter,
|
|
|
|
data: htmlContent,
|
|
|
|
}
|
2020-11-30 11:29:34 +00:00
|
|
|
|
2022-04-07 09:52:41 +00:00
|
|
|
}
|
2020-11-28 15:45:01 +00:00
|
|
|
|
2022-04-29 03:38:42 +00:00
|
|
|
const cachedSlugMap = getSlugHashMap()
|
|
|
|
|
2022-04-16 15:55:20 +00:00
|
|
|
export function toFilePath(slug) {
|
2023-12-24 02:16:56 +00:00
|
|
|
return cachedSlugMap[slug]
|
2022-04-29 03:38:42 +00:00
|
|
|
}
|
2022-04-17 13:04:58 +00:00
|
|
|
|
2022-04-29 03:38:42 +00:00
|
|
|
export function getSlugHashMap() {
|
2023-12-24 02:16:56 +00:00
|
|
|
// 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
|
|
|
|
|
|
|
|
const slugMap = new Map()
|
|
|
|
getAllMarkdownFiles().map(aFile => {
|
|
|
|
const aSlug = toSlug(aFile);
|
|
|
|
// if (slugMap.has(aSlug)) {
|
|
|
|
// slugMap[aSlug].push(aFile)
|
|
|
|
// } else {
|
|
|
|
// slugMap[aSlug] = [aFile]
|
|
|
|
// }
|
|
|
|
// Note: [Future improvement] Resolve conflict
|
|
|
|
slugMap[aSlug] = aFile
|
|
|
|
})
|
|
|
|
|
|
|
|
const indexFile = "/🌎 Home.md"
|
|
|
|
slugMap['index'] = Node.getMarkdownFolder() + indexFile
|
|
|
|
slugMap['/'] = Node.getMarkdownFolder() + indexFile
|
|
|
|
|
|
|
|
return slugMap
|
2022-04-16 15:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function toSlug(filePath) {
|
2023-12-24 02:16:56 +00:00
|
|
|
const markdownFolder = Node.getMarkdownFolder()
|
|
|
|
const isFile = Node.isFile(filePath)
|
|
|
|
const isMarkdownFolder = filePath.includes(markdownFolder)
|
|
|
|
|
|
|
|
if (isFile && isMarkdownFolder) {
|
|
|
|
return filePath.replace(markdownFolder, '')
|
|
|
|
.replaceAll('/', '_')
|
2023-12-24 05:02:08 +00:00
|
|
|
.replaceAll(' ', '+')
|
|
|
|
.replaceAll('&', '-')
|
2023-12-24 02:16:56 +00:00
|
|
|
.replace('.md', '')
|
|
|
|
} else {
|
|
|
|
//TODO handle this properly
|
|
|
|
return '/'
|
|
|
|
}
|
2022-04-16 15:55:20 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-18 04:48:36 +00:00
|
|
|
|
2022-04-29 08:35:10 +00:00
|
|
|
export function constructGraphData() {
|
2023-12-26 02:48:51 +00:00
|
|
|
const t = new Transformer()
|
2023-12-24 02:16:56 +00:00
|
|
|
const filepath = path.join(process.cwd(), "graph-data.json");
|
|
|
|
if (Node.isFile(filepath)) {
|
|
|
|
const data = fs.readFileSync(filepath);
|
|
|
|
return JSON.parse(String(data))
|
|
|
|
} else {
|
|
|
|
const filePaths = getAllMarkdownFiles();
|
|
|
|
const edges = []
|
|
|
|
const nodes = []
|
|
|
|
filePaths
|
|
|
|
.forEach(aFilePath => {
|
|
|
|
// const {currentFilePath} = getFileNames(filename)
|
|
|
|
const aNode = {
|
2023-12-26 02:48:51 +00:00
|
|
|
title: t.parseFileNameFromPath(aFilePath),
|
2023-12-24 02:16:56 +00:00
|
|
|
slug: toSlug(aFilePath),
|
|
|
|
shortSummary: getShortSummary(toSlug(aFilePath))
|
|
|
|
}
|
|
|
|
nodes.push(aNode)
|
|
|
|
|
|
|
|
// console.log("Constructing graph for node: " + aFilePath )
|
2023-12-26 02:48:51 +00:00
|
|
|
const internalLinks = t.getInternalLinks(aFilePath)
|
2023-12-24 02:16:56 +00:00
|
|
|
internalLinks.forEach(aLink => {
|
|
|
|
|
|
|
|
if (aLink.slug === null || aLink.slug.length === 0) return
|
|
|
|
|
|
|
|
const anEdge = {
|
|
|
|
source: 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;
|
|
|
|
}
|
2020-11-30 11:29:34 +00:00
|
|
|
}
|
2020-11-28 15:45:01 +00:00
|
|
|
|
2022-04-07 09:52:41 +00:00
|
|
|
|
2022-04-18 04:48:36 +00:00
|
|
|
export function getLocalGraphData(currentNodeId) {
|
2023-12-26 02:48:51 +00:00
|
|
|
const t = new Transformer()
|
2023-12-24 02:16:56 +00:00
|
|
|
const { nodes, edges } = constructGraphData()
|
2022-04-07 09:52:41 +00:00
|
|
|
|
2023-12-24 02:16:56 +00:00
|
|
|
const newNodes = nodes.map(aNode => (
|
|
|
|
{
|
|
|
|
data: {
|
|
|
|
id: aNode.slug.toString(),
|
2023-12-26 02:48:51 +00:00
|
|
|
label: t.parseFileNameFromPath(toFilePath(aNode.slug)),
|
2023-12-24 02:16:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
))
|
2020-11-30 11:29:34 +00:00
|
|
|
|
2023-12-24 02:16:56 +00:00
|
|
|
const newEdges = edges.map(anEdge => ({
|
|
|
|
data: {
|
|
|
|
source: anEdge.source,
|
|
|
|
target: anEdge.target,
|
|
|
|
}
|
|
|
|
}))
|
2022-04-15 02:04:28 +00:00
|
|
|
|
2022-04-07 09:52:41 +00:00
|
|
|
|
2023-12-24 02:16:56 +00:00
|
|
|
const existingNodeIDs = newNodes.map(aNode => aNode.data.id)
|
|
|
|
currentNodeId = currentNodeId === 'index' ? '__index' : currentNodeId
|
|
|
|
if (currentNodeId != null && existingNodeIDs.includes(currentNodeId)) {
|
|
|
|
const outGoingNodeIds = newEdges
|
|
|
|
.filter(anEdge => anEdge.data.source === currentNodeId)
|
|
|
|
.map(anEdge => anEdge.data.target)
|
2022-04-15 04:00:45 +00:00
|
|
|
|
2023-12-24 02:16:56 +00:00
|
|
|
const incomingNodeIds = newEdges
|
|
|
|
.filter(anEdge => anEdge.data.target === currentNodeId)
|
|
|
|
.map(anEdge => anEdge.data.source)
|
2022-04-18 04:48:36 +00:00
|
|
|
|
2023-12-24 02:16:56 +00:00
|
|
|
outGoingNodeIds.push(currentNodeId)
|
2022-04-18 04:48:36 +00:00
|
|
|
|
2023-12-24 02:16:56 +00:00
|
|
|
const localNodeIds = incomingNodeIds.concat(outGoingNodeIds.filter(item => incomingNodeIds.indexOf(item) < 0))
|
|
|
|
if (localNodeIds.indexOf(currentNodeId) < 0) {
|
|
|
|
localNodeIds.push(currentNodeId)
|
|
|
|
}
|
2022-04-15 04:00:45 +00:00
|
|
|
|
2023-12-24 02:16:56 +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-24 02:16:56 +00:00
|
|
|
// Filter self-reference edges
|
|
|
|
localEdges = localEdges.filter(edge => edge.data.source !== edge.data.target)
|
2022-04-18 09:47:02 +00:00
|
|
|
|
2023-12-24 02:16:56 +00:00
|
|
|
// TODO: Find out why target ==='/' in some case
|
|
|
|
localEdges = localEdges.filter(edge => edge.data.target !== '/')
|
|
|
|
return {
|
|
|
|
nodes: localNodes,
|
|
|
|
edges: localEdges
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const filteredEdges = newEdges
|
|
|
|
.filter(edge => existingNodeIDs.includes(edge.data.source))
|
|
|
|
.filter(edge => existingNodeIDs.includes(edge.data.target))
|
|
|
|
|
|
|
|
return {
|
|
|
|
nodes: newNodes,
|
|
|
|
edges: filteredEdges
|
2022-04-15 02:04:28 +00:00
|
|
|
}
|
2023-12-24 02:16:56 +00:00
|
|
|
}
|
2022-04-15 04:00:45 +00:00
|
|
|
|
|
|
|
|
2020-11-28 15:45:01 +00:00
|
|
|
}
|
|
|
|
|
2022-04-17 13:04:58 +00:00
|
|
|
export function getAllSlugs() {
|
2023-12-24 02:16:56 +00:00
|
|
|
//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))
|
2022-03-23 03:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getDirectoryData() {
|
2023-12-24 02:16:56 +00:00
|
|
|
const filteredDirectory = dirTree(Node.getMarkdownFolder(), { extensions: /\.md/, exclude: [/\.git/, /\.obsidian/] });
|
|
|
|
return convertObject(filteredDirectory)
|
2022-03-23 03:50:06 +00:00
|
|
|
}
|
2022-04-07 09:52:41 +00:00
|
|
|
|
2022-03-23 03:50:06 +00:00
|
|
|
let _counter = 0;
|
2022-04-07 09:52:41 +00:00
|
|
|
|
2022-03-23 03:50:06 +00:00
|
|
|
export function convertObject(thisObject) {
|
2023-12-26 02:48:51 +00:00
|
|
|
const t = new Transformer()
|
2023-12-24 02:16:56 +00:00
|
|
|
const children = []
|
|
|
|
|
|
|
|
let routerPath = getAllSlugs().find(slug => {
|
2023-12-26 02:48:51 +00:00
|
|
|
const fileName = t.parseFileNameFromPath(toFilePath(slug))
|
|
|
|
return t.normalizeFileName(fileName) === t.normalizeFileName(thisObject.name)
|
2023-12-24 02:16:56 +00:00
|
|
|
}) || null
|
|
|
|
routerPath = routerPath ? '/note/' + routerPath : null
|
|
|
|
const newObject = {
|
|
|
|
name: thisObject.name,
|
|
|
|
children: children,
|
|
|
|
id: (_counter++).toString(),
|
|
|
|
routePath: routerPath || null
|
|
|
|
};
|
|
|
|
|
|
|
|
if (thisObject.children != null && thisObject.children.length > 0) {
|
|
|
|
thisObject.children.forEach(aChild => {
|
|
|
|
const newChild = convertObject(aChild)
|
|
|
|
children.push(newChild)
|
|
|
|
})
|
|
|
|
return newObject;
|
|
|
|
} else {
|
|
|
|
return newObject
|
|
|
|
}
|
2022-03-24 04:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function flat(array) {
|
2023-12-24 02:16:56 +00:00
|
|
|
var result = [];
|
|
|
|
array.forEach(function(a) {
|
|
|
|
result.push(a);
|
|
|
|
if (Array.isArray(a.children)) {
|
|
|
|
result = result.concat(flat(a.children));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
2022-03-24 04:15:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 09:52:41 +00:00
|
|
|
export function getFlattenArray(thisObject) {
|
2023-12-24 02:16:56 +00:00
|
|
|
return flat(thisObject.children)
|
|
|
|
}
|