import {open, mkdir, writeFile, readdir} from 'node:fs/promises' const inDir = './1dl/' const outDir = './public/pathways/' const files = await readdir(inDir) const syntax = () => { const t = /^\(.*\)/ const s = /^\[(.*?)\]/ const n = /^(-->)/ return {t, s, n} } const parse = async (filepath) => { const file = await open(filepath) const {t, s, n} = syntax() const parsed = [] let currentTitle = "" let currentSection = "" for await (let line of file.readLines()) { line = line.trim() if (!line.length) continue if (line.match(t)) { currentTitle = line.slice(1, -1) parsed.push({title: currentTitle}) continue } if (line.match(s)) { currentSection = line.slice(1, -1) parsed[parsed.length-1][currentSection] = [] continue } if (line.match(n)) { let note = line.slice(2) parsed[parsed.length-1][currentSection].push({note: note}) continue } parsed[parsed.length-1][currentSection].push(line) } return parsed } const pathways = [] for (const file of files) { const parsed = await parse(inDir+file) let filename = file.replace('.1dl', '') pathways.push({slug: filename, ...parsed[0]}) writeFile(outDir + file + '.json', JSON.stringify(parsed)) } writeFile('./src/assets/pathways.json', JSON.stringify(pathways))