fix all lint issues in utils file

This commit is contained in:
Triston Armstrong 2024-01-07 10:22:37 -06:00
parent af6e6d87c1
commit e57213bdf3
2 changed files with 74 additions and 61 deletions

View File

@ -7,7 +7,8 @@ module.exports = {
'eslint:recommended',
'standard-with-typescript',
'plugin:@typescript-eslint/recommended',
'plugin:react/recommended'
'plugin:react/recommended',
'next/core-web-vitals'
],
overrides: [
{
@ -35,9 +36,14 @@ module.exports = {
'@typescript-eslint'
],
rules: {
'InferGetStaticPropsType': 'warn',
'no-trailing-spaces': 'warn',
'space-before-function-paren': 'off',
'@typescript-eslint/space-before-blocks': 'off',
'no-multi-spaces': 'warn',
'@typescript-eslint/no-this-alias': 'off',
'@typescript-eslint/space-infix-ops': 'warn',
'@typescript-eslint/space-before-function-paren': 'off',
'@typescript-eslint/explicit-function-return-type': 'warn',
'@typescript-eslint/strict-boolean-expressions': [
2,
{

View File

@ -1,24 +1,28 @@
import { Node } from "./node"
import { Transformer } from "./transformer";
import { unified } from "unified";
import markdown from "remark-parse";
import { Node } from './node'
import { Transformer } from './transformer'
import { unified } from 'unified'
import markdown from 'remark-parse'
import { toString } from 'mdast-util-to-string'
import path from "path";
import fs from "fs";
import path from 'path'
import fs from 'fs'
const dirTree = require("directory-tree");
import * as dirTree from 'directory-tree'
class Util {
_counter;
cachedSlugMap;
_counter
cachedSlugMap
constructor() {
this._counter = 0;
this.cachedSlugMap = this.getSlugHashMap();
this._counter = 0
this.cachedSlugMap = this.getSlugHashMap()
}
/**
* @returns {string | null}
* */
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
getContent(slug) {
let currentFilePath = this.toFilePath(slug)
const currentFilePath = this.toFilePath(slug)
if (currentFilePath === undefined || currentFilePath == null) return null
return Node.readFileSync(currentFilePath)
}
@ -31,8 +35,8 @@ class Util {
const tree = unified().use(markdown)
.parse(content)
let plainText = toString(tree)
return plainText.split(" ").splice(0, 40).join(" ")
const plainText = toString(tree)
return plainText.split(' ').splice(0, 40).join(' ')
}
getAllMarkdownFiles() {
@ -41,22 +45,21 @@ class Util {
getSinglePost(slug) {
// List of filenames that will provide existing links to wikilink
let currentFilePath = this.toFilePath(slug)
//console.log("currentFilePath: ", currentFilePath)
const currentFilePath = this.toFilePath(slug)
// console.log("currentFilePath: ", currentFilePath)
var fileContent = Node.readFileSync(currentFilePath)
const fileContent = Node.readFileSync(currentFilePath)
//const currentFileFrontMatter = Transformer.getFrontMatterData(fileContent)
// const currentFileFrontMatter = Transformer.getFrontMatterData(fileContent)
// console.log("===============\n\nFile is scanning: ", slug)
const [htmlContent] = Transformer.getHtmlContent(fileContent)
// console.log("==================================")
//console.log("hrmlcontents and backlinks")
// console.log("hrmlcontents and backlinks")
return {
id: slug,
// ...currentFileFrontMatter,
data: htmlContent,
data: htmlContent
}
}
toFilePath(slug) {
@ -71,8 +74,8 @@ class Util {
// is not SEO-friendly and make url look ugly ==> I chose this
const slugMap = new Map()
this.getAllMarkdownFiles().map(aFile => {
const aSlug = this.toSlug(aFile);
this.getAllMarkdownFiles().forEach(aFile => {
const aSlug = this.toSlug(aFile)
// if (slugMap.has(aSlug)) {
// slugMap[aSlug].push(aFile)
// } else {
@ -82,8 +85,8 @@ class Util {
slugMap[aSlug] = aFile
})
const indexFile = "/🌎 Home.md"
slugMap['index'] = Node.getMarkdownFolder() + indexFile
const indexFile = '/🌎 Home.md'
slugMap.index = Node.getMarkdownFolder() + indexFile
slugMap['/'] = Node.getMarkdownFolder() + indexFile
return slugMap
@ -94,26 +97,25 @@ class Util {
const isFile = Node.isFile(filePath)
const isMarkdownFolder = filePath.includes(markdownFolder)
if (isFile && isMarkdownFolder) {
if (isFile && Boolean(isMarkdownFolder)) {
return filePath.replace(markdownFolder, '')
.replaceAll('/', '_')
.replaceAll(' ', '+')
.replaceAll('&', '-')
.replace('.md', '')
} else {
//TODO handle this properly
// TODO handle this properly
return '/'
}
}
constructGraphData() {
const filepath = path.join(process.cwd(), "graph-data.json");
const filepath = path.join(process.cwd(), 'graph-data.json')
if (Node.isFile(filepath)) {
const data = fs.readFileSync(filepath);
const data = fs.readFileSync(filepath)
return JSON.parse(String(data))
} else {
const filePaths = this.getAllMarkdownFiles();
const filePaths = this.getAllMarkdownFiles()
const edges = []
const nodes = []
filePaths
@ -129,12 +131,11 @@ class Util {
// console.log("Constructing graph for node: " + aFilePath )
const internalLinks = Transformer.getInternalLinks(aFilePath)
internalLinks.forEach(aLink => {
if (aLink.slug === null || aLink.slug.length === 0) return
const anEdge = {
source: this.toSlug(aFilePath),
target: aLink.slug,
target: aLink.slug
}
edges.push(anEdge)
// console.log("Source: " + anEdge.source)
@ -143,9 +144,9 @@ class Util {
// console.log("==============Constructing graph" )
}
)
const data = { nodes, edges };
fs.writeFileSync(filepath, JSON.stringify(data), "utf-8");
return data;
const data = { nodes, edges }
fs.writeFileSync(filepath, JSON.stringify(data), 'utf-8')
return data
}
}
@ -156,7 +157,7 @@ class Util {
{
data: {
id: aNode.slug.toString(),
label: Transformer.parseFileNameFromPath(this.toFilePath(aNode.slug)),
label: Transformer.parseFileNameFromPath(this.toFilePath(aNode.slug))
}
}
))
@ -164,14 +165,13 @@ class Util {
const newEdges = edges.map(anEdge => ({
data: {
source: anEdge.source,
target: anEdge.target,
target: anEdge.target
}
}))
const existingNodeIDs = newNodes.map(aNode => aNode.data.id)
currentNodeId = currentNodeId === 'index' ? '__index' : currentNodeId
if (currentNodeId != null && existingNodeIDs.includes(currentNodeId)) {
if (currentNodeId != null && Boolean(existingNodeIDs.includes(currentNodeId))) {
const outGoingNodeIds = newEdges
.filter(anEdge => anEdge.data.source === currentNodeId)
.map(anEdge => anEdge.data.target)
@ -188,7 +188,7 @@ class Util {
}
const localNodes = newNodes.filter(aNode => localNodeIds.includes(aNode.data.id))
let localEdges = newEdges.filter(edge => localNodeIds.includes(edge.data.source)).filter(edge => localNodeIds.includes(edge.data.target));
let localEdges = newEdges.filter(edge => localNodeIds.includes(edge.data.source)).filter(edge => localNodeIds.includes(edge.data.target))
// Filter self-reference edges
localEdges = localEdges.filter(edge => edge.data.source !== edge.data.target)
@ -209,64 +209,71 @@ class Util {
edges: filteredEdges
}
}
}
getAllSlugs() {
//console.log("\n\nAll Posts are scanning")
// console.log("\n\nAll Posts are scanning")
// Get file names under /posts
const markdownFolder = Node.getMarkdownFolder()
const markdownFiles = Node.getFiles(markdownFolder)
const filePaths = markdownFiles.filter(file => !(file.endsWith("index") || file.endsWith("sidebar")))
const filePaths = markdownFiles.filter(file => !(Boolean(file.endsWith('index')) || Boolean(file.endsWith('sidebar'))))
return filePaths.map(f => this.toSlug(f))
}
getDirectoryData() {
const filteredDirectory = dirTree(Node.getMarkdownFolder(), { extensions: /\.md/, exclude: [/\.git/, /\.obsidian/] });
const filteredDirectory = dirTree(Node.getMarkdownFolder(), { extensions: /\.md/, exclude: [/\.git/, /\.obsidian/] })
return this.convertObject(filteredDirectory)
}
convertObject(thisObject) {
const children = []
const slugs = this.getAllSlugs()
let routerPath = this.getAllSlugs().find(slug => {
function findFunc(slug) {
const fileName = Transformer.parseFileNameFromPath(this.toFilePath(slug))
return Transformer.normalizeFileName(fileName) === Transformer.normalizeFileName(thisObject.name)
}) || null
routerPath = routerPath ? '/notes/' + routerPath : null
}
const foundSlugs = slugs.find(slug => findFunc(slug))
let routerPath = foundSlugs !== (null | undefined) ? foundSlugs : null
routerPath = routerPath !== (null | undefined) ? '/notes/' + routerPath : null
const newObject = {
name: thisObject.name,
children: children,
children,
id: (this._counter++).toString(),
routePath: routerPath || null
};
routePath: routerPath !== (null | undefined) ? routerPath : null
}
if (thisObject.children != null && thisObject.children.length > 0) {
thisObject.children.forEach(aChild => {
const newChild = this.convertObject(aChild)
children.push(newChild)
})
return newObject;
return newObject
} else {
return newObject
}
}
flat = (array) => {
var result = [];
let result = []
const outerThis = this
// eslint-disable-next-line @typescript-eslint/space-before-function-paren
array.forEach(function(a) {
result.push(a);
result.push(a)
if (Array.isArray(a.children)) {
result = result.concat(outerThis.flat(a.children));
result = result.concat(outerThis.flat(a.children))
}
});
return result;
})
return result
}
getFlattenArray(thisObject) {
return this.flat(thisObject.children)
}
}
export default new Util()
const util = new Util()
export default util