2024-01-07 16:32:38 +00:00
|
|
|
|
// import matter from 'gray-matter'
|
|
|
|
|
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'
|
2024-01-14 05:26:21 +00:00
|
|
|
|
import { default as Node } from './node'
|
2022-04-19 06:17:47 +00:00
|
|
|
|
import rehypePrism from 'rehype-prism-plus'
|
|
|
|
|
import remarkRehype from 'remark-rehype'
|
|
|
|
|
import rehypeStringify from 'rehype-stringify'
|
2022-04-28 09:44:59 +00:00
|
|
|
|
import obsidianImage from './obsidian-image.js'
|
2023-12-27 05:36:36 +00:00
|
|
|
|
import Utils from '../lib/utils.js'
|
2023-12-26 02:48:51 +00:00
|
|
|
|
|
|
|
|
|
export class Transformer {
|
2023-12-26 03:40:06 +00:00
|
|
|
|
static haveFrontMatter = (content) => {
|
2024-01-07 16:32:38 +00:00
|
|
|
|
if (content === (null | undefined | '')) return false
|
|
|
|
|
const indexOfFirst = content.indexOf('---')
|
2023-12-24 02:13:02 +00:00
|
|
|
|
if (indexOfFirst === -1) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2024-01-07 16:32:38 +00:00
|
|
|
|
const indexOfSecond = content.indexOf('---', (indexOfFirst + 1))
|
|
|
|
|
return indexOfSecond !== -1
|
2023-12-26 02:48:51 +00:00
|
|
|
|
}
|
2022-04-07 03:44:02 +00:00
|
|
|
|
|
2023-12-26 03:40:06 +00:00
|
|
|
|
static getFrontMatterData = (filecontent) => {
|
2023-12-24 02:13:02 +00:00
|
|
|
|
return {}
|
2023-12-26 02:48:51 +00:00
|
|
|
|
}
|
2020-11-30 11:29:34 +00:00
|
|
|
|
|
2023-12-26 03:40:06 +00:00
|
|
|
|
static pageResolver = (pageName) => {
|
2023-12-27 05:36:36 +00:00
|
|
|
|
const allFileNames = Utils.getAllMarkdownFiles()
|
2023-12-24 02:13:02 +00:00
|
|
|
|
const result = allFileNames.find(aFile => {
|
2024-01-07 16:32:38 +00:00
|
|
|
|
const parseFileNameFromPath = Transformer.parseFileNameFromPath(aFile)
|
2023-12-26 03:40:06 +00:00
|
|
|
|
return Transformer.normalizeFileName(parseFileNameFromPath) === Transformer.normalizeFileName(pageName)
|
2023-12-24 02:13:02 +00:00
|
|
|
|
}
|
|
|
|
|
)
|
2022-04-16 15:55:20 +00:00
|
|
|
|
|
2023-12-24 02:13:02 +00:00
|
|
|
|
if (result === undefined || result.length === 0) {
|
|
|
|
|
// console.log("Cannot resolve file path " + pageName)
|
|
|
|
|
}
|
2022-04-18 04:48:36 +00:00
|
|
|
|
|
2023-12-24 02:13:02 +00:00
|
|
|
|
// console.log("Internal Link resolved: [" + pageName + "] ==> [" + temp[0] +"]")
|
2024-01-07 16:32:38 +00:00
|
|
|
|
return (result !== undefined && result.length > 0) ? [Utils.toSlug(result)] : ['/']
|
2023-12-26 02:48:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-26 03:40:06 +00:00
|
|
|
|
static hrefTemplate = (permalink) => {
|
|
|
|
|
// permalink = Transformer.normalizeFileName(permalink)
|
2024-01-07 16:32:38 +00:00
|
|
|
|
permalink = permalink.replace('ç', 'c').replace('ı', 'i').replace('ş', 's')
|
2023-12-31 17:56:14 +00:00
|
|
|
|
return `/notes/${permalink}`
|
2023-12-26 02:48:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-26 03:40:06 +00:00
|
|
|
|
static getHtmlContent = (content) => {
|
2023-12-24 02:13:02 +00:00
|
|
|
|
let htmlContent = []
|
2023-12-26 03:40:06 +00:00
|
|
|
|
const sanitizedContent = Transformer.preprocessThreeDashes(content)
|
2024-01-08 05:07:20 +00:00
|
|
|
|
|
2023-12-24 02:13:02 +00:00
|
|
|
|
unified()
|
|
|
|
|
.use(markdown, { gfm: true })
|
|
|
|
|
.use(obsidianImage)
|
|
|
|
|
.use(highlight)
|
2024-01-07 16:32:38 +00:00
|
|
|
|
.use(externalLinks, { target: '_blank', rel: ['noopener'] })
|
2023-12-24 02:13:02 +00:00
|
|
|
|
// .use(frontmatter, ['yaml', 'toml'])
|
|
|
|
|
.use(wikiLinkPlugin, {
|
|
|
|
|
permalinks: null,
|
2023-12-26 02:48:51 +00:00
|
|
|
|
pageResolver: (pageName) => {
|
2023-12-26 03:40:06 +00:00
|
|
|
|
return Transformer.pageResolver(pageName)
|
2023-12-24 02:13:02 +00:00
|
|
|
|
},
|
2023-12-26 02:48:51 +00:00
|
|
|
|
hrefTemplate: (permalink) => {
|
2024-01-07 16:32:38 +00:00
|
|
|
|
return Transformer.hrefTemplate(permalink)
|
2023-12-24 02:13:02 +00:00
|
|
|
|
},
|
|
|
|
|
|
2024-01-07 16:32:38 +00:00
|
|
|
|
aliasDivider: '|'
|
2023-12-24 02:13:02 +00:00
|
|
|
|
})
|
|
|
|
|
.use(remarkRehype)
|
|
|
|
|
.use(rehypePrism)
|
|
|
|
|
.use(rehypeStringify)
|
|
|
|
|
.process(sanitizedContent,
|
|
|
|
|
function(err, file) {
|
2024-01-07 16:32:38 +00:00
|
|
|
|
htmlContent.push(String(file).replace('\n', ''))
|
2024-01-07 18:34:09 +00:00
|
|
|
|
if (err !== null && err !== undefined) {
|
|
|
|
|
console.log('CUSTOM ERROR @transformer.js:89:' + err)
|
2024-01-08 05:07:20 +00:00
|
|
|
|
} else {
|
|
|
|
|
return
|
2023-12-24 02:13:02 +00:00
|
|
|
|
}
|
2020-11-30 11:29:34 +00:00
|
|
|
|
}
|
2023-12-24 02:13:02 +00:00
|
|
|
|
)
|
2024-01-08 05:07:20 +00:00
|
|
|
|
|
2024-01-07 16:32:38 +00:00
|
|
|
|
htmlContent = htmlContent.join('')
|
|
|
|
|
htmlContent = htmlContent.split('---')
|
2023-12-24 02:13:02 +00:00
|
|
|
|
return [htmlContent]
|
2023-12-26 02:48:51 +00:00
|
|
|
|
}
|
2023-12-24 02:13:02 +00:00
|
|
|
|
|
|
|
|
|
/* SANITIZE MARKDOWN FOR --- */
|
2023-12-26 03:40:06 +00:00
|
|
|
|
static preprocessThreeDashes = (content) => {
|
2024-01-07 16:32:38 +00:00
|
|
|
|
const indexOfFirst = content.indexOf('---')
|
2023-12-24 02:13:02 +00:00
|
|
|
|
if (indexOfFirst === -1) {
|
|
|
|
|
return content
|
|
|
|
|
}
|
2024-01-07 16:32:38 +00:00
|
|
|
|
const indexOfSecond = content.indexOf('---', (indexOfFirst + 1))
|
|
|
|
|
content.slice(0, indexOfSecond)
|
|
|
|
|
const contentPart = content.slice(indexOfSecond)
|
|
|
|
|
return contentPart.split('---').join('')
|
2023-12-26 02:48:51 +00:00
|
|
|
|
}
|
2023-12-24 02:13:02 +00:00
|
|
|
|
|
|
|
|
|
/* Normalize File Names */
|
2023-12-26 03:40:06 +00:00
|
|
|
|
static normalizeFileName = (filename) => {
|
2024-01-07 16:32:38 +00:00
|
|
|
|
let processedFileName = filename.replace('.md', '')
|
2023-12-24 02:13:02 +00:00
|
|
|
|
processedFileName = processedFileName.replace('(', '').replace(')', '')
|
2024-01-07 16:32:38 +00:00
|
|
|
|
processedFileName = processedFileName.split(' ').join('-')
|
2023-12-24 02:13:02 +00:00
|
|
|
|
processedFileName = processedFileName.toLowerCase()
|
|
|
|
|
const conversionLetters = [
|
2024-01-07 16:32:38 +00:00
|
|
|
|
['ç', 'c'], ['ş', 's'], ['ı', 'i'], ['ü', 'u'], ['ö', 'o'], ['ğ', 'g'],
|
|
|
|
|
['Ç', 'C'], ['Ş', 'S'], ['İ', 'I'], ['Ü', 'U'], ['Ö', 'O'], ['Ğ', 'G']
|
2023-12-24 02:13:02 +00:00
|
|
|
|
|
2024-01-07 16:32:38 +00:00
|
|
|
|
]
|
2023-12-24 02:13:02 +00:00
|
|
|
|
conversionLetters.forEach(letterPair => {
|
|
|
|
|
processedFileName = processedFileName.split(letterPair[0]).join(letterPair[1])
|
2024-01-07 16:32:38 +00:00
|
|
|
|
// processedFileName = processedFileName.replace(letterPair[0], letterPair[1])
|
2023-12-24 02:13:02 +00:00
|
|
|
|
}
|
|
|
|
|
)
|
2024-01-07 16:32:38 +00:00
|
|
|
|
// console.log("filename", processedFileName)
|
2023-12-24 02:13:02 +00:00
|
|
|
|
return processedFileName
|
2023-12-26 02:48:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-24 02:13:02 +00:00
|
|
|
|
/* Parse file name from path then sanitize it */
|
2023-12-26 03:40:06 +00:00
|
|
|
|
static parseFileNameFromPath = (filepath) => {
|
2024-01-07 16:32:38 +00:00
|
|
|
|
if (typeof filepath === 'string' && filepath.includes('/')) {
|
|
|
|
|
const parsedFileFromPath = filepath.split('/')[filepath.split('/').length - 1]
|
|
|
|
|
return parsedFileFromPath.replace('.md', '')
|
2023-12-24 02:13:02 +00:00
|
|
|
|
} else {
|
2024-01-07 16:32:38 +00:00
|
|
|
|
console.log('Failed: CANNOT Parse' + filepath)
|
2023-12-24 02:13:02 +00:00
|
|
|
|
return null
|
|
|
|
|
}
|
2023-12-26 02:48:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 16:32:38 +00:00
|
|
|
|
/* Pair provided and existing Filenames */
|
2023-12-26 03:40:06 +00:00
|
|
|
|
static getInternalLinks = (aFilePath) => {
|
2024-01-07 16:32:38 +00:00
|
|
|
|
const fileContent = Node.readFileSync(aFilePath)
|
2023-12-24 02:13:02 +00:00
|
|
|
|
const internalLinks = []
|
2023-12-26 03:40:06 +00:00
|
|
|
|
const sanitizedContent = Transformer.preprocessThreeDashes(fileContent)
|
2023-12-24 02:13:02 +00:00
|
|
|
|
unified()
|
|
|
|
|
.use(markdown, { gfm: true })
|
|
|
|
|
.use(wikiLinkPlugin, {
|
|
|
|
|
pageResolver: function(pageName) {
|
2023-12-26 03:40:06 +00:00
|
|
|
|
// let name = [Transformer.parseFileNameFromPath(pageName)];
|
2023-12-24 02:13:02 +00:00
|
|
|
|
|
2024-01-07 16:32:38 +00:00
|
|
|
|
let canonicalSlug
|
|
|
|
|
if (pageName.includes('#') !== (false, null, undefined)) {
|
2023-12-24 02:13:02 +00:00
|
|
|
|
// console.log(pageName)
|
|
|
|
|
const tempSlug = pageName.split('#')[0]
|
|
|
|
|
if (tempSlug.length === 0) {
|
|
|
|
|
// Meaning it in form of #Heading1 --> slug will be this file slug
|
2023-12-27 05:36:36 +00:00
|
|
|
|
canonicalSlug = Utils.toSlug(aFilePath)
|
2023-12-24 02:13:02 +00:00
|
|
|
|
} else {
|
2023-12-26 03:40:06 +00:00
|
|
|
|
canonicalSlug = Transformer.pageResolver(tempSlug)[0].split('#')[0]
|
2020-11-30 11:29:34 +00:00
|
|
|
|
}
|
2023-12-24 02:13:02 +00:00
|
|
|
|
} else {
|
2023-12-26 03:40:06 +00:00
|
|
|
|
canonicalSlug = Transformer.pageResolver(pageName)[0].split('#')[0]
|
2023-12-24 02:13:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const backLink = {
|
2023-12-27 05:36:36 +00:00
|
|
|
|
title: Transformer.parseFileNameFromPath(Utils.toFilePath(canonicalSlug)),
|
2023-12-24 02:13:02 +00:00
|
|
|
|
slug: canonicalSlug,
|
|
|
|
|
shortSummary: canonicalSlug
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 16:32:38 +00:00
|
|
|
|
if (canonicalSlug != null && !internalLinks.includes(canonicalSlug)) {
|
|
|
|
|
internalLinks.push(backLink)
|
2023-12-24 02:13:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [canonicalSlug]
|
2024-01-07 16:32:38 +00:00
|
|
|
|
},
|
2023-12-26 02:48:51 +00:00
|
|
|
|
hrefTemplate: (permalink) => {
|
2023-12-26 03:40:06 +00:00
|
|
|
|
return Transformer.hrefTemplate(permalink)
|
2023-12-24 02:13:02 +00:00
|
|
|
|
},
|
|
|
|
|
|
2024-01-07 16:32:38 +00:00
|
|
|
|
aliasDivider: '|'
|
2023-12-24 02:13:02 +00:00
|
|
|
|
})
|
|
|
|
|
.use(html)
|
|
|
|
|
.processSync(sanitizedContent)
|
2024-01-07 16:32:38 +00:00
|
|
|
|
return internalLinks
|
2023-12-24 02:13:02 +00:00
|
|
|
|
}
|
2020-11-30 11:29:34 +00:00
|
|
|
|
}
|