fix all lint issues in utils file
This commit is contained in:
parent
af6e6d87c1
commit
e57213bdf3
@ -7,7 +7,8 @@ module.exports = {
|
|||||||
'eslint:recommended',
|
'eslint:recommended',
|
||||||
'standard-with-typescript',
|
'standard-with-typescript',
|
||||||
'plugin:@typescript-eslint/recommended',
|
'plugin:@typescript-eslint/recommended',
|
||||||
'plugin:react/recommended'
|
'plugin:react/recommended',
|
||||||
|
'next/core-web-vitals'
|
||||||
],
|
],
|
||||||
overrides: [
|
overrides: [
|
||||||
{
|
{
|
||||||
@ -35,9 +36,14 @@ module.exports = {
|
|||||||
'@typescript-eslint'
|
'@typescript-eslint'
|
||||||
],
|
],
|
||||||
rules: {
|
rules: {
|
||||||
'InferGetStaticPropsType': 'warn',
|
'no-trailing-spaces': 'warn',
|
||||||
'space-before-function-paren': 'off',
|
'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/space-before-function-paren': 'off',
|
||||||
|
'@typescript-eslint/explicit-function-return-type': 'warn',
|
||||||
'@typescript-eslint/strict-boolean-expressions': [
|
'@typescript-eslint/strict-boolean-expressions': [
|
||||||
2,
|
2,
|
||||||
{
|
{
|
||||||
|
125
lib/utils.js
125
lib/utils.js
@ -1,24 +1,28 @@
|
|||||||
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'
|
||||||
|
|
||||||
const dirTree = require("directory-tree");
|
import * as dirTree from 'directory-tree'
|
||||||
|
|
||||||
class Util {
|
class Util {
|
||||||
_counter;
|
_counter
|
||||||
cachedSlugMap;
|
cachedSlugMap
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this._counter = 0;
|
this._counter = 0
|
||||||
this.cachedSlugMap = this.getSlugHashMap();
|
this.cachedSlugMap = this.getSlugHashMap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {string | null}
|
||||||
|
* */
|
||||||
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
||||||
getContent(slug) {
|
getContent(slug) {
|
||||||
let currentFilePath = this.toFilePath(slug)
|
const 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)
|
||||||
}
|
}
|
||||||
@ -31,8 +35,8 @@ class Util {
|
|||||||
|
|
||||||
const tree = unified().use(markdown)
|
const tree = unified().use(markdown)
|
||||||
.parse(content)
|
.parse(content)
|
||||||
let plainText = toString(tree)
|
const plainText = toString(tree)
|
||||||
return plainText.split(" ").splice(0, 40).join(" ")
|
return plainText.split(' ').splice(0, 40).join(' ')
|
||||||
}
|
}
|
||||||
|
|
||||||
getAllMarkdownFiles() {
|
getAllMarkdownFiles() {
|
||||||
@ -41,22 +45,21 @@ 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 = this.toFilePath(slug)
|
const currentFilePath = this.toFilePath(slug)
|
||||||
//console.log("currentFilePath: ", currentFilePath)
|
// 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)
|
// console.log("===============\n\nFile is scanning: ", slug)
|
||||||
const [htmlContent] = Transformer.getHtmlContent(fileContent)
|
const [htmlContent] = Transformer.getHtmlContent(fileContent)
|
||||||
// console.log("==================================")
|
// console.log("==================================")
|
||||||
//console.log("hrmlcontents and backlinks")
|
// console.log("hrmlcontents and backlinks")
|
||||||
return {
|
return {
|
||||||
id: slug,
|
id: slug,
|
||||||
// ...currentFileFrontMatter,
|
// ...currentFileFrontMatter,
|
||||||
data: htmlContent,
|
data: htmlContent
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
toFilePath(slug) {
|
toFilePath(slug) {
|
||||||
@ -71,8 +74,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()
|
||||||
this.getAllMarkdownFiles().map(aFile => {
|
this.getAllMarkdownFiles().forEach(aFile => {
|
||||||
const aSlug = this.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 {
|
||||||
@ -82,8 +85,8 @@ class Util {
|
|||||||
slugMap[aSlug] = aFile
|
slugMap[aSlug] = aFile
|
||||||
})
|
})
|
||||||
|
|
||||||
const indexFile = "/🌎 Home.md"
|
const indexFile = '/🌎 Home.md'
|
||||||
slugMap['index'] = Node.getMarkdownFolder() + indexFile
|
slugMap.index = Node.getMarkdownFolder() + indexFile
|
||||||
slugMap['/'] = Node.getMarkdownFolder() + indexFile
|
slugMap['/'] = Node.getMarkdownFolder() + indexFile
|
||||||
|
|
||||||
return slugMap
|
return slugMap
|
||||||
@ -94,26 +97,25 @@ class Util {
|
|||||||
const isFile = Node.isFile(filePath)
|
const isFile = Node.isFile(filePath)
|
||||||
const isMarkdownFolder = filePath.includes(markdownFolder)
|
const isMarkdownFolder = filePath.includes(markdownFolder)
|
||||||
|
|
||||||
if (isFile && isMarkdownFolder) {
|
if (isFile && Boolean(isMarkdownFolder)) {
|
||||||
return filePath.replace(markdownFolder, '')
|
return filePath.replace(markdownFolder, '')
|
||||||
.replaceAll('/', '_')
|
.replaceAll('/', '_')
|
||||||
.replaceAll(' ', '+')
|
.replaceAll(' ', '+')
|
||||||
.replaceAll('&', '-')
|
.replaceAll('&', '-')
|
||||||
.replace('.md', '')
|
.replace('.md', '')
|
||||||
} else {
|
} else {
|
||||||
//TODO handle this properly
|
// TODO handle this properly
|
||||||
return '/'
|
return '/'
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
constructGraphData() {
|
constructGraphData() {
|
||||||
const filepath = path.join(process.cwd(), "graph-data.json");
|
const filepath = path.join(process.cwd(), 'graph-data.json')
|
||||||
if (Node.isFile(filepath)) {
|
if (Node.isFile(filepath)) {
|
||||||
const data = fs.readFileSync(filepath);
|
const data = fs.readFileSync(filepath)
|
||||||
return JSON.parse(String(data))
|
return JSON.parse(String(data))
|
||||||
} else {
|
} else {
|
||||||
const filePaths = this.getAllMarkdownFiles();
|
const filePaths = this.getAllMarkdownFiles()
|
||||||
const edges = []
|
const edges = []
|
||||||
const nodes = []
|
const nodes = []
|
||||||
filePaths
|
filePaths
|
||||||
@ -129,12 +131,11 @@ class Util {
|
|||||||
// console.log("Constructing graph for node: " + aFilePath )
|
// console.log("Constructing graph for node: " + aFilePath )
|
||||||
const internalLinks = Transformer.getInternalLinks(aFilePath)
|
const internalLinks = Transformer.getInternalLinks(aFilePath)
|
||||||
internalLinks.forEach(aLink => {
|
internalLinks.forEach(aLink => {
|
||||||
|
|
||||||
if (aLink.slug === null || aLink.slug.length === 0) return
|
if (aLink.slug === null || aLink.slug.length === 0) return
|
||||||
|
|
||||||
const anEdge = {
|
const anEdge = {
|
||||||
source: this.toSlug(aFilePath),
|
source: this.toSlug(aFilePath),
|
||||||
target: aLink.slug,
|
target: aLink.slug
|
||||||
}
|
}
|
||||||
edges.push(anEdge)
|
edges.push(anEdge)
|
||||||
// console.log("Source: " + anEdge.source)
|
// console.log("Source: " + anEdge.source)
|
||||||
@ -143,9 +144,9 @@ class Util {
|
|||||||
// 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,7 +157,7 @@ class Util {
|
|||||||
{
|
{
|
||||||
data: {
|
data: {
|
||||||
id: aNode.slug.toString(),
|
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 => ({
|
const newEdges = edges.map(anEdge => ({
|
||||||
data: {
|
data: {
|
||||||
source: anEdge.source,
|
source: anEdge.source,
|
||||||
target: anEdge.target,
|
target: anEdge.target
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
|
||||||
const existingNodeIDs = newNodes.map(aNode => aNode.data.id)
|
const existingNodeIDs = newNodes.map(aNode => aNode.data.id)
|
||||||
currentNodeId = currentNodeId === 'index' ? '__index' : currentNodeId
|
currentNodeId = currentNodeId === 'index' ? '__index' : currentNodeId
|
||||||
if (currentNodeId != null && existingNodeIDs.includes(currentNodeId)) {
|
if (currentNodeId != null && Boolean(existingNodeIDs.includes(currentNodeId))) {
|
||||||
const outGoingNodeIds = newEdges
|
const outGoingNodeIds = newEdges
|
||||||
.filter(anEdge => anEdge.data.source === currentNodeId)
|
.filter(anEdge => anEdge.data.source === currentNodeId)
|
||||||
.map(anEdge => anEdge.data.target)
|
.map(anEdge => anEdge.data.target)
|
||||||
@ -188,7 +188,7 @@ class Util {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const localNodes = newNodes.filter(aNode => localNodeIds.includes(aNode.data.id))
|
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
|
// Filter self-reference edges
|
||||||
localEdges = localEdges.filter(edge => edge.data.source !== edge.data.target)
|
localEdges = localEdges.filter(edge => edge.data.source !== edge.data.target)
|
||||||
@ -209,64 +209,71 @@ class Util {
|
|||||||
edges: filteredEdges
|
edges: filteredEdges
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getAllSlugs() {
|
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 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 => !(Boolean(file.endsWith('index')) || Boolean(file.endsWith('sidebar'))))
|
||||||
return filePaths.map(f => this.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 this.convertObject(filteredDirectory)
|
return this.convertObject(filteredDirectory)
|
||||||
}
|
}
|
||||||
|
|
||||||
convertObject(thisObject) {
|
convertObject(thisObject) {
|
||||||
const children = []
|
const children = []
|
||||||
|
const slugs = this.getAllSlugs()
|
||||||
|
|
||||||
let routerPath = this.getAllSlugs().find(slug => {
|
function findFunc(slug) {
|
||||||
const fileName = Transformer.parseFileNameFromPath(this.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
|
}
|
||||||
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 = {
|
const newObject = {
|
||||||
name: thisObject.name,
|
name: thisObject.name,
|
||||||
children: children,
|
children,
|
||||||
id: (this._counter++).toString(),
|
id: (this._counter++).toString(),
|
||||||
routePath: routerPath || null
|
routePath: routerPath !== (null | undefined) ? 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 = this.convertObject(aChild)
|
const newChild = this.convertObject(aChild)
|
||||||
children.push(newChild)
|
children.push(newChild)
|
||||||
})
|
})
|
||||||
return newObject;
|
return newObject
|
||||||
} else {
|
} else {
|
||||||
return newObject
|
return newObject
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
flat = (array) => {
|
flat = (array) => {
|
||||||
var result = [];
|
let result = []
|
||||||
const outerThis = this
|
const outerThis = this
|
||||||
|
// eslint-disable-next-line @typescript-eslint/space-before-function-paren
|
||||||
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(outerThis.flat(a.children));
|
result = result.concat(outerThis.flat(a.children))
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
return result;
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
getFlattenArray(thisObject) {
|
getFlattenArray(thisObject) {
|
||||||
return this.flat(thisObject.children)
|
return this.flat(thisObject.children)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new Util()
|
const util = new Util()
|
||||||
|
export default util
|
||||||
|
Loading…
Reference in New Issue
Block a user