2020-11-30 11:29:34 +00:00
|
|
|
|
import matter from 'gray-matter'
|
2022-04-05 03:45:30 +00:00
|
|
|
|
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";
|
2022-04-07 09:52:41 +00:00
|
|
|
|
import {Node} from "./node";
|
2022-04-17 13:04:58 +00:00
|
|
|
|
import {getAllMarkdownFiles, toFilePath, toSlug} from "./utils";
|
2020-11-30 11:29:34 +00:00
|
|
|
|
|
|
|
|
|
export const Transformer = {
|
2022-04-05 04:47:41 +00:00
|
|
|
|
haveFrontMatter: function (content) {
|
2020-11-30 11:29:34 +00:00
|
|
|
|
//console.log("\t Front matter data content", content)
|
|
|
|
|
if (!content) return false
|
2022-04-07 09:52:41 +00:00
|
|
|
|
const indexOfFirst = content.indexOf("---");
|
2020-11-30 11:29:34 +00:00
|
|
|
|
//console.log("\t Front matter data firstIndex ", indexOfFirst)
|
|
|
|
|
//console.log("index first", indexOfFirst)
|
2022-04-05 04:47:41 +00:00
|
|
|
|
if (indexOfFirst === -1) {
|
2020-11-30 11:29:34 +00:00
|
|
|
|
return false
|
|
|
|
|
}
|
2022-04-07 03:44:02 +00:00
|
|
|
|
let indexOfSecond = content.indexOf("---", (indexOfFirst + 1));
|
|
|
|
|
return indexOfSecond !== -1;
|
|
|
|
|
|
2020-11-30 11:29:34 +00:00
|
|
|
|
},
|
2022-04-05 04:47:41 +00:00
|
|
|
|
getFrontMatterData: function (filecontent) {
|
|
|
|
|
if (Transformer.haveFrontMatter(filecontent)) {
|
2020-11-30 11:29:34 +00:00
|
|
|
|
return matter(filecontent).data
|
|
|
|
|
}
|
|
|
|
|
return {}
|
|
|
|
|
},
|
|
|
|
|
|
2022-04-16 15:55:20 +00:00
|
|
|
|
|
|
|
|
|
pageResolver: function (pageName) {
|
2022-04-17 13:04:58 +00:00
|
|
|
|
const allFileNames = getAllMarkdownFiles()
|
2022-04-16 15:55:20 +00:00
|
|
|
|
const result = allFileNames.find(aFile => {
|
|
|
|
|
let parseFileNameFromPath = Transformer.parseFileNameFromPath(aFile);
|
2022-04-17 13:04:58 +00:00
|
|
|
|
return Transformer.normalizeFileName(parseFileNameFromPath) === Transformer.normalizeFileName(pageName)
|
2022-04-16 15:55:20 +00:00
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// permalink = permalink.replace("ç", "c").replace("ı", "i").replace("ş", "s")
|
|
|
|
|
// console.log(`/note/${toSlug(result)}`)
|
|
|
|
|
if (result === undefined || result.length === 0) {
|
|
|
|
|
console.log("Cannot resolve file path " + pageName)
|
|
|
|
|
}
|
|
|
|
|
return (result !== undefined && result.length > 0) ? [toSlug(result)] : ["/"]
|
|
|
|
|
},
|
|
|
|
|
hrefTemplate: function (permalink) {
|
2022-04-17 13:04:58 +00:00
|
|
|
|
// permalink = Transformer.normalizeFileName(permalink)
|
2022-04-16 15:55:20 +00:00
|
|
|
|
permalink = permalink.replace("ç", "c").replace("ı", "i").replace("ş", "s")
|
|
|
|
|
return `/note/${permalink}`;
|
|
|
|
|
}, getHtmlContent: function (content) {
|
2020-11-30 11:29:34 +00:00
|
|
|
|
let htmlContent = []
|
2022-04-07 03:44:02 +00:00
|
|
|
|
const sanitizedContent = Transformer.preprocessThreeDashes(content)
|
|
|
|
|
|
|
|
|
|
unified()
|
2022-04-05 04:47:41 +00:00
|
|
|
|
.use(markdown, {gfm: true})
|
2020-11-30 11:29:34 +00:00
|
|
|
|
.use(highlight)
|
2021-03-03 21:33:38 +00:00
|
|
|
|
.use(externalLinks, {target: "_blank", rel: ['noopener']})
|
2020-11-30 11:29:34 +00:00
|
|
|
|
.use(frontmatter, ['yaml', 'toml'])
|
|
|
|
|
.use(wikiLinkPlugin, {
|
2022-04-07 09:52:41 +00:00
|
|
|
|
permalinks: null,
|
|
|
|
|
pageResolver: function (pageName) {
|
2022-04-16 15:55:20 +00:00
|
|
|
|
return Transformer.pageResolver(pageName)
|
2022-04-07 03:44:02 +00:00
|
|
|
|
},
|
2022-04-07 09:52:41 +00:00
|
|
|
|
hrefTemplate: function (permalink) {
|
2022-04-16 15:55:20 +00:00
|
|
|
|
return Transformer.hrefTemplate(permalink);
|
2022-04-07 03:44:02 +00:00
|
|
|
|
},
|
2022-03-16 05:01:01 +00:00
|
|
|
|
|
2022-04-07 09:52:41 +00:00
|
|
|
|
aliasDivider: "|"
|
2022-04-07 03:44:02 +00:00
|
|
|
|
})
|
|
|
|
|
.use(html)
|
2020-11-30 11:29:34 +00:00
|
|
|
|
.process(sanitizedContent,
|
|
|
|
|
function (err, file) {
|
2022-04-07 09:52:41 +00:00
|
|
|
|
htmlContent.push(String(file).replace("\n", ""))
|
2022-04-16 15:55:20 +00:00
|
|
|
|
if (err) {
|
|
|
|
|
console.log("ERRROR:" + err)
|
|
|
|
|
}
|
2022-04-07 09:52:41 +00:00
|
|
|
|
}
|
2020-11-30 11:29:34 +00:00
|
|
|
|
)
|
2022-04-07 09:52:41 +00:00
|
|
|
|
htmlContent = htmlContent.join("")
|
2020-11-30 11:29:34 +00:00
|
|
|
|
htmlContent = htmlContent.split("---")
|
2022-04-07 09:52:41 +00:00
|
|
|
|
return [htmlContent]
|
2020-11-30 11:29:34 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* SANITIZE MARKDOWN FOR --- */
|
2022-04-05 04:47:41 +00:00
|
|
|
|
preprocessThreeDashes: function (content) {
|
2022-04-07 09:52:41 +00:00
|
|
|
|
const indexOfFirst = content.indexOf("---");
|
2022-04-05 04:47:41 +00:00
|
|
|
|
if (indexOfFirst === -1) {
|
2020-11-30 11:29:34 +00:00
|
|
|
|
return content
|
|
|
|
|
}
|
2022-04-07 09:52:41 +00:00
|
|
|
|
const indexOfSecond = content.indexOf("---", (indexOfFirst + 1));
|
2022-04-17 13:04:58 +00:00
|
|
|
|
content.slice(0, indexOfSecond);
|
2020-11-30 11:29:34 +00:00
|
|
|
|
const contentPart = content.slice(indexOfSecond);
|
2022-04-17 13:04:58 +00:00
|
|
|
|
return contentPart.split("---").join("")
|
2020-11-30 11:29:34 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* Normalize File Names */
|
2022-04-05 04:47:41 +00:00
|
|
|
|
normalizeFileName: function (filename) {
|
2022-04-07 09:52:41 +00:00
|
|
|
|
let processedFileName = filename.replace(".md", "");
|
2020-12-01 03:28:42 +00:00
|
|
|
|
processedFileName = processedFileName.replace('(', '').replace(')', '')
|
2020-11-30 11:29:34 +00:00
|
|
|
|
processedFileName = processedFileName.split(" ").join("-")
|
2022-04-17 13:04:58 +00:00
|
|
|
|
processedFileName = processedFileName.toLowerCase()
|
2020-12-01 03:28:42 +00:00
|
|
|
|
const conversionLetters = [
|
2022-04-05 04:47:41 +00:00
|
|
|
|
["ç", "c"], ["ş", "s"], ["ı", "i"], ["ü", "u"], ["ö", "o"], ["ğ", "g"],
|
|
|
|
|
["Ç", "C"], ["Ş", "S"], ["İ", "I"], ["Ü", "U"], ["Ö", "O"], ["Ğ", "G"]
|
2020-12-01 03:28:42 +00:00
|
|
|
|
|
|
|
|
|
];
|
2020-11-30 11:29:34 +00:00
|
|
|
|
conversionLetters.forEach(letterPair => {
|
2022-04-05 04:47:41 +00:00
|
|
|
|
processedFileName = processedFileName.split(letterPair[0]).join(letterPair[1])
|
|
|
|
|
//processedFileName = processedFileName.replace(letterPair[0], letterPair[1])
|
2020-11-30 11:29:34 +00:00
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
//console.log("filename", processedFileName)
|
|
|
|
|
return processedFileName
|
|
|
|
|
},
|
|
|
|
|
/* Parse file name from path then sanitize it */
|
2022-04-05 04:47:41 +00:00
|
|
|
|
parseFileNameFromPath: function (filepath) {
|
2020-11-30 11:29:34 +00:00
|
|
|
|
const parsedFileFromPath = filepath.split("/")[filepath.split("/").length - 1]
|
2022-04-16 15:55:20 +00:00
|
|
|
|
return parsedFileFromPath.replace(".md", "")
|
2020-11-30 11:29:34 +00:00
|
|
|
|
},
|
|
|
|
|
/* Pair provided and existing Filenames*/
|
2022-04-07 09:52:41 +00:00
|
|
|
|
getInternalLinks: function (aFilePath) {
|
2022-04-16 15:55:20 +00:00
|
|
|
|
// const filePaths = Node.getFiles(postsDirectory);
|
|
|
|
|
// const currentFilePath = Transformer.pairCurrentFile(aFilePath, filePaths)
|
2022-04-07 09:52:41 +00:00
|
|
|
|
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)];
|
2022-04-16 15:55:20 +00:00
|
|
|
|
|
|
|
|
|
//TODO: Handle # Link
|
2022-04-07 09:52:41 +00:00
|
|
|
|
let slug = Transformer.parseFileNameFromPath(pageName);
|
|
|
|
|
if (slug.includes('#')) {
|
|
|
|
|
console.log(slug)
|
|
|
|
|
}
|
2022-04-16 15:55:20 +00:00
|
|
|
|
const canonicalSlug = Transformer.pageResolver(pageName)[0]
|
2022-04-07 09:52:41 +00:00
|
|
|
|
|
|
|
|
|
const backLink = {
|
2022-04-17 13:04:58 +00:00
|
|
|
|
title: Transformer.parseFileNameFromPath(toFilePath(canonicalSlug)),
|
2022-04-07 09:52:41 +00:00
|
|
|
|
slug: canonicalSlug,
|
|
|
|
|
shortSummary: canonicalSlug
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-16 15:55:20 +00:00
|
|
|
|
if (canonicalSlug != null && internalLinks.find(aLink => aLink.slug === canonicalSlug) == null) {
|
2022-04-07 09:52:41 +00:00
|
|
|
|
internalLinks.push(backLink);
|
|
|
|
|
}
|
2022-04-16 15:55:20 +00:00
|
|
|
|
|
|
|
|
|
|
2022-04-07 09:52:41 +00:00
|
|
|
|
return [canonicalSlug]
|
|
|
|
|
}
|
|
|
|
|
,
|
|
|
|
|
hrefTemplate: function (permalink) {
|
2022-04-16 15:55:20 +00:00
|
|
|
|
return Transformer.hrefTemplate(permalink)
|
2022-04-07 09:52:41 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
aliasDivider: "|"
|
|
|
|
|
})
|
|
|
|
|
.use(html)
|
|
|
|
|
.processSync(sanitizedContent)
|
|
|
|
|
|
|
|
|
|
return internalLinks;
|
2020-11-30 11:29:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|