XeNote/lib/post.js

176 lines
5.9 KiB
JavaScript
Raw Normal View History

2020-11-28 15:45:01 +00:00
import path from 'path'
2020-11-28 16:36:28 +00:00
import matter, { test } from 'gray-matter'
2020-11-28 15:45:01 +00:00
import fs from "fs"
import { Node } from "./node"
2020-11-30 11:29:34 +00:00
import { Transformer } from "./transformer";
2020-11-28 15:45:01 +00:00
import BiMap from "bimap";
2020-12-01 03:28:42 +00:00
function pathSelector(filename, allFilePaths){
if (filename.replace(".md", "") === "index"){
return "/index.md"
}
else if (filename.replace(".md", "") === "sidebar"){
return "/sidebar.md"
}
const editedFilePaths = allFilePaths.filter(f => !(f.endsWith("index.md") && f.endsWith("sidebar.md") ))
return editedFilePaths
}
2020-11-28 15:45:01 +00:00
const postsDirectory = path.join(process.cwd(), 'posts')
2020-11-30 11:29:34 +00:00
export function getSinglePost(filename) {
2020-12-06 19:40:20 +00:00
console.log("\n\nFile is scanning: ", filename)
2020-11-28 16:36:28 +00:00
2020-12-01 03:28:42 +00:00
// List of filenames that will provide existing links to wikilink
var filePaths = Node.getFiles(postsDirectory)
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
var 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 {
2020-12-06 19:40:20 +00:00
//console.log(333)
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")
}
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
2020-11-30 11:29:34 +00:00
const currentFileFrontMatter = Transformer.getFrontMatterData(fileContent)
//console.log("\tFounded front matter data: ", currentFileFrontMatter, "\n")
// fileContent = Transformer.preprocessThreeDashes(fileContent)
2020-11-30 11:29:34 +00:00
//fileContent = fileContent.split("---").join("")
//console.log("filecontent end")
2020-11-28 15:45:01 +00:00
2020-11-30 11:29:34 +00:00
const [htmlContent, backlinks] = Transformer.getHtmlContent(fileContent, {
2020-11-28 15:45:01 +00:00
fileNames:fileNames,
})
2020-11-30 11:29:34 +00:00
//console.log("hrmlcontents and backlinks")
2020-11-28 15:45:01 +00:00
return {
id:filename,
2020-11-30 11:29:34 +00:00
...currentFileFrontMatter,
2020-11-28 15:45:01 +00:00
data:htmlContent
}
}
export function getAllBacklinks(){
2020-11-30 11:29:34 +00:00
//console.log("\n\nBacklinks are scanning")
2020-11-28 15:45:01 +00:00
//var bimap = new BiMap
2020-11-30 11:29:34 +00:00
var internalLinks = []
2020-11-28 15:45:01 +00:00
// Get file names under /posts
2020-12-01 03:28:42 +00:00
const filePaths = Node.getFiles(postsDirectory).filter(f => !f.endsWith("sidebar.md"))
2020-11-30 11:29:34 +00:00
const fileNames = filePaths.map(f => Transformer.parseFileNameFromPath(f))
//console.log("\tFounded filePaths: ", fileNames)
2020-11-28 15:45:01 +00:00
var allBacklinkData = filePaths.map(fileName => {
// Remove ".md" from file name to get id
2020-11-30 11:29:34 +00:00
const slug = Transformer.parseFileNameFromPath(fileName)
//console.log("filename", fileNames)
const fileData = {
id:slug
}
2020-11-28 15:45:01 +00:00
//console.log("AllBacklinks slug", slug)
// Read markdown file as string
2020-11-30 11:29:34 +00:00
var fileContent = Node.readFileSync(fileName, 'utf8')
const frontmatterData = Transformer.getFrontMatterData(fileContent)
const requiredParameters = ["title", "description"]
requiredParameters.forEach(param => {
if (frontmatterData[param])
fileData[param] = frontmatterData[param]
})
//fileContent = fileContent.split("---").join("")
const [htmlContent, backlinks] = Transformer.getHtmlContent(fileContent, {
2020-11-28 15:45:01 +00:00
fileNames:fileNames,
2020-11-30 11:29:34 +00:00
})
// Check if scanned slug post has any internal links
const existingInternalLink = backlinks.filter(bl => fileNames.includes(bl))
fileData.to = existingInternalLink
fileData.href = slug === "index" ? "/" : `/note/${slug}`
//console.log("\n\nbacklinks",[ slug, [backlinks]] )
//bimap.push(slug, backlinks)
// Check if internal link exists
//const internalLinks = backlinks.filter(bl => fileNames.includes(bl))
internalLinks.push(fileData)
//console.log("bimap: ", bimap.key(slug))
// Combine the data with the slug
//return backlinkList.length > 0 ? JSON.stringify(backlinkList) : null
})
//console.log("founded internal links for ", internalLinks)
//console.log("\n\ninternal list: ", internalLinks)
return internalLinks
//return [allBacklinkData.filter(bl => bl !== null), JSON.stringify(fileNames)]
}
2020-11-28 15:45:01 +00:00
2020-11-30 11:29:34 +00:00
export function getGraphData(){
const backlinkData = getAllBacklinks()
const elements = []
// First create Nodes
backlinkData.forEach(el => {
const node = {data: {id: el.id}};
if(el.title){
node.data.title = el.title
}
if (el.description){
node.data.description = el.description
}
elements.push(node)
}
)
// Second create Edges
backlinkData.forEach(el => {
// check if has any internal link
if (el.to.length > 0){
// 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)
})
}
})
return elements
2020-11-28 15:45:01 +00:00
}
export function getPostListData() {
2020-11-30 11:29:34 +00:00
//console.log("\n\nAll Posts are scanning")
// Get file names under /posts
2020-12-01 03:28:42 +00:00
const filePaths = Node.getFiles(postsDirectory).filter(f => !(f.endsWith("index") || f.endsWith("sidebar")))
2020-11-30 11:29:34 +00:00
const fileNames = filePaths.map(f => Transformer.parseFileNameFromPath(f))
//console.log("filePaths", filePaths)
2020-11-28 15:45:01 +00:00
2020-12-01 03:28:42 +00:00
return fileNames
2020-11-28 15:45:01 +00:00
}