Skip to content

Commit

Permalink
Update MarkdownComponent.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Feb 26, 2024
1 parent a6fb19a commit f4dd101
Showing 1 changed file with 26 additions and 36 deletions.
62 changes: 26 additions & 36 deletions src/components/MarkdownComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,16 @@ function Youtube({ arg }: { arg: string }) {
function Files({ body }: { body?: string }) {
/* Takes an ascii string of indented folder and file paths:
const from = `/meta
/2022
all.json
posts.json
videos.json
/2023
all.json
posts.json
/2022
all.json
index.json`
posts.json
videos.json
/2023
all.json
posts.json
all.json
index.json`
// and returns a nested object representing the file structure:
const to = {
meta: {
Expand All @@ -115,36 +115,26 @@ function Files({ body }: { body?: string }) {
}
}
*/
function parseFileStructure(ascii: string) {
const parseLineIndentation = (line:string) => {
const match = line.match(/^(\s*)/)
return match ? match[0].length / 2 : 0
}
const lines = ascii.trim().split("\n")
const root = {}

let currentPath: any = [root]
lines.forEach((line) => {
function parseFileStructure(ascii:string, indent:number = 2) {
const lines = ascii.split('\n')
const root = { _: [] }
const stack = [root]

for (const line of lines) {
const depth = line.search(/\S/)/indent
const name = line.trim()
const isFile = name.includes(".")
const level = parseLineIndentation(line)

// Navigate up the currentPath to find the current level's parent
while (level < currentPath.length - 1) {
currentPath.pop()
}

if (isFile) {
// Current Line is a file, add it to the files array (denoted by "_")
currentPath[level]._ ??= []
currentPath[level]._.push(name)
const parent:{[name:string]:any} = stack[depth]

if (name.includes('.')) {
parent._.push(name)
} else {
const dir = name.replace("/", "")
// Current Line is a folder, create a new object and update the currentPath
currentPath[level][dir] ??= {}
currentPath.push(currentPath[level][dir])
const newObj = { _: [] }
const dir = name.substring(1)
parent[dir] = newObj
stack.length = depth + 1
stack.push(newObj)
}
})
}
return root
}

Expand Down

0 comments on commit f4dd101

Please sign in to comment.