51 lines
1.3 KiB
JavaScript
51 lines
1.3 KiB
JavaScript
import knownNodes from './nodes/index.js'
|
|
import { useDocument } from '@terrace-lang/js'
|
|
import { createFileReader } from '@terrace-lang/js/readers/node-readline'
|
|
|
|
export default async function(inputPath) {
|
|
const doc = useDocument(createFileReader(inputPath))
|
|
const { next, line, match, tail, level, head } = doc
|
|
|
|
const pageData = {
|
|
type: `Page`,
|
|
path: inputPath,
|
|
title: '',
|
|
description: [],
|
|
layout: '',
|
|
headings: [],
|
|
children: []
|
|
}
|
|
|
|
while(await next()) {
|
|
if (!line()) continue
|
|
if (match('title')) pageData.title = tail()
|
|
else if (match('layout')) pageData.layout = tail()
|
|
else if (match('description')) {
|
|
const l = level()
|
|
while(await next(l)) {
|
|
pageData.description.push(line(l))
|
|
}
|
|
}
|
|
else if (match('Section')) {
|
|
pageData.children.push(await knownNodes.Section(doc, level(), pageData))
|
|
}
|
|
}
|
|
|
|
// Structure headings into tree.
|
|
pageData.headings.forEach((heading, index) => {
|
|
let parent = null
|
|
for (let i = index; i > 0; --i) {
|
|
if (pageData.headings[i].level === heading.level - 1) {
|
|
parent = pageData.headings[i]
|
|
break
|
|
}
|
|
}
|
|
|
|
if (parent) parent.children.push(heading)
|
|
})
|
|
|
|
pageData.headings = pageData.headings.filter(h => h.level === 2)
|
|
|
|
return pageData
|
|
}
|