From f56e07b94e5229dcbd15b42cf1a183a805abc847 Mon Sep 17 00:00:00 2001 From: Triston Armstrong Date: Mon, 25 Dec 2023 20:48:51 -0600 Subject: [PATCH] convert transformer to a class NOTE - all of these methods can be static i believe. no need to use this and instantiate new instances every time.. i dont think atm anyway --- lib/transformer.js | 78 +++++++++++++++++++++++++--------------------- lib/utils.js | 20 ++++++------ 2 files changed, 54 insertions(+), 44 deletions(-) diff --git a/lib/transformer.js b/lib/transformer.js index db85035..bb2b0e3 100644 --- a/lib/transformer.js +++ b/lib/transformer.js @@ -13,8 +13,11 @@ import rehypeStringify from 'rehype-stringify' import obsidianImage from './obsidian-image.js' import { getAllMarkdownFiles, toFilePath, toSlug } from "./utils"; -export const Transformer = { - haveFrontMatter: function(content) { + +export class Transformer { + constructor() { } + + haveFrontMatter = (content) => { //console.log("\t Front matter data content", content) if (!content) return false const indexOfFirst = content.indexOf("---"); @@ -25,21 +28,21 @@ export const Transformer = { } let indexOfSecond = content.indexOf("---", (indexOfFirst + 1)); return indexOfSecond !== -1; + } - }, - getFrontMatterData: function(filecontent) { - if (Transformer.haveFrontMatter(filecontent)) { + getFrontMatterData = (filecontent) => { + if (this.haveFrontMatter(filecontent)) { return matter(filecontent).data } return {} - }, + } - pageResolver: function(pageName) { + pageResolver = (pageName) => { const allFileNames = getAllMarkdownFiles() const result = allFileNames.find(aFile => { - let parseFileNameFromPath = Transformer.parseFileNameFromPath(aFile); - return Transformer.normalizeFileName(parseFileNameFromPath) === Transformer.normalizeFileName(pageName) + let parseFileNameFromPath = this.parseFileNameFromPath(aFile); + return this.normalizeFileName(parseFileNameFromPath) === this.normalizeFileName(pageName) } ) @@ -51,14 +54,17 @@ export const Transformer = { // console.log("Internal Link resolved: [" + pageName + "] ==> [" + temp[0] +"]") return (result !== undefined && result.length > 0) ? [toSlug(result)] : ["/"] - }, - hrefTemplate: function(permalink) { - // permalink = Transformer.normalizeFileName(permalink) + } + + hrefTemplate = (permalink) => { + // permalink = this.normalizeFileName(permalink) permalink = permalink.replace("ç", "c").replace("ı", "i").replace("ş", "s") - return `/note/${permalink}`; - }, getHtmlContent: function(content) { + return `/note/${permalink}` + } + + getHtmlContent = (content) => { let htmlContent = [] - const sanitizedContent = Transformer.preprocessThreeDashes(content) + const sanitizedContent = this.preprocessThreeDashes(content) unified() .use(markdown, { gfm: true }) @@ -68,11 +74,11 @@ export const Transformer = { // .use(frontmatter, ['yaml', 'toml']) .use(wikiLinkPlugin, { permalinks: null, - pageResolver: function(pageName) { - return Transformer.pageResolver(pageName) + pageResolver: (pageName) => { + return this.pageResolver(pageName) }, - hrefTemplate: function(permalink) { - return Transformer.hrefTemplate(permalink); + hrefTemplate: (permalink) => { + return this.hrefTemplate(permalink); }, aliasDivider: "|" @@ -91,10 +97,10 @@ export const Transformer = { htmlContent = htmlContent.join("") htmlContent = htmlContent.split("---") return [htmlContent] - }, + } /* SANITIZE MARKDOWN FOR --- */ - preprocessThreeDashes: function(content) { + preprocessThreeDashes = (content) => { const indexOfFirst = content.indexOf("---"); if (indexOfFirst === -1) { return content @@ -103,10 +109,10 @@ export const Transformer = { content.slice(0, indexOfSecond); const contentPart = content.slice(indexOfSecond); return contentPart.split("---").join("") - }, + } /* Normalize File Names */ - normalizeFileName: function(filename) { + normalizeFileName = (filename) => { let processedFileName = filename.replace(".md", ""); processedFileName = processedFileName.replace('(', '').replace(')', '') processedFileName = processedFileName.split(" ").join("-") @@ -123,9 +129,10 @@ export const Transformer = { ) //console.log("filename", processedFileName) return processedFileName - }, + } + /* Parse file name from path then sanitize it */ - parseFileNameFromPath: function(filepath) { + parseFileNameFromPath = (filepath) => { if (typeof filepath === 'string' && filepath.includes("/")) { const parsedFileFromPath = filepath.split("/")[filepath.split("/").length - 1] return parsedFileFromPath.replace(".md", "") @@ -133,18 +140,20 @@ export const Transformer = { console.log("Failed: CANNOT Parse" + filepath) return null } - }, + } + /* Pair provided and existing Filenames*/ - getInternalLinks: function(aFilePath) { + getInternalLinks = (aFilePath) => { const fileContent = Node.readFileSync(aFilePath); const internalLinks = [] - const sanitizedContent = Transformer.preprocessThreeDashes(fileContent) + const sanitizedContent = this.preprocessThreeDashes(fileContent) + const outer_this = this unified() .use(markdown, { gfm: true }) .use(wikiLinkPlugin, { pageResolver: function(pageName) { - // let name = [Transformer.parseFileNameFromPath(pageName)]; + // let name = [this.parseFileNameFromPath(pageName)]; let canonicalSlug; if (pageName.includes('#')) { @@ -154,15 +163,14 @@ export const Transformer = { // Meaning it in form of #Heading1 --> slug will be this file slug canonicalSlug = toSlug(aFilePath) } else { - canonicalSlug = Transformer.pageResolver(tempSlug)[0].split('#')[0] + canonicalSlug = outer_this.pageResolver(tempSlug)[0].split('#')[0] } } else { - canonicalSlug = Transformer.pageResolver(pageName)[0].split('#')[0] + canonicalSlug = outer_this.pageResolver(pageName)[0].split('#')[0] } - const backLink = { - title: Transformer.parseFileNameFromPath(toFilePath(canonicalSlug)), + title: outer_this.parseFileNameFromPath(toFilePath(canonicalSlug)), slug: canonicalSlug, shortSummary: canonicalSlug } @@ -174,8 +182,8 @@ export const Transformer = { return [canonicalSlug] } , - hrefTemplate: function(permalink) { - return Transformer.hrefTemplate(permalink) + hrefTemplate: (permalink) => { + return this.hrefTemplate(permalink) }, aliasDivider: "|" diff --git a/lib/utils.js b/lib/utils.js index c7b0492..cae9a76 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -34,6 +34,7 @@ export function getAllMarkdownFiles() { export function getSinglePost(slug) { + const t = new Transformer() // List of filenames that will provide existing links to wikilink let currentFilePath = toFilePath(slug) @@ -41,9 +42,9 @@ export function getSinglePost(slug) { var fileContent = Node.readFileSync(currentFilePath) - //const currentFileFrontMatter = Transformer.getFrontMatterData(fileContent) + //const currentFileFrontMatter = t.getFrontMatterData(fileContent) // console.log("===============\n\nFile is scanning: ", slug) - const [htmlContent] = Transformer.getHtmlContent(fileContent) + const [htmlContent] = t.getHtmlContent(fileContent) // console.log("==================================") //console.log("hrmlcontents and backlinks") return { @@ -108,7 +109,7 @@ export function toSlug(filePath) { export function constructGraphData() { - + const t = new Transformer() const filepath = path.join(process.cwd(), "graph-data.json"); if (Node.isFile(filepath)) { const data = fs.readFileSync(filepath); @@ -121,14 +122,14 @@ export function constructGraphData() { .forEach(aFilePath => { // const {currentFilePath} = getFileNames(filename) const aNode = { - title: Transformer.parseFileNameFromPath(aFilePath), + title: t.parseFileNameFromPath(aFilePath), slug: toSlug(aFilePath), shortSummary: getShortSummary(toSlug(aFilePath)) } nodes.push(aNode) // console.log("Constructing graph for node: " + aFilePath ) - const internalLinks = Transformer.getInternalLinks(aFilePath) + const internalLinks = t.getInternalLinks(aFilePath) internalLinks.forEach(aLink => { if (aLink.slug === null || aLink.slug.length === 0) return @@ -152,14 +153,14 @@ export function constructGraphData() { export function getLocalGraphData(currentNodeId) { - + const t = new Transformer() const { nodes, edges } = constructGraphData() const newNodes = nodes.map(aNode => ( { data: { id: aNode.slug.toString(), - label: Transformer.parseFileNameFromPath(toFilePath(aNode.slug)), + label: t.parseFileNameFromPath(toFilePath(aNode.slug)), } } )) @@ -233,11 +234,12 @@ export function getDirectoryData() { let _counter = 0; export function convertObject(thisObject) { + const t = new Transformer() const children = [] let routerPath = getAllSlugs().find(slug => { - const fileName = Transformer.parseFileNameFromPath(toFilePath(slug)) - return Transformer.normalizeFileName(fileName) === Transformer.normalizeFileName(thisObject.name) + const fileName = t.parseFileNameFromPath(toFilePath(slug)) + return t.normalizeFileName(fileName) === t.normalizeFileName(thisObject.name) }) || null routerPath = routerPath ? '/note/' + routerPath : null const newObject = {