XeNote/lib/utils.js

225 lines
6.3 KiB
JavaScript
Raw Normal View History

2020-11-28 15:45:01 +00:00
import path from 'path'
2022-03-24 04:55:12 +00:00
import {Node} from "./node"
import {Transformer} from "./transformer";
import unified from "unified";
import markdown from "remark-parse";
import {toString} from 'mdast-util-to-string'
const dirTree = require("directory-tree");
const postsDirectory = path.join(process.cwd(), 'posts')
export function getContent(slug) {
let currentFilePath = toFilePath(slug)
if (currentFilePath === undefined || currentFilePath == null) return null
return Node.readFileSync(currentFilePath)
}
export function getShortSummary(slug) {
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-03-23 03:50:06 +00:00
export function getAllMarkdownFiles() {
return Node.getFiles(postsDirectory)
}
export function getSinglePost(slug) {
console.log("\n\nFile is scanning: ", slug)
// List of filenames that will provide existing links to wikilink
let currentFilePath = slug !== "index" ? toFilePath(slug) : path.join(process.cwd(), 'posts') + "/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
const currentFileFrontMatter = Transformer.getFrontMatterData(fileContent)
const [htmlContent] = Transformer.getHtmlContent(fileContent)
//console.log("hrmlcontents and backlinks")
return {
id: slug,
...currentFileFrontMatter,
data: htmlContent,
}
2020-11-30 11:29:34 +00:00
}
2020-11-28 15:45:01 +00:00
export function toFilePath(slug) {
// Construct file name from slug of /notes/abcxyz
let filePath ;
if (slug === '/') {
filePath = path.join(process.cwd(), 'posts') + "/index.md"
} else {
filePath = postsDirectory + slug
.replaceAll('__','/')
.replaceAll('--',' ')
.replaceAll('ambersand','&')
+ ".md";
}
if (Node.isFile(filePath)) {
return filePath
} else {
return null
}
}
export function toSlug(filePath) {
// Convert File Path to unique slug
// let result = filePath.replace('/','__').replace(' ','-').replace('.md', '')
return filePath.replace(postsDirectory, '')
.replaceAll('/','__')
.replaceAll(' ','--')
.replaceAll('&','ambersand')
.replace('.md', '')
}
export function constructBackLinks() {
const filePaths = getAllMarkdownFiles();
const edges = []
const nodes = []
2020-11-30 11:29:34 +00:00
filePaths.forEach(filename => {
// const {currentFilePath} = getFileNames(filename)
const internalLinks = Transformer.getInternalLinks(filename)
internalLinks.forEach(aLink => {
if (aLink.slug === null || aLink.slug.length === 0) return
const anEdge = {
source: toSlug(filename),
target: aLink.slug,
}
edges.push(anEdge)
if (nodes.findIndex(aNode => aNode.slug === aLink.slug) === -1) {
aLink.shortSummary = getShortSummary(aLink.slug)
2022-04-18 00:58:20 +00:00
// console.log(aLink.shortSummary)
nodes.push(aLink)
}
2020-11-30 11:29:34 +00:00
})
}
)
2020-11-30 11:29:34 +00:00
return {nodes, edges};
2020-11-30 11:29:34 +00:00
}
2020-11-28 15:45:01 +00:00
export function getGraphData(currentNodeId) {
2020-11-30 11:29:34 +00:00
const {nodes, edges} = constructBackLinks()
const newNodes = nodes.map(aNode => (
{
data: {
id: aNode.slug.toString(),
2022-04-17 14:01:13 +00:00
label: Transformer.parseFileNameFromPath(toFilePath(aNode.slug)),
}
}
))
2020-11-30 11:29:34 +00:00
const newEdges = edges.map(anEdge => ({
data: {
source: anEdge.source,
target: anEdge.target,
2020-11-30 11:29:34 +00:00
}
}))
const existingNodeIDs = newNodes.map(aNode => aNode.data.id)
if (currentNodeId != null && existingNodeIDs.includes(currentNodeId)) {
const localNodeIDs = newEdges
.filter(anEdge => anEdge.data.source === currentNodeId)
.map(anEdge => anEdge.data.target)
localNodeIDs.push(currentNodeId)
const localNodes = newNodes.filter(aNode => localNodeIDs.includes(aNode.data.id))
const localEdges = newEdges.filter(edge => localNodeIDs.includes(edge.data.source)).filter(edge => localNodeIDs.includes(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
}
}
2020-11-28 15:45:01 +00:00
}
export function getAllSlugs() {
//console.log("\n\nAll Posts are scanning")
// Get file names under /posts
const filePaths = Node.getFiles(postsDirectory).filter(f => !(f.endsWith("index") || f.endsWith("sidebar")))
return filePaths.map(f => toSlug(f))
2022-03-23 03:50:06 +00:00
}
export function getDirectoryData() {
const filteredDirectory = dirTree(postsDirectory, {extensions: /\.md/});
return convertObject(filteredDirectory)
2022-03-23 03:50:06 +00:00
}
2022-03-23 03:50:06 +00:00
let _counter = 0;
2022-03-23 03:50:06 +00:00
export function convertObject(thisObject) {
const children = []
let routerPath = getAllSlugs().find(slug => {
const fileName = Transformer.parseFileNameFromPath(toFilePath(slug))
return Transformer.normalizeFileName(fileName) === Transformer.normalizeFileName(thisObject.name)
}) || null
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
}
}
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;
}
export function getFlattenArray(thisObject) {
return flat(thisObject.children)
2020-11-28 15:45:01 +00:00
}