XeNote/lib/utils.js

207 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'
export function getContent(filename) {
let { currentFilePath} = getFileNames(filename);
//console.log("currentFilePath: ", currentFilePath)
if (currentFilePath === undefined || currentFilePath == null) return null
return Node.readFileSync(currentFilePath)
}
export function getShortSummary(filename) {
const content = getContent(filename)
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
const dirTree = require("directory-tree");
2020-11-28 15:45:01 +00:00
const postsDirectory = path.join(process.cwd(), 'posts')
export function getAllFileNames() {
return Node.getFiles(postsDirectory).map(f => Transformer.parseFileNameFromPath(f))
}
export function getFileNames(filename) {
let filePaths = Node.getFiles(postsDirectory);
2020-12-01 03:28:42 +00:00
const fileNames = filePaths.map(f => Transformer.parseFileNameFromPath(f))
2020-12-06 19:40:20 +00:00
//console.log("\t filenames: ",fileNames, "\n")
2020-12-01 03:28:42 +00:00
// IF filename is not sidebar.md THEN Exclude sidebar.md from file list
let currentFilePath;
if (filename === "sidebar") {
2020-12-06 19:40:20 +00:00
//console.log(111)
2020-12-01 03:28:42 +00:00
currentFilePath = path.join(postsDirectory, "/sidebar.md")
} else if (filename === "index") {
2020-12-06 19:40:20 +00:00
//console.log(222)
2020-12-01 03:28:42 +00:00
currentFilePath = path.join(postsDirectory, "/index.md")
} else {
//TODO remove reference to index/sidebar md
2020-12-01 03:28:42 +00:00
filePaths = filePaths.filter(f => !(f.endsWith("sidebar.md") && f.endsWith("index.md")))
//console.log("\tDirectory is scanning to find corresponding filename")
currentFilePath = Transformer.pairCurrentFile(filename, filePaths)
//console.log("\tScan is finished. Founded filepath", currentFilePath, "\n")
}
return {fileNames, currentFilePath};
}
export function getSinglePost(filename) {
console.log("\n\nFile is scanning: ", filename)
// List of filenames that will provide existing links to wikilink
let {fileNames, currentFilePath} = getFileNames(filename);
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)
//console.log("\tFounded front matter data: ", currentFileFrontMatter, "\n")
// fileContent = Transformer.preprocessThreeDashes(fileContent)
//fileContent = fileContent.split("---").join("")
//console.log("filecontent end")
const [htmlContent] = Transformer.getHtmlContent(fileContent, {
fileNames: fileNames,
})
//console.log("hrmlcontents and backlinks")
return {
id: filename,
...currentFileFrontMatter,
data: htmlContent,
}
2020-11-30 11:29:34 +00:00
}
2020-11-28 15:45:01 +00:00
export function constructBackLinks() {
const filePaths = getContentPaths()
const edges = []
const nodes = []
2020-11-30 11:29:34 +00:00
filePaths.forEach( filename => {
const {currentFilePath, fileNames} = getFileNames(filename)
const internalLinks = Transformer.getInternalLinks(currentFilePath)
internalLinks.forEach(aLink => {
if (aLink.slug === null || aLink.slug.length === 0) return
const anEdge = {
source: filename,
target: aLink.slug,
}
edges.push(anEdge)
if (nodes.findIndex(aNode => aNode.slug === aLink.slug) === -1) {
aLink.shortSummary = getShortSummary(aLink.slug)
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() {
const backlinkData = constructBackLinks()
2020-11-30 11:29:34 +00:00
const elements = []
2020-11-30 11:29:34 +00:00
// First create Nodes
backlinkData.forEach(el => {
const node = {data: {id: el.id}};
2020-11-30 11:29:34 +00:00
if (el.title) {
node.data.title = el.title
}
if (el.description) {
node.data.description = el.description
}
elements.push(node)
2020-11-30 11:29:34 +00:00
}
)
2020-11-30 11:29:34 +00:00
// Second create Edges
backlinkData.forEach(el => {
// check if has any internal link
if (el.to.length > 0) {
2020-11-30 11:29:34 +00:00
// create edge from element to its links
el.to.forEach(linkElement => {
const edge = {
data: {
id: `${el.id}-${linkElement}`,
source: el.id,
target: linkElement
}
}
elements.push(edge)
})
}
})
2020-11-30 11:29:34 +00:00
return elements
2020-11-28 15:45:01 +00:00
}
export function getContentPaths() {
//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 => Transformer.parseFileNameFromPath(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 = getContentPaths().find(fileName => 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
}