From 9447c9a405ffc50ee28f9c8c1babde0db3195fca Mon Sep 17 00:00:00 2001 From: Triston Armstrong Date: Sat, 13 Jan 2024 23:26:21 -0600 Subject: [PATCH] cleanup some libs --- lib/node.js | 33 +++++++++++++++++++++++---------- lib/transformer.js | 2 +- lib/utils.js | 15 ++++++++++----- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/lib/node.js b/lib/node.js index 2c93f5c..44fd596 100644 --- a/lib/node.js +++ b/lib/node.js @@ -1,9 +1,11 @@ import path from 'path' import fs from "fs" -export class Node { +class Node { + _markdownFolder; + _filesList; - static isFile = (filename) => { + isFile = (filename) => { try { return fs.existsSync(filename) } catch (err) { @@ -13,36 +15,47 @@ export class Node { } - static getFullPath = (folderPath) => { + getFullPath = (folderPath) => { return fs.readdirSync(folderPath).map(fn => path.join(folderPath, fn)) } - static getFiles = (dir) => { + getFiles = (dir) => { + if (this._filesList) return this._filesList var results = []; var list = fs.readdirSync(dir); + const outterThis = this list.forEach(function(file) { file = dir + '/' + file; var stat = fs.statSync(file); if (stat && stat.isDirectory()) { /* Recurse into a subdirectory */ - results = results.concat(Node.getFiles(file)); + results = results.concat(outterThis.getFiles(file)); } else { /* Is a file */ results.push(file); } }); - return results.filter(f => f.endsWith(".md")) + + const filtered = results.filter(f => f.endsWith(".md")) + + this._filesList = filtered + return this._filesList } - static readFileSync = (fullPath) => { + readFileSync = (fullPath) => { return fs.readFileSync(fullPath, "utf8") } - static getMarkdownFolder = () => { + getMarkdownFolder = () => { + if (this._markdownFolder) return this._markdownFolder const notesDir = path.join(process.cwd(), 'notes') - if (!Node.isFile(notesDir)) { + if (!this.isFile(notesDir)) { console.warn("Notes Directory does not seem to exist: ", notesDir) } - return notesDir + this._markdownFolder = notesDir + return this._markdownFolder } } + +const node = new Node() +export default node diff --git a/lib/transformer.js b/lib/transformer.js index 011309b..8857afa 100644 --- a/lib/transformer.js +++ b/lib/transformer.js @@ -6,7 +6,7 @@ import html from 'remark-html' // import frontmatter from "remark-frontmatter"; import externalLinks from 'remark-external-links' import highlight from 'remark-highlight.js' -import { Node } from './node' +import { default as Node } from './node' import rehypePrism from 'rehype-prism-plus' import remarkRehype from 'remark-rehype' import rehypeStringify from 'rehype-stringify' diff --git a/lib/utils.js b/lib/utils.js index 30a6db0..8034179 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,4 +1,4 @@ -import { Node } from './node' +import { default as Node } from './node' import { Transformer } from './transformer' import { unified } from 'unified' import markdown from 'remark-parse' @@ -10,11 +10,12 @@ const dirTree = require("directory-tree"); class Util { _counter - cachedSlugMap + _cachedSlugMap + _directoryData constructor() { this._counter = 0 - this.cachedSlugMap = this.getSlugHashMap() + this._cachedSlugMap = this.getSlugHashMap() } /** @@ -63,7 +64,7 @@ class Util { } toFilePath(slug) { - return this.cachedSlugMap[slug] + return this._cachedSlugMap[slug] } getSlugHashMap() { @@ -220,9 +221,13 @@ class Util { return filePaths.map(f => this.toSlug(f)) } + /** Gets all directories - if cached already, gets cached */ getDirectoryData() { + if (this._directoryData) return this._directoryData const filteredDirectory = dirTree(Node.getMarkdownFolder(), { extensions: /\.md/, exclude: [/\.git/, /\.obsidian/] }) - return this.convertObject(filteredDirectory) + const convertedDirectoryData = this.convertObject(filteredDirectory) + this._directoryData = convertedDirectoryData + return this._directoryData } convertObject(thisObject) {