62 lines
1.5 KiB
JavaScript
62 lines
1.5 KiB
JavaScript
import knownNodes from './nodes/index.js'
|
|
import { useDocument } from '@terrace-lang/js'
|
|
import { createFileReader } from '@terrace-lang/js/readers/node-readline'
|
|
|
|
import process from 'node:process'
|
|
import path from 'node:path'
|
|
|
|
export default async function (filePath) {
|
|
filePath = path.resolve(filePath)
|
|
const doc = useDocument(createFileReader(filePath), ' ')
|
|
|
|
const page = {
|
|
type: `Page`,
|
|
title: '',
|
|
description: [],
|
|
layout: '',
|
|
headings: [],
|
|
children: []
|
|
}
|
|
|
|
const context = {
|
|
page,
|
|
filePath
|
|
}
|
|
|
|
const originalCWD = process.cwd()
|
|
for await (const node of doc) {
|
|
if (node.isEmpty()) continue
|
|
if (node.is('title')) page.title = node.tail
|
|
else if (node.is('layout')) page.layout = node.tail
|
|
else if (node.is('description')) {
|
|
for await (const child of node.children()) {
|
|
page.description.push(child.content)
|
|
}
|
|
}
|
|
else if (node.is('Section')) {
|
|
page.children.push(await knownNodes.Section(doc, node, context))
|
|
}
|
|
else if (node.is('Include')) {
|
|
page.children.push(await knownNodes.Include(doc, node, context))
|
|
}
|
|
}
|
|
process.chdir(originalCWD)
|
|
|
|
// Structure headings into tree.
|
|
page.headings.forEach((heading, index) => {
|
|
let parent = null
|
|
for (let i = index; i > 0; --i) {
|
|
if (page.headings[i].level === heading.level - 1) {
|
|
parent = page.headings[i]
|
|
break
|
|
}
|
|
}
|
|
|
|
if (parent) parent.children.push(heading)
|
|
})
|
|
|
|
page.headings = page.headings.filter(h => h.level === 2)
|
|
|
|
return page
|
|
}
|