XeNote/lib/transformer.js
Tuan Cao 2bb96504c9 Finished constructing backlink from internal links
TODO: performance improvement, and code reability.
2022-04-07 16:52:41 +07:00

176 lines
6.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import matter from 'gray-matter'
import path from 'path'
import fs from "fs"
import unified from "unified";
import markdown from "remark-parse";
import {wikiLinkPlugin} from "remark-wiki-link";
import html from "remark-html";
import frontmatter from "remark-frontmatter";
import externalLinks from "remark-external-links";
import highlight from "remark-highlight.js";
import {Node} from "./node";
const postsDirectory = path.join(process.cwd(), 'posts')
const isFile = fileName => {
return fs.lstatSync(fileName).isFile()
}
export const Transformer = {
haveFrontMatter: function (content) {
//console.log("\t Front matter data content", content)
if (!content) return false
const indexOfFirst = content.indexOf("---");
//console.log("\t Front matter data firstIndex ", indexOfFirst)
//console.log("index first", indexOfFirst)
if (indexOfFirst === -1) {
return false
}
let indexOfSecond = content.indexOf("---", (indexOfFirst + 1));
return indexOfSecond !== -1;
},
getFrontMatterData: function (filecontent) {
if (Transformer.haveFrontMatter(filecontent)) {
return matter(filecontent).data
}
return {}
},
getHtmlContent: function (content, {fileNames}) {
let htmlContent = []
const sanitizedContent = Transformer.preprocessThreeDashes(content)
unified()
.use(markdown, {gfm: true})
.use(highlight)
.use(externalLinks, {target: "_blank", rel: ['noopener']})
.use(frontmatter, ['yaml', 'toml'])
.use(wikiLinkPlugin, {
permalinks: null,
pageResolver: function (pageName) {
return [Transformer.parseFileNameFromPath(pageName)]
},
hrefTemplate: function (permalink) {
permalink = Transformer.normalizeFileName(permalink)
permalink = permalink.replace("ç", "c").replace("ı", "i").replace("ş", "s")
return `/note/${permalink}`
},
aliasDivider: "|"
})
.use(html)
.process(sanitizedContent,
function (err, file) {
htmlContent.push(String(file).replace("\n", ""))
}
)
htmlContent = htmlContent.join("")
htmlContent = htmlContent.split("---")
return [htmlContent]
},
/* SANITIZE MARKDOWN FOR --- */
preprocessThreeDashes: function (content) {
const indexOfFirst = content.indexOf("---");
if (indexOfFirst === -1) {
return content
}
const indexOfSecond = content.indexOf("---", (indexOfFirst + 1));
const frontPart = content.slice(0, indexOfSecond);
const contentPart = content.slice(indexOfSecond);
const processedContent = contentPart.split("---").join("")
//console.log("preprocess", indexOfFirst, indexOfSecond)
//return frontPart.concat(processedContent)
return processedContent
},
/* Normalize File Names */
normalizeFileName: function (filename) {
let processedFileName = filename.replace(".md", "");
processedFileName = processedFileName.replace('(', '').replace(')', '')
processedFileName = processedFileName.split(" ").join("-")
// processedFileName = processedFileName.toLowerCase()
const conversionLetters = [
["ç", "c"], ["ş", "s"], ["ı", "i"], ["ü", "u"], ["ö", "o"], ["ğ", "g"],
["Ç", "C"], ["Ş", "S"], ["İ", "I"], ["Ü", "U"], ["Ö", "O"], ["Ğ", "G"]
];
conversionLetters.forEach(letterPair => {
processedFileName = processedFileName.split(letterPair[0]).join(letterPair[1])
//processedFileName = processedFileName.replace(letterPair[0], letterPair[1])
}
)
//console.log("filename", processedFileName)
return processedFileName
},
/* Parse file name from path then sanitize it */
parseFileNameFromPath: function (filepath) {
const parsedFileFromPath = filepath.split("/")[filepath.split("/").length - 1]
const parsedFileName = parsedFileFromPath.replace(".md", "")
return Transformer.normalizeFileName(parsedFileName)
},
/* Pair provided and existing Filenames*/
pairCurrentFile: function (provided, listOfFilePaths) {
//console.log(provided, ListOfFilePaths)
const providedSanitizedFileName = Transformer.normalizeFileName(provided);
// Map file paths and return true if it pairs with provided
const possibleFilePath = listOfFilePaths.filter(possibleFilePath => {
const possibleFileName = Transformer.parseFileNameFromPath(possibleFilePath);
const possibleSanitizedFileName = Transformer.normalizeFileName(possibleFileName)
//console.log("----", providedSanitizedFileName, possibleSanitizedFileName)
//console.log("---", possibleSanitizedFileName, providedSanitizedFileName)
return providedSanitizedFileName === possibleSanitizedFileName;
})
console.log("p---", possibleFilePath)
return possibleFilePath[0]
},
getInternalLinks: function (aFilePath) {
const filePaths = Node.getFiles(postsDirectory);
const currentFilePath = Transformer.pairCurrentFile(aFilePath, filePaths)
const fileContent = Node.readFileSync(aFilePath);
const internalLinks = []
const sanitizedContent = Transformer.preprocessThreeDashes(fileContent)
unified()
.use(markdown, {gfm: true})
.use(wikiLinkPlugin, {
pageResolver: function (pageName) {
// let name = [Transformer.parseFileNameFromPath(pageName)];
let slug = Transformer.parseFileNameFromPath(pageName);
if (slug.includes('#')) {
console.log(slug)
}
const canonicalSlug = slug.includes('#') ? slug.split('#')[0] : slug
const backLink = {
title: canonicalSlug,
slug: canonicalSlug,
shortSummary: canonicalSlug
}
if (canonicalSlug != null && internalLinks.find(aLink => aLink.slug === canonicalSlug ) == null) {
internalLinks.push(backLink);
}
return [canonicalSlug]
}
,
hrefTemplate: function (permalink) {
permalink = Transformer.normalizeFileName(permalink)
permalink = permalink.replace("ç", "c").replace("ı", "i").replace("ş", "s")
return `/note/${permalink}`
},
aliasDivider: "|"
})
.use(html)
.processSync(sanitizedContent)
return internalLinks;
}
}