convert all new class usages to use this.

This commit is contained in:
Triston Armstrong 2023-12-26 23:36:36 -06:00
parent cf93ff3231
commit a1826fdcba
4 changed files with 37 additions and 50 deletions

View File

@ -11,8 +11,7 @@ import rehypePrism from 'rehype-prism-plus'
import remarkRehype from 'remark-rehype' import remarkRehype from 'remark-rehype'
import rehypeStringify from 'rehype-stringify' import rehypeStringify from 'rehype-stringify'
import obsidianImage from './obsidian-image.js' import obsidianImage from './obsidian-image.js'
import { getAllMarkdownFiles, toFilePath, toSlug } from "./utils"; import Utils from '../lib/utils.js'
export class Transformer { export class Transformer {
@ -38,7 +37,7 @@ export class Transformer {
static pageResolver = (pageName) => { static pageResolver = (pageName) => {
const allFileNames = getAllMarkdownFiles() const allFileNames = Utils.getAllMarkdownFiles()
const result = allFileNames.find(aFile => { const result = allFileNames.find(aFile => {
let parseFileNameFromPath = Transformer.parseFileNameFromPath(aFile); let parseFileNameFromPath = Transformer.parseFileNameFromPath(aFile);
return Transformer.normalizeFileName(parseFileNameFromPath) === Transformer.normalizeFileName(pageName) return Transformer.normalizeFileName(parseFileNameFromPath) === Transformer.normalizeFileName(pageName)
@ -52,7 +51,7 @@ export class Transformer {
} }
// console.log("Internal Link resolved: [" + pageName + "] ==> [" + temp[0] +"]") // 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) => { static hrefTemplate = (permalink) => {
@ -159,7 +158,7 @@ export class Transformer {
const tempSlug = pageName.split('#')[0] const tempSlug = pageName.split('#')[0]
if (tempSlug.length === 0) { if (tempSlug.length === 0) {
// 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 = Utils.toSlug(aFilePath)
} else { } else {
canonicalSlug = Transformer.pageResolver(tempSlug)[0].split('#')[0] canonicalSlug = Transformer.pageResolver(tempSlug)[0].split('#')[0]
} }
@ -168,7 +167,7 @@ export class Transformer {
} }
const backLink = { const backLink = {
title: Transformer.parseFileNameFromPath(toFilePath(canonicalSlug)), title: Transformer.parseFileNameFromPath(Utils.toFilePath(canonicalSlug)),
slug: canonicalSlug, slug: canonicalSlug,
shortSummary: canonicalSlug shortSummary: canonicalSlug
} }

View File

@ -18,13 +18,13 @@ class Util {
} }
getContent(slug) { getContent(slug) {
let currentFilePath = toFilePath(slug) let currentFilePath = this.toFilePath(slug)
if (currentFilePath === undefined || currentFilePath == null) return null if (currentFilePath === undefined || currentFilePath == null) return null
return Node.readFileSync(currentFilePath) return Node.readFileSync(currentFilePath)
} }
getShortSummary(slug) { getShortSummary(slug) {
const content = getContent(slug) const content = this.getContent(slug)
if (content === undefined || content === null) { if (content === undefined || content === null) {
return return
} }
@ -41,7 +41,7 @@ class Util {
getSinglePost(slug) { getSinglePost(slug) {
// 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 = this.toFilePath(slug)
//console.log("currentFilePath: ", currentFilePath) //console.log("currentFilePath: ", currentFilePath)
var fileContent = Node.readFileSync(currentFilePath) var fileContent = Node.readFileSync(currentFilePath)
@ -60,7 +60,7 @@ class Util {
} }
toFilePath(slug) { toFilePath(slug) {
return cachedSlugMap[slug] return this.cachedSlugMap[slug]
} }
getSlugHashMap() { getSlugHashMap() {
@ -71,8 +71,8 @@ class Util {
// is not SEO-friendly and make url look ugly ==> I chose this // is not SEO-friendly and make url look ugly ==> I chose this
const slugMap = new Map() const slugMap = new Map()
getAllMarkdownFiles().map(aFile => { this.getAllMarkdownFiles().map(aFile => {
const aSlug = toSlug(aFile); const aSlug = this.toSlug(aFile);
// if (slugMap.has(aSlug)) { // if (slugMap.has(aSlug)) {
// slugMap[aSlug].push(aFile) // slugMap[aSlug].push(aFile)
// } else { // } else {
@ -113,7 +113,7 @@ class Util {
const data = fs.readFileSync(filepath); const data = fs.readFileSync(filepath);
return JSON.parse(String(data)) return JSON.parse(String(data))
} else { } else {
const filePaths = getAllMarkdownFiles(); const filePaths = this.getAllMarkdownFiles();
const edges = [] const edges = []
const nodes = [] const nodes = []
filePaths filePaths
@ -121,8 +121,8 @@ class Util {
// const {currentFilePath} = getFileNames(filename) // const {currentFilePath} = getFileNames(filename)
const aNode = { const aNode = {
title: Transformer.parseFileNameFromPath(aFilePath), title: Transformer.parseFileNameFromPath(aFilePath),
slug: toSlug(aFilePath), slug: this.toSlug(aFilePath),
shortSummary: getShortSummary(toSlug(aFilePath)) shortSummary: this.getShortSummary(this.toSlug(aFilePath))
} }
nodes.push(aNode) nodes.push(aNode)
@ -133,7 +133,7 @@ class Util {
if (aLink.slug === null || aLink.slug.length === 0) return if (aLink.slug === null || aLink.slug.length === 0) return
const anEdge = { const anEdge = {
source: toSlug(aFilePath), source: this.toSlug(aFilePath),
target: aLink.slug, target: aLink.slug,
} }
edges.push(anEdge) edges.push(anEdge)
@ -156,7 +156,7 @@ class Util {
{ {
data: { data: {
id: aNode.slug.toString(), 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 markdownFolder = Node.getMarkdownFolder()
const markdownFiles = Node.getFiles(markdownFolder) const markdownFiles = Node.getFiles(markdownFolder)
const filePaths = markdownFiles.filter(file => !(file.endsWith("index") || file.endsWith("sidebar"))) 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() { getDirectoryData() {
const filteredDirectory = dirTree(Node.getMarkdownFolder(), { extensions: /\.md/, exclude: [/\.git/, /\.obsidian/] }); const filteredDirectory = dirTree(Node.getMarkdownFolder(), { extensions: /\.md/, exclude: [/\.git/, /\.obsidian/] });
return convertObject(filteredDirectory) return this.convertObject(filteredDirectory)
} }
convertObject(thisObject) { convertObject(thisObject) {
const children = [] const children = []
let routerPath = getAllSlugs().find(slug => { let routerPath = this.getAllSlugs().find(slug => {
const fileName = Transformer.parseFileNameFromPath(toFilePath(slug)) const fileName = Transformer.parseFileNameFromPath(this.toFilePath(slug))
return Transformer.normalizeFileName(fileName) === Transformer.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 = {
name: thisObject.name, name: thisObject.name,
children: children, children: children,
id: (_counter++).toString(), id: (this._counter++).toString(),
routePath: routerPath || null routePath: routerPath || null
}; };
if (thisObject.children != null && thisObject.children.length > 0) { if (thisObject.children != null && thisObject.children.length > 0) {
thisObject.children.forEach(aChild => { thisObject.children.forEach(aChild => {
const newChild = convertObject(aChild) const newChild = this.convertObject(aChild)
children.push(newChild) children.push(newChild)
}) })
return newObject; return newObject;
@ -253,18 +253,19 @@ class Util {
} }
} }
flat(array) { flat = (array) => {
var result = []; var result = [];
const outerThis = this
array.forEach(function(a) { array.forEach(function(a) {
result.push(a); result.push(a);
if (Array.isArray(a.children)) { if (Array.isArray(a.children)) {
result = result.concat(flat(a.children)); result = result.concat(outerThis.flat(a.children));
} }
}); });
return result; return result;
} }
getFlattenArray(thisObject) { getFlattenArray(thisObject) {
return flat(thisObject.children) return this.flat(thisObject.children)
} }
} }

View File

@ -1,12 +1,6 @@
import Head from "next/head"; import Head from "next/head";
import Layout from "../../components/Layout"; import Layout from "../../components/Layout";
import { import Util from "../../lib/utils"
getAllSlugs,
getSinglePost,
convertObject,
getDirectoryData,
constructGraphData
} from "../../lib/utils";
import FolderTree from "../../components/FolderTree"; import FolderTree from "../../components/FolderTree";
import { getFlattenArray } from "../../lib/utils"; import { getFlattenArray } from "../../lib/utils";
import MDContent from "../../components/MDContent"; import MDContent from "../../components/MDContent";
@ -31,7 +25,7 @@ export default function Home({ note, backLinks, fileNames, tree, flattenNodes })
} }
export async function getStaticPaths() { export async function getStaticPaths() {
const allPostsData = getAllSlugs(); const allPostsData = Util.getAllSlugs();
const paths = allPostsData.map(p => ({ params: { id: p } })) const paths = allPostsData.map(p => ({ params: { id: p } }))
return { return {
@ -42,10 +36,10 @@ export async function getStaticPaths() {
export function getStaticProps({ params }) { export function getStaticProps({ params }) {
const { nodes, edges } = constructGraphData() const { nodes, edges } = Util.constructGraphData()
const note = getSinglePost(params.id); const note = Util.getSinglePost(params.id);
const tree = convertObject(getDirectoryData()); const tree = Util.convertObject(Util.getDirectoryData());
const flattenNodes = getFlattenArray(tree) const flattenNodes = Util.getFlattenArray(tree)
const listOfEdges = edges.filter(anEdge => anEdge.target === params.id) 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) const internalLinks = listOfEdges.map(anEdge => nodes.find(aNode => aNode.slug === anEdge.source)).filter(element => element !== undefined)

View File

@ -1,12 +1,5 @@
import Layout from "../../components/Layout"; import Layout from "../../components/Layout";
import { import Util from '../../lib/utils'
getSinglePost,
getDirectoryData,
convertObject,
getFlattenArray,
getLocalGraphData,
constructGraphData
} from "../../lib/utils";
import FolderTree from "../../components/FolderTree"; import FolderTree from "../../components/FolderTree";
import MDContent from "../../components/MDContent"; import MDContent from "../../components/MDContent";
@ -28,10 +21,10 @@ export default function Home({ content, tree, flattenNodes, backLinks }) {
export function getStaticProps() { export function getStaticProps() {
const { nodes, edges } = constructGraphData() const { nodes, edges } = Util.constructGraphData()
const tree = convertObject(getDirectoryData()); const tree = Util.convertObject(Util.getDirectoryData());
const contentData = getSinglePost("index"); const contentData = Util.getSinglePost("index");
const flattenNodes = getFlattenArray(tree) const flattenNodes = Util.getFlattenArray(tree)
const listOfEdges = edges.filter(anEdge => anEdge.target === "index") 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 internalLinks = listOfEdges.map(anEdge => nodes.find(aNode => aNode.slug === anEdge.source)).filter(element => element !== undefined)
const backLinks = [...new Set(internalLinks)] const backLinks = [...new Set(internalLinks)]