make transformer methods static
This commit is contained in:
parent
f56e07b94e
commit
84c657cffa
@ -15,9 +15,8 @@ import { getAllMarkdownFiles, toFilePath, toSlug } from "./utils";
|
||||
|
||||
|
||||
export class Transformer {
|
||||
constructor() { }
|
||||
|
||||
haveFrontMatter = (content) => {
|
||||
static haveFrontMatter = (content) => {
|
||||
//console.log("\t Front matter data content", content)
|
||||
if (!content) return false
|
||||
const indexOfFirst = content.indexOf("---");
|
||||
@ -30,19 +29,19 @@ export class Transformer {
|
||||
return indexOfSecond !== -1;
|
||||
}
|
||||
|
||||
getFrontMatterData = (filecontent) => {
|
||||
if (this.haveFrontMatter(filecontent)) {
|
||||
static getFrontMatterData = (filecontent) => {
|
||||
if (Transformer.haveFrontMatter(filecontent)) {
|
||||
return matter(filecontent).data
|
||||
}
|
||||
return {}
|
||||
}
|
||||
|
||||
|
||||
pageResolver = (pageName) => {
|
||||
static pageResolver = (pageName) => {
|
||||
const allFileNames = getAllMarkdownFiles()
|
||||
const result = allFileNames.find(aFile => {
|
||||
let parseFileNameFromPath = this.parseFileNameFromPath(aFile);
|
||||
return this.normalizeFileName(parseFileNameFromPath) === this.normalizeFileName(pageName)
|
||||
let parseFileNameFromPath = Transformer.parseFileNameFromPath(aFile);
|
||||
return Transformer.normalizeFileName(parseFileNameFromPath) === Transformer.normalizeFileName(pageName)
|
||||
}
|
||||
)
|
||||
|
||||
@ -56,15 +55,15 @@ export class Transformer {
|
||||
return (result !== undefined && result.length > 0) ? [toSlug(result)] : ["/"]
|
||||
}
|
||||
|
||||
hrefTemplate = (permalink) => {
|
||||
// permalink = this.normalizeFileName(permalink)
|
||||
static hrefTemplate = (permalink) => {
|
||||
// permalink = Transformer.normalizeFileName(permalink)
|
||||
permalink = permalink.replace("ç", "c").replace("ı", "i").replace("ş", "s")
|
||||
return `/note/${permalink}`
|
||||
}
|
||||
|
||||
getHtmlContent = (content) => {
|
||||
static getHtmlContent = (content) => {
|
||||
let htmlContent = []
|
||||
const sanitizedContent = this.preprocessThreeDashes(content)
|
||||
const sanitizedContent = Transformer.preprocessThreeDashes(content)
|
||||
|
||||
unified()
|
||||
.use(markdown, { gfm: true })
|
||||
@ -75,10 +74,10 @@ export class Transformer {
|
||||
.use(wikiLinkPlugin, {
|
||||
permalinks: null,
|
||||
pageResolver: (pageName) => {
|
||||
return this.pageResolver(pageName)
|
||||
return Transformer.pageResolver(pageName)
|
||||
},
|
||||
hrefTemplate: (permalink) => {
|
||||
return this.hrefTemplate(permalink);
|
||||
return Transformer.hrefTemplate(permalink);
|
||||
},
|
||||
|
||||
aliasDivider: "|"
|
||||
@ -100,7 +99,7 @@ export class Transformer {
|
||||
}
|
||||
|
||||
/* SANITIZE MARKDOWN FOR --- */
|
||||
preprocessThreeDashes = (content) => {
|
||||
static preprocessThreeDashes = (content) => {
|
||||
const indexOfFirst = content.indexOf("---");
|
||||
if (indexOfFirst === -1) {
|
||||
return content
|
||||
@ -112,7 +111,7 @@ export class Transformer {
|
||||
}
|
||||
|
||||
/* Normalize File Names */
|
||||
normalizeFileName = (filename) => {
|
||||
static normalizeFileName = (filename) => {
|
||||
let processedFileName = filename.replace(".md", "");
|
||||
processedFileName = processedFileName.replace('(', '').replace(')', '')
|
||||
processedFileName = processedFileName.split(" ").join("-")
|
||||
@ -132,7 +131,7 @@ export class Transformer {
|
||||
}
|
||||
|
||||
/* Parse file name from path then sanitize it */
|
||||
parseFileNameFromPath = (filepath) => {
|
||||
static parseFileNameFromPath = (filepath) => {
|
||||
if (typeof filepath === 'string' && filepath.includes("/")) {
|
||||
const parsedFileFromPath = filepath.split("/")[filepath.split("/").length - 1]
|
||||
return parsedFileFromPath.replace(".md", "")
|
||||
@ -143,17 +142,16 @@ export class Transformer {
|
||||
}
|
||||
|
||||
/* Pair provided and existing Filenames*/
|
||||
getInternalLinks = (aFilePath) => {
|
||||
static getInternalLinks = (aFilePath) => {
|
||||
const fileContent = Node.readFileSync(aFilePath);
|
||||
const internalLinks = []
|
||||
const sanitizedContent = this.preprocessThreeDashes(fileContent)
|
||||
const outer_this = this
|
||||
const sanitizedContent = Transformer.preprocessThreeDashes(fileContent)
|
||||
unified()
|
||||
.use(markdown, { gfm: true })
|
||||
.use(wikiLinkPlugin, {
|
||||
pageResolver: function(pageName) {
|
||||
|
||||
// let name = [this.parseFileNameFromPath(pageName)];
|
||||
// let name = [Transformer.parseFileNameFromPath(pageName)];
|
||||
|
||||
let canonicalSlug;
|
||||
if (pageName.includes('#')) {
|
||||
@ -163,14 +161,14 @@ export class Transformer {
|
||||
// Meaning it in form of #Heading1 --> slug will be this file slug
|
||||
canonicalSlug = toSlug(aFilePath)
|
||||
} else {
|
||||
canonicalSlug = outer_this.pageResolver(tempSlug)[0].split('#')[0]
|
||||
canonicalSlug = Transformer.pageResolver(tempSlug)[0].split('#')[0]
|
||||
}
|
||||
} else {
|
||||
canonicalSlug = outer_this.pageResolver(pageName)[0].split('#')[0]
|
||||
canonicalSlug = Transformer.pageResolver(pageName)[0].split('#')[0]
|
||||
}
|
||||
|
||||
const backLink = {
|
||||
title: outer_this.parseFileNameFromPath(toFilePath(canonicalSlug)),
|
||||
title: Transformer.parseFileNameFromPath(toFilePath(canonicalSlug)),
|
||||
slug: canonicalSlug,
|
||||
shortSummary: canonicalSlug
|
||||
}
|
||||
@ -183,7 +181,7 @@ export class Transformer {
|
||||
}
|
||||
,
|
||||
hrefTemplate: (permalink) => {
|
||||
return this.hrefTemplate(permalink)
|
||||
return Transformer.hrefTemplate(permalink)
|
||||
},
|
||||
|
||||
aliasDivider: "|"
|
||||
|
20
lib/utils.js
20
lib/utils.js
@ -33,18 +33,15 @@ 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)
|
||||
//console.log("currentFilePath: ", currentFilePath)
|
||||
|
||||
var fileContent = Node.readFileSync(currentFilePath)
|
||||
|
||||
//const currentFileFrontMatter = t.getFrontMatterData(fileContent)
|
||||
//const currentFileFrontMatter = Transformer.getFrontMatterData(fileContent)
|
||||
// console.log("===============\n\nFile is scanning: ", slug)
|
||||
const [htmlContent] = t.getHtmlContent(fileContent)
|
||||
const [htmlContent] = Transformer.getHtmlContent(fileContent)
|
||||
// console.log("==================================")
|
||||
//console.log("hrmlcontents and backlinks")
|
||||
return {
|
||||
@ -109,7 +106,6 @@ 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);
|
||||
@ -122,14 +118,14 @@ export function constructGraphData() {
|
||||
.forEach(aFilePath => {
|
||||
// const {currentFilePath} = getFileNames(filename)
|
||||
const aNode = {
|
||||
title: t.parseFileNameFromPath(aFilePath),
|
||||
title: Transformer.parseFileNameFromPath(aFilePath),
|
||||
slug: toSlug(aFilePath),
|
||||
shortSummary: getShortSummary(toSlug(aFilePath))
|
||||
}
|
||||
nodes.push(aNode)
|
||||
|
||||
// console.log("Constructing graph for node: " + aFilePath )
|
||||
const internalLinks = t.getInternalLinks(aFilePath)
|
||||
const internalLinks = Transformer.getInternalLinks(aFilePath)
|
||||
internalLinks.forEach(aLink => {
|
||||
|
||||
if (aLink.slug === null || aLink.slug.length === 0) return
|
||||
@ -153,14 +149,13 @@ 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: t.parseFileNameFromPath(toFilePath(aNode.slug)),
|
||||
label: Transformer.parseFileNameFromPath(toFilePath(aNode.slug)),
|
||||
}
|
||||
}
|
||||
))
|
||||
@ -234,12 +229,11 @@ export function getDirectoryData() {
|
||||
let _counter = 0;
|
||||
|
||||
export function convertObject(thisObject) {
|
||||
const t = new Transformer()
|
||||
const children = []
|
||||
|
||||
let routerPath = getAllSlugs().find(slug => {
|
||||
const fileName = t.parseFileNameFromPath(toFilePath(slug))
|
||||
return t.normalizeFileName(fileName) === t.normalizeFileName(thisObject.name)
|
||||
const fileName = Transformer.parseFileNameFromPath(toFilePath(slug))
|
||||
return Transformer.normalizeFileName(fileName) === Transformer.normalizeFileName(thisObject.name)
|
||||
}) || null
|
||||
routerPath = routerPath ? '/note/' + routerPath : null
|
||||
const newObject = {
|
||||
|
Loading…
Reference in New Issue
Block a user