2020-11-30 11:29:34 +00:00
|
|
|
|
import matter from 'gray-matter'
|
|
|
|
|
import path from 'path'
|
|
|
|
|
import fs from "fs"
|
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-05 04:47:41 +00:00
|
|
|
|
import remarkRehype from 'remark-rehype'
|
|
|
|
|
import rehypeStringify from 'rehype-stringify'
|
|
|
|
|
import {createElement, Fragment, useEffect, useState} from 'react'
|
|
|
|
|
import rehypeReact from 'rehype-react'
|
|
|
|
|
import CustomLink from "../components/CustomLink";
|
2020-11-30 11:29:34 +00:00
|
|
|
|
|
|
|
|
|
const postsDirectory = path.join(process.cwd(), 'posts')
|
|
|
|
|
const isFile = fileName => {
|
2022-04-05 04:47:41 +00:00
|
|
|
|
return fs.lstatSync(fileName).isFile()
|
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
|
|
|
|
|
var indexOfFirst = content.indexOf("---")
|
|
|
|
|
//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
|
|
|
|
|
}
|
|
|
|
|
var indexOfSecond = content.indexOf("---", (indexOfFirst + 1))
|
|
|
|
|
if (indexOfSecond !== -1) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
},
|
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-05 04:47:41 +00:00
|
|
|
|
getProcessor : function (content, {fileNames}) {
|
2020-11-30 11:29:34 +00:00
|
|
|
|
let htmlContent = []
|
|
|
|
|
let internalLinks = []
|
2022-04-05 04:47:41 +00:00
|
|
|
|
return unified()
|
|
|
|
|
.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-05 04:47:41 +00:00
|
|
|
|
permalinks:fileNames,
|
|
|
|
|
pageResolver: function(pageName){
|
|
|
|
|
const name = [Transformer.parseFileNameFromPath(pageName)]
|
|
|
|
|
//console.log("\n\nwiki internal links", Transformer.parseFileNameFromPath(name[0]));
|
|
|
|
|
internalLinks.push(Transformer.parseFileNameFromPath(name[0]));
|
|
|
|
|
return name
|
|
|
|
|
},
|
|
|
|
|
hrefTemplate: function(permalink){
|
|
|
|
|
permalink = Transformer.normalizeFileName(permalink)
|
|
|
|
|
permalink = permalink.replace("ç","c").replace("ı","i").replace("ş","s")
|
|
|
|
|
//console.log("wiki pemalink", permalink);
|
|
|
|
|
return `/note/${permalink}`
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
aliasDivider:"|"
|
|
|
|
|
})
|
|
|
|
|
.use(remarkRehype)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
},
|
2022-03-16 05:01:01 +00:00
|
|
|
|
|
2022-04-05 04:47:41 +00:00
|
|
|
|
getHtmlContent: function (content, {fileNames}) {
|
|
|
|
|
let htmlContent = []
|
|
|
|
|
let internalLinks = []
|
|
|
|
|
const sanitizedContent = Transformer.preprocessThreeDashes(content)
|
|
|
|
|
Transformer.getProcessor(content, fileNames)
|
|
|
|
|
// .use(rehypeReact, {createElement, Fragment})
|
|
|
|
|
.use(rehypeStringify)
|
2020-11-30 11:29:34 +00:00
|
|
|
|
.process(sanitizedContent,
|
|
|
|
|
function (err, file) {
|
|
|
|
|
//console.log("asd", String(file).slice(0,50))
|
|
|
|
|
//console.error("remark: ", report(err || file))
|
|
|
|
|
htmlContent.push(String(file).replace("\n", ""))
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
htmlContent = htmlContent.join("")
|
|
|
|
|
htmlContent = htmlContent.split("---")
|
|
|
|
|
//console.log("ffffff ", htmlContent)
|
|
|
|
|
return [htmlContent, internalLinks]
|
|
|
|
|
},
|
|
|
|
|
|
2022-04-05 04:47:41 +00:00
|
|
|
|
// getReact : function (content, {fileNames}) {
|
|
|
|
|
// const sanitizedContent = Transformer.preprocessThreeDashes(content)
|
|
|
|
|
// let result = null
|
|
|
|
|
// Transformer.getProcessor(content, {fileNames})
|
|
|
|
|
// .use(rehypeReact, {createElement, Fragment})
|
|
|
|
|
// .process(sanitizedContent, file => {
|
|
|
|
|
// result = file.result
|
|
|
|
|
// })
|
|
|
|
|
//
|
|
|
|
|
// return result
|
|
|
|
|
// },
|
|
|
|
|
|
2020-11-30 11:29:34 +00:00
|
|
|
|
/* SANITIZE MARKDOWN FOR --- */
|
2022-04-05 04:47:41 +00:00
|
|
|
|
preprocessThreeDashes: function (content) {
|
2020-11-30 11:29:34 +00:00
|
|
|
|
var indexOfFirst = content.indexOf("---")
|
2022-04-05 04:47:41 +00:00
|
|
|
|
if (indexOfFirst === -1) {
|
2020-11-30 11:29:34 +00:00
|
|
|
|
return content
|
|
|
|
|
}
|
|
|
|
|
var 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 */
|
2022-04-05 04:47:41 +00:00
|
|
|
|
normalizeFileName: function (filename) {
|
2020-11-30 11:29:34 +00:00
|
|
|
|
var 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-03-16 05:01:01 +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-05 04:47:41 +00:00
|
|
|
|
const parsedFileName = parsedFileFromPath.replace(".md", "")
|
2020-11-30 11:29:34 +00:00
|
|
|
|
return Transformer.normalizeFileName(parsedFileName)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* Pair provided and existing Filenames*/
|
2022-04-05 04:47:41 +00:00
|
|
|
|
pairCurrentFile: function (provided, ListOfFilePaths) {
|
2020-11-30 11:29:34 +00:00
|
|
|
|
//console.log(provided, ListOfFilePaths)
|
|
|
|
|
const providedSanitizedFileName = Transformer.normalizeFileName(provided);
|
2022-04-05 04:47:41 +00:00
|
|
|
|
|
2020-11-30 11:29:34 +00:00
|
|
|
|
// Map file paths and return true if it pairs with provided
|
2022-04-05 04:47:41 +00:00
|
|
|
|
const possibleFilePath = ListOfFilePaths.filter(possibleFilePath => {
|
2020-11-30 11:29:34 +00:00
|
|
|
|
const possibleFileName = Transformer.parseFileNameFromPath(possibleFilePath);
|
|
|
|
|
const possibleSanitizedFileName = Transformer.normalizeFileName(possibleFileName)
|
|
|
|
|
//console.log("----", providedSanitizedFileName, possibleSanitizedFileName)
|
2022-04-05 04:47:41 +00:00
|
|
|
|
|
2020-11-30 11:29:34 +00:00
|
|
|
|
//console.log("---", possibleSanitizedFileName, providedSanitizedFileName)
|
2022-04-05 04:47:41 +00:00
|
|
|
|
if (providedSanitizedFileName === possibleSanitizedFileName) {
|
2020-11-30 11:29:34 +00:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
})
|
2020-12-06 19:40:20 +00:00
|
|
|
|
console.log("p---", possibleFilePath)
|
2020-11-30 11:29:34 +00:00
|
|
|
|
return possibleFilePath[0]
|
|
|
|
|
}
|
|
|
|
|
}
|