44 lines
1.3 KiB
JavaScript
44 lines
1.3 KiB
JavaScript
import { useDocument } from '@terrace-lang/js/document'
|
|
import { createFileReader } from '@terrace-lang/js/readers/node-readline'
|
|
import fs from 'fs/promises'
|
|
import path from 'path'
|
|
import process from 'node:process'
|
|
import knownNodes from './index.js'
|
|
|
|
export default async function (originalDoc, rootNode, context) {
|
|
const includePath = rootNode.tail
|
|
const includedDoc = useDocument(createFileReader(includePath))
|
|
|
|
const node = {
|
|
type: rootNode.head,
|
|
class: '',
|
|
children: []
|
|
}
|
|
|
|
const root = path.dirname(context.filePath)
|
|
const originalFilepath = context.filePath
|
|
context.filePath = includePath
|
|
|
|
process.chdir(path.dirname(originalFilepath))
|
|
for await (const childNode of includedDoc) {
|
|
if (childNode.isEmpty()) continue
|
|
if (childNode.is('title')) context.page.title = childNode.tail
|
|
else if (childNode.is('layout')) context.page.layout = childNode.tail
|
|
else if (childNode.is('description')) {
|
|
for await (const grandchild of childNode.children()) {
|
|
context.page.description.push(grandchild.content)
|
|
}
|
|
}
|
|
else if (!childNode.head) continue
|
|
else {
|
|
const block = childNode.head
|
|
if (!knownNodes[block]) continue
|
|
node.children.push(await knownNodes[block](includedDoc, childNode, context))
|
|
}
|
|
}
|
|
|
|
process.chdir(path.dirname(originalFilepath))
|
|
|
|
return node
|
|
}
|