From a1826fdcba53abbb224faadbf79c6af6e469bf4a Mon Sep 17 00:00:00 2001 From: Triston Armstrong Date: Tue, 26 Dec 2023 23:36:36 -0600 Subject: [PATCH] convert all new class usages to use this. --- lib/transformer.js | 11 +++++------ lib/utils.js | 41 +++++++++++++++++++++-------------------- pages/notes/[id].js | 18 ++++++------------ pages/notes/index.js | 17 +++++------------ 4 files changed, 37 insertions(+), 50 deletions(-) diff --git a/lib/transformer.js b/lib/transformer.js index f6a1614..b04108e 100644 --- a/lib/transformer.js +++ b/lib/transformer.js @@ -11,8 +11,7 @@ import rehypePrism from 'rehype-prism-plus' import remarkRehype from 'remark-rehype' import rehypeStringify from 'rehype-stringify' import obsidianImage from './obsidian-image.js' -import { getAllMarkdownFiles, toFilePath, toSlug } from "./utils"; - +import Utils from '../lib/utils.js' export class Transformer { @@ -38,7 +37,7 @@ export class Transformer { static pageResolver = (pageName) => { - const allFileNames = getAllMarkdownFiles() + const allFileNames = Utils.getAllMarkdownFiles() const result = allFileNames.find(aFile => { let parseFileNameFromPath = Transformer.parseFileNameFromPath(aFile); return Transformer.normalizeFileName(parseFileNameFromPath) === Transformer.normalizeFileName(pageName) @@ -52,7 +51,7 @@ export class Transformer { } // console.log("Internal Link resolved: [" + pageName + "] ==> [" + temp[0] +"]") - return (result !== undefined && result.length > 0) ? [toSlug(result)] : ["/"] + return (result !== undefined && result.length > 0) ? [Utils.toSlug(result)] : ["/"] } static hrefTemplate = (permalink) => { @@ -159,7 +158,7 @@ export class Transformer { const tempSlug = pageName.split('#')[0] if (tempSlug.length === 0) { // Meaning it in form of #Heading1 --> slug will be this file slug - canonicalSlug = toSlug(aFilePath) + canonicalSlug = Utils.toSlug(aFilePath) } else { canonicalSlug = Transformer.pageResolver(tempSlug)[0].split('#')[0] } @@ -168,7 +167,7 @@ export class Transformer { } const backLink = { - title: Transformer.parseFileNameFromPath(toFilePath(canonicalSlug)), + title: Transformer.parseFileNameFromPath(Utils.toFilePath(canonicalSlug)), slug: canonicalSlug, shortSummary: canonicalSlug } diff --git a/lib/utils.js b/lib/utils.js index f6140a2..b679eb4 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -18,13 +18,13 @@ class Util { } getContent(slug) { - let currentFilePath = toFilePath(slug) + let currentFilePath = this.toFilePath(slug) if (currentFilePath === undefined || currentFilePath == null) return null return Node.readFileSync(currentFilePath) } getShortSummary(slug) { - const content = getContent(slug) + const content = this.getContent(slug) if (content === undefined || content === null) { return } @@ -41,7 +41,7 @@ class Util { getSinglePost(slug) { // List of filenames that will provide existing links to wikilink - let currentFilePath = toFilePath(slug) + let currentFilePath = this.toFilePath(slug) //console.log("currentFilePath: ", currentFilePath) var fileContent = Node.readFileSync(currentFilePath) @@ -60,7 +60,7 @@ class Util { } toFilePath(slug) { - return cachedSlugMap[slug] + return this.cachedSlugMap[slug] } getSlugHashMap() { @@ -71,8 +71,8 @@ class Util { // is not SEO-friendly and make url look ugly ==> I chose this const slugMap = new Map() - getAllMarkdownFiles().map(aFile => { - const aSlug = toSlug(aFile); + this.getAllMarkdownFiles().map(aFile => { + const aSlug = this.toSlug(aFile); // if (slugMap.has(aSlug)) { // slugMap[aSlug].push(aFile) // } else { @@ -113,7 +113,7 @@ class Util { const data = fs.readFileSync(filepath); return JSON.parse(String(data)) } else { - const filePaths = getAllMarkdownFiles(); + const filePaths = this.getAllMarkdownFiles(); const edges = [] const nodes = [] filePaths @@ -121,8 +121,8 @@ class Util { // const {currentFilePath} = getFileNames(filename) const aNode = { title: Transformer.parseFileNameFromPath(aFilePath), - slug: toSlug(aFilePath), - shortSummary: getShortSummary(toSlug(aFilePath)) + slug: this.toSlug(aFilePath), + shortSummary: this.getShortSummary(this.toSlug(aFilePath)) } nodes.push(aNode) @@ -133,7 +133,7 @@ class Util { if (aLink.slug === null || aLink.slug.length === 0) return const anEdge = { - source: toSlug(aFilePath), + source: this.toSlug(aFilePath), target: aLink.slug, } edges.push(anEdge) @@ -156,7 +156,7 @@ class Util { { data: { id: aNode.slug.toString(), - label: Transformer.parseFileNameFromPath(toFilePath(aNode.slug)), + label: Transformer.parseFileNameFromPath(this.toFilePath(aNode.slug)), } } )) @@ -219,32 +219,32 @@ class Util { const markdownFolder = Node.getMarkdownFolder() const markdownFiles = Node.getFiles(markdownFolder) const filePaths = markdownFiles.filter(file => !(file.endsWith("index") || file.endsWith("sidebar"))) - return filePaths.map(f => toSlug(f)) + return filePaths.map(f => this.toSlug(f)) } getDirectoryData() { const filteredDirectory = dirTree(Node.getMarkdownFolder(), { extensions: /\.md/, exclude: [/\.git/, /\.obsidian/] }); - return convertObject(filteredDirectory) + return this.convertObject(filteredDirectory) } convertObject(thisObject) { const children = [] - let routerPath = getAllSlugs().find(slug => { - const fileName = Transformer.parseFileNameFromPath(toFilePath(slug)) + let routerPath = this.getAllSlugs().find(slug => { + const fileName = Transformer.parseFileNameFromPath(this.toFilePath(slug)) return Transformer.normalizeFileName(fileName) === Transformer.normalizeFileName(thisObject.name) }) || null routerPath = routerPath ? '/note/' + routerPath : null const newObject = { name: thisObject.name, children: children, - id: (_counter++).toString(), + id: (this._counter++).toString(), routePath: routerPath || null }; if (thisObject.children != null && thisObject.children.length > 0) { thisObject.children.forEach(aChild => { - const newChild = convertObject(aChild) + const newChild = this.convertObject(aChild) children.push(newChild) }) return newObject; @@ -253,18 +253,19 @@ class Util { } } - flat(array) { + flat = (array) => { var result = []; + const outerThis = this array.forEach(function(a) { result.push(a); if (Array.isArray(a.children)) { - result = result.concat(flat(a.children)); + result = result.concat(outerThis.flat(a.children)); } }); return result; } getFlattenArray(thisObject) { - return flat(thisObject.children) + return this.flat(thisObject.children) } } diff --git a/pages/notes/[id].js b/pages/notes/[id].js index 9966b50..eb6b61b 100644 --- a/pages/notes/[id].js +++ b/pages/notes/[id].js @@ -1,12 +1,6 @@ import Head from "next/head"; import Layout from "../../components/Layout"; -import { - getAllSlugs, - getSinglePost, - convertObject, - getDirectoryData, - constructGraphData -} from "../../lib/utils"; +import Util from "../../lib/utils" import FolderTree from "../../components/FolderTree"; import { getFlattenArray } from "../../lib/utils"; import MDContent from "../../components/MDContent"; @@ -31,7 +25,7 @@ export default function Home({ note, backLinks, fileNames, tree, flattenNodes }) } export async function getStaticPaths() { - const allPostsData = getAllSlugs(); + const allPostsData = Util.getAllSlugs(); const paths = allPostsData.map(p => ({ params: { id: p } })) return { @@ -42,10 +36,10 @@ export async function getStaticPaths() { export function getStaticProps({ params }) { - const { nodes, edges } = constructGraphData() - const note = getSinglePost(params.id); - const tree = convertObject(getDirectoryData()); - const flattenNodes = getFlattenArray(tree) + const { nodes, edges } = Util.constructGraphData() + const note = Util.getSinglePost(params.id); + const tree = Util.convertObject(Util.getDirectoryData()); + const flattenNodes = Util.getFlattenArray(tree) const listOfEdges = edges.filter(anEdge => anEdge.target === params.id) const internalLinks = listOfEdges.map(anEdge => nodes.find(aNode => aNode.slug === anEdge.source)).filter(element => element !== undefined) diff --git a/pages/notes/index.js b/pages/notes/index.js index 7958e85..887d678 100644 --- a/pages/notes/index.js +++ b/pages/notes/index.js @@ -1,12 +1,5 @@ import Layout from "../../components/Layout"; -import { - getSinglePost, - getDirectoryData, - convertObject, - getFlattenArray, - getLocalGraphData, - constructGraphData -} from "../../lib/utils"; +import Util from '../../lib/utils' import FolderTree from "../../components/FolderTree"; import MDContent from "../../components/MDContent"; @@ -28,10 +21,10 @@ export default function Home({ content, tree, flattenNodes, backLinks }) { export function getStaticProps() { - const { nodes, edges } = constructGraphData() - const tree = convertObject(getDirectoryData()); - const contentData = getSinglePost("index"); - const flattenNodes = getFlattenArray(tree) + const { nodes, edges } = Util.constructGraphData() + const tree = Util.convertObject(Util.getDirectoryData()); + const contentData = Util.getSinglePost("index"); + const flattenNodes = Util.getFlattenArray(tree) const listOfEdges = edges.filter(anEdge => anEdge.target === "index") const internalLinks = listOfEdges.map(anEdge => nodes.find(aNode => aNode.slug === anEdge.source)).filter(element => element !== undefined) const backLinks = [...new Set(internalLinks)]