fix a few utils bugs

- unified import no longer default import
- make index file a variable but also be my actual entry file for my notes
- create some variables to make reading easier
- cleanup the file/directory replacement characters (what like "++++" which is ridiculous)
- lots of formatting changes
This commit is contained in:
Triston Armstrong 2023-12-23 20:16:56 -06:00
parent 44c5ca07b2
commit c31428d8e4

View File

@ -1,8 +1,8 @@
import {Node} from "./node" import { 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";
import {toString} from 'mdast-util-to-string' import { toString } from 'mdast-util-to-string'
import path from "path"; import path from "path";
import fs from "fs"; import fs from "fs";
@ -79,20 +79,24 @@ export function getSlugHashMap() {
slugMap[aSlug] = aFile slugMap[aSlug] = aFile
}) })
slugMap['index'] = Node.getMarkdownFolder() + "/index.md" const indexFile = "/🌎 Home.md"
slugMap['/'] = Node.getMarkdownFolder() + "/index.md" slugMap['index'] = Node.getMarkdownFolder() + indexFile
slugMap['/'] = Node.getMarkdownFolder() + indexFile
return slugMap return slugMap
} }
export function toSlug(filePath) { export function toSlug(filePath) {
const markdownFolder = Node.getMarkdownFolder()
const isFile = Node.isFile(filePath)
const isMarkdownFolder = filePath.includes(markdownFolder)
if (Node.isFile(filePath) && filePath.includes(Node.getMarkdownFolder())) { if (isFile && isMarkdownFolder) {
return filePath.replace(Node.getMarkdownFolder(), '') return filePath.replace(markdownFolder, '')
.replaceAll('/', '__') .replaceAll('/', '_')
.replaceAll(' ', '++++') .replaceAll(' ', '%')
.replaceAll('&', 'ambersand') .replaceAll('&', '+')
.replace('.md', '') .replace('.md', '')
} else { } else {
//TODO handle this properly //TODO handle this properly
@ -140,7 +144,7 @@ export function constructGraphData() {
// console.log("==============Constructing graph" ) // console.log("==============Constructing graph" )
} }
) )
const data = {nodes, edges}; const data = { nodes, edges };
fs.writeFileSync(filepath, JSON.stringify(data), "utf-8"); fs.writeFileSync(filepath, JSON.stringify(data), "utf-8");
return data; return data;
} }
@ -149,7 +153,7 @@ export function constructGraphData() {
export function getLocalGraphData(currentNodeId) { export function getLocalGraphData(currentNodeId) {
const {nodes, edges} = constructGraphData() const { nodes, edges } = constructGraphData()
const newNodes = nodes.map(aNode => ( const newNodes = nodes.map(aNode => (
{ {
@ -215,12 +219,14 @@ export function getLocalGraphData(currentNodeId) {
export function getAllSlugs() { export function getAllSlugs() {
//console.log("\n\nAll Posts are scanning") //console.log("\n\nAll Posts are scanning")
// Get file names under /posts // Get file names under /posts
const filePaths = Node.getFiles(Node.getMarkdownFolder()).filter(f => !(f.endsWith("index") || f.endsWith("sidebar"))) 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 => toSlug(f))
} }
export function getDirectoryData() { export function getDirectoryData() {
const filteredDirectory = dirTree(Node.getMarkdownFolder(), {extensions: /\.md/}); const filteredDirectory = dirTree(Node.getMarkdownFolder(), { extensions: /\.md/, exclude: [/\.git/, /\.obsidian/] });
return convertObject(filteredDirectory) return convertObject(filteredDirectory)
} }
@ -254,7 +260,7 @@ export function convertObject(thisObject) {
function flat(array) { function flat(array) {
var result = []; var result = [];
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(flat(a.children));