cleanup some libs

This commit is contained in:
Triston Armstrong 2024-01-13 23:26:21 -06:00
parent 59e3783cbd
commit 9447c9a405
3 changed files with 34 additions and 16 deletions

View File

@ -1,9 +1,11 @@
import path from 'path' import path from 'path'
import fs from "fs" import fs from "fs"
export class Node { class Node {
_markdownFolder;
_filesList;
static isFile = (filename) => { isFile = (filename) => {
try { try {
return fs.existsSync(filename) return fs.existsSync(filename)
} catch (err) { } catch (err) {
@ -13,36 +15,47 @@ export class Node {
} }
static getFullPath = (folderPath) => { getFullPath = (folderPath) => {
return fs.readdirSync(folderPath).map(fn => path.join(folderPath, fn)) return fs.readdirSync(folderPath).map(fn => path.join(folderPath, fn))
} }
static getFiles = (dir) => { getFiles = (dir) => {
if (this._filesList) return this._filesList
var results = []; var results = [];
var list = fs.readdirSync(dir); var list = fs.readdirSync(dir);
const outterThis = this
list.forEach(function(file) { list.forEach(function(file) {
file = dir + '/' + file; file = dir + '/' + file;
var stat = fs.statSync(file); var stat = fs.statSync(file);
if (stat && stat.isDirectory()) { if (stat && stat.isDirectory()) {
/* Recurse into a subdirectory */ /* Recurse into a subdirectory */
results = results.concat(Node.getFiles(file)); results = results.concat(outterThis.getFiles(file));
} else { } else {
/* Is a file */ /* Is a file */
results.push(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") return fs.readFileSync(fullPath, "utf8")
} }
static getMarkdownFolder = () => { getMarkdownFolder = () => {
if (this._markdownFolder) return this._markdownFolder
const notesDir = path.join(process.cwd(), 'notes') 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) 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

View File

@ -6,7 +6,7 @@ import html from 'remark-html'
// import frontmatter from "remark-frontmatter"; // import frontmatter from "remark-frontmatter";
import externalLinks from 'remark-external-links' import externalLinks from 'remark-external-links'
import highlight from 'remark-highlight.js' import highlight from 'remark-highlight.js'
import { Node } from './node' import { default as Node } from './node'
import rehypePrism from 'rehype-prism-plus' 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'

View File

@ -1,4 +1,4 @@
import { Node } from './node' import { default as Node } from './node'
import { Transformer } from './transformer' import { Transformer } from './transformer'
import { unified } from 'unified' import { unified } from 'unified'
import markdown from 'remark-parse' import markdown from 'remark-parse'
@ -10,11 +10,12 @@ const dirTree = require("directory-tree");
class Util { class Util {
_counter _counter
cachedSlugMap _cachedSlugMap
_directoryData
constructor() { constructor() {
this._counter = 0 this._counter = 0
this.cachedSlugMap = this.getSlugHashMap() this._cachedSlugMap = this.getSlugHashMap()
} }
/** /**
@ -63,7 +64,7 @@ class Util {
} }
toFilePath(slug) { toFilePath(slug) {
return this.cachedSlugMap[slug] return this._cachedSlugMap[slug]
} }
getSlugHashMap() { getSlugHashMap() {
@ -220,9 +221,13 @@ class Util {
return filePaths.map(f => this.toSlug(f)) return filePaths.map(f => this.toSlug(f))
} }
/** Gets all directories - if cached already, gets cached */
getDirectoryData() { getDirectoryData() {
if (this._directoryData) return this._directoryData
const filteredDirectory = dirTree(Node.getMarkdownFolder(), { extensions: /\.md/, exclude: [/\.git/, /\.obsidian/] }) 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) { convertObject(thisObject) {