2022-03-24 04:55:12 +00:00
|
|
|
import {Node} from "./node"
|
|
|
|
import {Transformer} from "./transformer";
|
2022-04-08 07:44:45 +00:00
|
|
|
import unified from "unified";
|
|
|
|
import markdown from "remark-parse";
|
|
|
|
import {toString} from 'mdast-util-to-string'
|
|
|
|
|
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) {
|
|
|
|
let currentFilePath = toFilePath(slug)
|
2022-04-08 07:44:45 +00:00
|
|
|
if (currentFilePath === undefined || currentFilePath == null) return null
|
2022-04-15 02:04:28 +00:00
|
|
|
return Node.readFileSync(currentFilePath)
|
2022-04-08 07:44:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-17 13:04:58 +00:00
|
|
|
export function getShortSummary(slug) {
|
|
|
|
const content = getContent(slug)
|
2022-04-08 07:44:45 +00:00
|
|
|
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-03-23 03:50:06 +00:00
|
|
|
|
2022-04-17 13:04:58 +00:00
|
|
|
export function getAllMarkdownFiles() {
|
2022-04-18 01:58:37 +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
|
|
|
|
2022-04-07 09:52:41 +00:00
|
|
|
|
|
|
|
// List of filenames that will provide existing links to wikilink
|
2022-04-18 01:58:37 +00:00
|
|
|
let currentFilePath = slug !== "index" ? toFilePath(slug) : Node.getMarkdownFolder() + "/index.md"
|
2020-12-06 19:40:20 +00:00
|
|
|
//console.log("currentFilePath: ", currentFilePath)
|
2020-11-30 11:29:34 +00:00
|
|
|
|
|
|
|
var fileContent = Node.readFileSync(currentFilePath)
|
2020-11-28 15:45:01 +00:00
|
|
|
|
2022-04-07 09:52:41 +00:00
|
|
|
const currentFileFrontMatter = Transformer.getFrontMatterData(fileContent)
|
2022-04-18 04:48:36 +00:00
|
|
|
// console.log("===============\n\nFile is scanning: ", slug)
|
2022-04-16 15:55:20 +00:00
|
|
|
const [htmlContent] = Transformer.getHtmlContent(fileContent)
|
2022-04-18 04:48:36 +00:00
|
|
|
// console.log("==================================")
|
2022-04-07 09:52:41 +00:00
|
|
|
//console.log("hrmlcontents and backlinks")
|
|
|
|
return {
|
2022-04-17 13:04:58 +00:00
|
|
|
id: slug,
|
2022-04-07 09:52:41 +00:00
|
|
|
...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-16 15:55:20 +00:00
|
|
|
export function toFilePath(slug) {
|
|
|
|
// Construct file name from slug of /notes/abcxyz
|
2022-04-18 04:48:36 +00:00
|
|
|
let filePath;
|
2022-04-17 13:04:58 +00:00
|
|
|
|
|
|
|
if (slug === '/') {
|
2022-04-18 04:48:36 +00:00
|
|
|
filePath = Node.getMarkdownFolder() + "/index.md"
|
|
|
|
} else {
|
2022-04-18 01:58:37 +00:00
|
|
|
filePath = Node.getMarkdownFolder() + slug
|
2022-04-18 04:48:36 +00:00
|
|
|
.replaceAll('__', '/')
|
|
|
|
.replaceAll('--', ' ')
|
|
|
|
.replaceAll('ambersand', '&')
|
2022-04-17 13:04:58 +00:00
|
|
|
+ ".md";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Node.isFile(filePath)) {
|
|
|
|
return filePath
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
2022-04-16 15:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function toSlug(filePath) {
|
|
|
|
|
2022-04-18 01:58:37 +00:00
|
|
|
if (Node.isFile(filePath) && filePath.includes(Node.getMarkdownFolder())) {
|
|
|
|
return filePath.replace(Node.getMarkdownFolder(), '')
|
2022-04-18 04:48:36 +00:00
|
|
|
.replaceAll('/', '__')
|
|
|
|
.replaceAll(' ', '--')
|
|
|
|
.replaceAll('&', 'ambersand')
|
2022-04-18 01:10:19 +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
|
|
|
export function constructGraphData() {
|
2022-04-07 09:52:41 +00:00
|
|
|
|
2022-04-17 13:04:58 +00:00
|
|
|
const filePaths = getAllMarkdownFiles();
|
2022-04-07 09:52:41 +00:00
|
|
|
const edges = []
|
|
|
|
const nodes = []
|
2020-11-30 11:29:34 +00:00
|
|
|
|
2022-04-18 04:48:36 +00:00
|
|
|
filePaths
|
|
|
|
.forEach(aFilePath => {
|
2022-04-17 13:04:58 +00:00
|
|
|
// const {currentFilePath} = getFileNames(filename)
|
2022-04-18 04:48:36 +00:00
|
|
|
const aNode = {
|
|
|
|
slug: toSlug(aFilePath),
|
|
|
|
shortSummary: getShortSummary(toSlug(aFilePath))
|
|
|
|
}
|
|
|
|
nodes.push(aNode)
|
|
|
|
|
|
|
|
console.log("Constructing graph for node: " + aFilePath )
|
|
|
|
const internalLinks = Transformer.getInternalLinks(aFilePath)
|
2022-04-07 09:52:41 +00:00
|
|
|
internalLinks.forEach(aLink => {
|
|
|
|
|
|
|
|
if (aLink.slug === null || aLink.slug.length === 0) return
|
|
|
|
|
|
|
|
const anEdge = {
|
2022-04-18 04:48:36 +00:00
|
|
|
source: toSlug(aFilePath),
|
2022-04-07 09:52:41 +00:00
|
|
|
target: aLink.slug,
|
|
|
|
}
|
|
|
|
edges.push(anEdge)
|
2022-04-18 04:48:36 +00:00
|
|
|
console.log("Source: " + anEdge.source)
|
|
|
|
console.log("Target: " + anEdge.target)
|
2020-11-30 11:29:34 +00:00
|
|
|
})
|
2022-04-18 04:48:36 +00:00
|
|
|
console.log("==============Constructing graph" )
|
2022-04-07 09:52:41 +00:00
|
|
|
}
|
|
|
|
)
|
2020-11-30 11:29:34 +00:00
|
|
|
|
2022-04-07 09:52:41 +00:00
|
|
|
return {nodes, edges};
|
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) {
|
2020-11-30 11:29:34 +00:00
|
|
|
|
2022-04-18 04:48:36 +00:00
|
|
|
const {nodes, edges} = constructGraphData()
|
2022-04-07 09:52:41 +00:00
|
|
|
|
2022-04-15 02:04:28 +00:00
|
|
|
const newNodes = nodes.map(aNode => (
|
2022-04-15 04:00:45 +00:00
|
|
|
{
|
|
|
|
data: {
|
|
|
|
id: aNode.slug.toString(),
|
2022-04-17 14:01:13 +00:00
|
|
|
label: Transformer.parseFileNameFromPath(toFilePath(aNode.slug)),
|
2022-04-15 04:00:45 +00:00
|
|
|
}
|
2022-04-15 02:04:28 +00:00
|
|
|
}
|
|
|
|
))
|
2020-11-30 11:29:34 +00:00
|
|
|
|
2022-04-15 02:04:28 +00:00
|
|
|
const newEdges = edges.map(anEdge => ({
|
|
|
|
data: {
|
|
|
|
source: anEdge.source,
|
|
|
|
target: anEdge.target,
|
2020-11-30 11:29:34 +00:00
|
|
|
}
|
2022-04-15 02:04:28 +00:00
|
|
|
}))
|
|
|
|
|
2022-04-07 09:52:41 +00:00
|
|
|
|
2022-04-15 04:00:45 +00:00
|
|
|
const existingNodeIDs = newNodes.map(aNode => aNode.data.id)
|
2022-04-18 04:48:36 +00:00
|
|
|
currentNodeId = currentNodeId === 'index' ? '__index' : currentNodeId
|
2022-04-15 04:00:45 +00:00
|
|
|
if (currentNodeId != null && existingNodeIDs.includes(currentNodeId)) {
|
2022-04-18 04:48:36 +00:00
|
|
|
const outGoingNodeIds = newEdges
|
2022-04-15 04:00:45 +00:00
|
|
|
.filter(anEdge => anEdge.data.source === currentNodeId)
|
|
|
|
.map(anEdge => anEdge.data.target)
|
|
|
|
|
2022-04-18 04:48:36 +00:00
|
|
|
const incomingNodeIds = newEdges
|
|
|
|
.filter(anEdge => anEdge.data.target === currentNodeId)
|
|
|
|
.map(anEdge => anEdge.data.source)
|
|
|
|
|
|
|
|
outGoingNodeIds.push(currentNodeId)
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-04-18 04:48:36 +00:00
|
|
|
const localNodes = newNodes.filter(aNode => localNodeIds.includes(aNode.data.id))
|
|
|
|
var localEdges = newEdges.filter(edge => localNodeIds.includes(edge.data.source)).filter(edge => localNodeIds.includes(edge.data.target))
|
2022-04-15 04:00:45 +00:00
|
|
|
|
2022-04-18 04:48:36 +00:00
|
|
|
// Filter self-reference edges
|
|
|
|
localEdges = localEdges.filter(edge => edge.data.source !== edge.data.target)
|
2022-04-15 04:00:45 +00:00
|
|
|
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
|
|
|
}
|
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() {
|
2022-04-07 09:52:41 +00:00
|
|
|
//console.log("\n\nAll Posts are scanning")
|
|
|
|
// Get file names under /posts
|
2022-04-18 01:58:37 +00:00
|
|
|
const filePaths = Node.getFiles(Node.getMarkdownFolder()).filter(f => !(f.endsWith("index") || f.endsWith("sidebar")))
|
2022-04-16 15:55:20 +00:00
|
|
|
return filePaths.map(f => toSlug(f))
|
2022-03-23 03:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getDirectoryData() {
|
2022-04-18 01:58:37 +00:00
|
|
|
const filteredDirectory = dirTree(Node.getMarkdownFolder(), {extensions: /\.md/});
|
2022-04-07 09:52:41 +00:00
|
|
|
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) {
|
|
|
|
const children = []
|
2022-03-24 04:15:43 +00:00
|
|
|
|
2022-04-17 13:04:58 +00:00
|
|
|
let routerPath = getAllSlugs().find(slug => {
|
|
|
|
const fileName = Transformer.parseFileNameFromPath(toFilePath(slug))
|
2022-04-18 04:48:36 +00:00
|
|
|
return Transformer.normalizeFileName(fileName) === Transformer.normalizeFileName(thisObject.name)
|
2022-04-17 13:04:58 +00:00
|
|
|
}) || null
|
2022-04-07 09:52:41 +00:00
|
|
|
routerPath = routerPath ? '/note/' + routerPath : null
|
|
|
|
const newObject = {
|
|
|
|
name: thisObject.name,
|
|
|
|
children: children,
|
|
|
|
id: (_counter++).toString(),
|
|
|
|
routePath: routerPath || null
|
|
|
|
};
|
2022-03-23 03:50:06 +00:00
|
|
|
|
|
|
|
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) {
|
|
|
|
var result = [];
|
|
|
|
array.forEach(function (a) {
|
|
|
|
result.push(a);
|
|
|
|
if (Array.isArray(a.children)) {
|
|
|
|
result = result.concat(flat(a.children));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-04-07 09:52:41 +00:00
|
|
|
export function getFlattenArray(thisObject) {
|
2022-03-24 04:15:43 +00:00
|
|
|
return flat(thisObject.children)
|
2020-11-28 15:45:01 +00:00
|
|
|
}
|