make transformer methods static

This commit is contained in:
Triston Armstrong 2023-12-25 21:40:06 -06:00
parent f56e07b94e
commit 84c657cffa
2 changed files with 29 additions and 37 deletions

View File

@ -15,9 +15,8 @@ import { getAllMarkdownFiles, toFilePath, toSlug } from "./utils";
export class Transformer { export class Transformer {
constructor() { }
haveFrontMatter = (content) => { static haveFrontMatter = (content) => {
//console.log("\t Front matter data content", content) //console.log("\t Front matter data content", content)
if (!content) return false if (!content) return false
const indexOfFirst = content.indexOf("---"); const indexOfFirst = content.indexOf("---");
@ -30,19 +29,19 @@ export class Transformer {
return indexOfSecond !== -1; return indexOfSecond !== -1;
} }
getFrontMatterData = (filecontent) => { static getFrontMatterData = (filecontent) => {
if (this.haveFrontMatter(filecontent)) { if (Transformer.haveFrontMatter(filecontent)) {
return matter(filecontent).data return matter(filecontent).data
} }
return {} return {}
} }
pageResolver = (pageName) => { static pageResolver = (pageName) => {
const allFileNames = getAllMarkdownFiles() const allFileNames = getAllMarkdownFiles()
const result = allFileNames.find(aFile => { const result = allFileNames.find(aFile => {
let parseFileNameFromPath = this.parseFileNameFromPath(aFile); let parseFileNameFromPath = Transformer.parseFileNameFromPath(aFile);
return this.normalizeFileName(parseFileNameFromPath) === this.normalizeFileName(pageName) return Transformer.normalizeFileName(parseFileNameFromPath) === Transformer.normalizeFileName(pageName)
} }
) )
@ -56,15 +55,15 @@ export class Transformer {
return (result !== undefined && result.length > 0) ? [toSlug(result)] : ["/"] return (result !== undefined && result.length > 0) ? [toSlug(result)] : ["/"]
} }
hrefTemplate = (permalink) => { static hrefTemplate = (permalink) => {
// permalink = this.normalizeFileName(permalink) // permalink = Transformer.normalizeFileName(permalink)
permalink = permalink.replace("ç", "c").replace("ı", "i").replace("ş", "s") permalink = permalink.replace("ç", "c").replace("ı", "i").replace("ş", "s")
return `/note/${permalink}` return `/note/${permalink}`
} }
getHtmlContent = (content) => { static getHtmlContent = (content) => {
let htmlContent = [] let htmlContent = []
const sanitizedContent = this.preprocessThreeDashes(content) const sanitizedContent = Transformer.preprocessThreeDashes(content)
unified() unified()
.use(markdown, { gfm: true }) .use(markdown, { gfm: true })
@ -75,10 +74,10 @@ export class Transformer {
.use(wikiLinkPlugin, { .use(wikiLinkPlugin, {
permalinks: null, permalinks: null,
pageResolver: (pageName) => { pageResolver: (pageName) => {
return this.pageResolver(pageName) return Transformer.pageResolver(pageName)
}, },
hrefTemplate: (permalink) => { hrefTemplate: (permalink) => {
return this.hrefTemplate(permalink); return Transformer.hrefTemplate(permalink);
}, },
aliasDivider: "|" aliasDivider: "|"
@ -100,7 +99,7 @@ export class Transformer {
} }
/* SANITIZE MARKDOWN FOR --- */ /* SANITIZE MARKDOWN FOR --- */
preprocessThreeDashes = (content) => { static preprocessThreeDashes = (content) => {
const indexOfFirst = content.indexOf("---"); const indexOfFirst = content.indexOf("---");
if (indexOfFirst === -1) { if (indexOfFirst === -1) {
return content return content
@ -112,7 +111,7 @@ export class Transformer {
} }
/* Normalize File Names */ /* Normalize File Names */
normalizeFileName = (filename) => { static normalizeFileName = (filename) => {
let processedFileName = filename.replace(".md", ""); let processedFileName = filename.replace(".md", "");
processedFileName = processedFileName.replace('(', '').replace(')', '') processedFileName = processedFileName.replace('(', '').replace(')', '')
processedFileName = processedFileName.split(" ").join("-") processedFileName = processedFileName.split(" ").join("-")
@ -132,7 +131,7 @@ export class Transformer {
} }
/* Parse file name from path then sanitize it */ /* Parse file name from path then sanitize it */
parseFileNameFromPath = (filepath) => { static parseFileNameFromPath = (filepath) => {
if (typeof filepath === 'string' && filepath.includes("/")) { if (typeof filepath === 'string' && filepath.includes("/")) {
const parsedFileFromPath = filepath.split("/")[filepath.split("/").length - 1] const parsedFileFromPath = filepath.split("/")[filepath.split("/").length - 1]
return parsedFileFromPath.replace(".md", "") return parsedFileFromPath.replace(".md", "")
@ -143,17 +142,16 @@ export class Transformer {
} }
/* Pair provided and existing Filenames*/ /* Pair provided and existing Filenames*/
getInternalLinks = (aFilePath) => { static getInternalLinks = (aFilePath) => {
const fileContent = Node.readFileSync(aFilePath); const fileContent = Node.readFileSync(aFilePath);
const internalLinks = [] const internalLinks = []
const sanitizedContent = this.preprocessThreeDashes(fileContent) const sanitizedContent = Transformer.preprocessThreeDashes(fileContent)
const outer_this = this
unified() unified()
.use(markdown, { gfm: true }) .use(markdown, { gfm: true })
.use(wikiLinkPlugin, { .use(wikiLinkPlugin, {
pageResolver: function(pageName) { pageResolver: function(pageName) {
// let name = [this.parseFileNameFromPath(pageName)]; // let name = [Transformer.parseFileNameFromPath(pageName)];
let canonicalSlug; let canonicalSlug;
if (pageName.includes('#')) { if (pageName.includes('#')) {
@ -163,14 +161,14 @@ export class Transformer {
// Meaning it in form of #Heading1 --> slug will be this file slug // Meaning it in form of #Heading1 --> slug will be this file slug
canonicalSlug = toSlug(aFilePath) canonicalSlug = toSlug(aFilePath)
} else { } else {
canonicalSlug = outer_this.pageResolver(tempSlug)[0].split('#')[0] canonicalSlug = Transformer.pageResolver(tempSlug)[0].split('#')[0]
} }
} else { } else {
canonicalSlug = outer_this.pageResolver(pageName)[0].split('#')[0] canonicalSlug = Transformer.pageResolver(pageName)[0].split('#')[0]
} }
const backLink = { const backLink = {
title: outer_this.parseFileNameFromPath(toFilePath(canonicalSlug)), title: Transformer.parseFileNameFromPath(toFilePath(canonicalSlug)),
slug: canonicalSlug, slug: canonicalSlug,
shortSummary: canonicalSlug shortSummary: canonicalSlug
} }
@ -183,7 +181,7 @@ export class Transformer {
} }
, ,
hrefTemplate: (permalink) => { hrefTemplate: (permalink) => {
return this.hrefTemplate(permalink) return Transformer.hrefTemplate(permalink)
}, },
aliasDivider: "|" aliasDivider: "|"

View File

@ -33,18 +33,15 @@ export function getAllMarkdownFiles() {
} }
export function getSinglePost(slug) { export function getSinglePost(slug) {
const t = new Transformer()
// List of filenames that will provide existing links to wikilink // List of filenames that will provide existing links to wikilink
let currentFilePath = toFilePath(slug) let currentFilePath = toFilePath(slug)
//console.log("currentFilePath: ", currentFilePath) //console.log("currentFilePath: ", currentFilePath)
var fileContent = Node.readFileSync(currentFilePath) var fileContent = Node.readFileSync(currentFilePath)
//const currentFileFrontMatter = t.getFrontMatterData(fileContent) //const currentFileFrontMatter = Transformer.getFrontMatterData(fileContent)
// console.log("===============\n\nFile is scanning: ", slug) // console.log("===============\n\nFile is scanning: ", slug)
const [htmlContent] = t.getHtmlContent(fileContent) const [htmlContent] = Transformer.getHtmlContent(fileContent)
// console.log("==================================") // console.log("==================================")
//console.log("hrmlcontents and backlinks") //console.log("hrmlcontents and backlinks")
return { return {
@ -109,7 +106,6 @@ export function toSlug(filePath) {
export function constructGraphData() { export function constructGraphData() {
const t = new Transformer()
const filepath = path.join(process.cwd(), "graph-data.json"); const filepath = path.join(process.cwd(), "graph-data.json");
if (Node.isFile(filepath)) { if (Node.isFile(filepath)) {
const data = fs.readFileSync(filepath); const data = fs.readFileSync(filepath);
@ -122,14 +118,14 @@ export function constructGraphData() {
.forEach(aFilePath => { .forEach(aFilePath => {
// const {currentFilePath} = getFileNames(filename) // const {currentFilePath} = getFileNames(filename)
const aNode = { const aNode = {
title: t.parseFileNameFromPath(aFilePath), title: Transformer.parseFileNameFromPath(aFilePath),
slug: toSlug(aFilePath), slug: toSlug(aFilePath),
shortSummary: getShortSummary(toSlug(aFilePath)) shortSummary: getShortSummary(toSlug(aFilePath))
} }
nodes.push(aNode) nodes.push(aNode)
// console.log("Constructing graph for node: " + aFilePath ) // console.log("Constructing graph for node: " + aFilePath )
const internalLinks = t.getInternalLinks(aFilePath) const internalLinks = Transformer.getInternalLinks(aFilePath)
internalLinks.forEach(aLink => { internalLinks.forEach(aLink => {
if (aLink.slug === null || aLink.slug.length === 0) return if (aLink.slug === null || aLink.slug.length === 0) return
@ -153,14 +149,13 @@ export function constructGraphData() {
export function getLocalGraphData(currentNodeId) { export function getLocalGraphData(currentNodeId) {
const t = new Transformer()
const { nodes, edges } = constructGraphData() const { nodes, edges } = constructGraphData()
const newNodes = nodes.map(aNode => ( const newNodes = nodes.map(aNode => (
{ {
data: { data: {
id: aNode.slug.toString(), id: aNode.slug.toString(),
label: t.parseFileNameFromPath(toFilePath(aNode.slug)), label: Transformer.parseFileNameFromPath(toFilePath(aNode.slug)),
} }
} }
)) ))
@ -234,12 +229,11 @@ export function getDirectoryData() {
let _counter = 0; let _counter = 0;
export function convertObject(thisObject) { export function convertObject(thisObject) {
const t = new Transformer()
const children = [] const children = []
let routerPath = getAllSlugs().find(slug => { let routerPath = getAllSlugs().find(slug => {
const fileName = t.parseFileNameFromPath(toFilePath(slug)) const fileName = Transformer.parseFileNameFromPath(toFilePath(slug))
return t.normalizeFileName(fileName) === t.normalizeFileName(thisObject.name) return Transformer.normalizeFileName(fileName) === Transformer.normalizeFileName(thisObject.name)
}) || null }) || null
routerPath = routerPath ? '/note/' + routerPath : null routerPath = routerPath ? '/note/' + routerPath : null
const newObject = { const newObject = {