37 lines
998 B
JavaScript
37 lines
998 B
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, rootLevel, context) {
|
|
const includePath = originalDoc.tail()
|
|
const includedDoc = useDocument(createFileReader(includePath))
|
|
const { next, head, tail, level } = includedDoc
|
|
|
|
const node = {
|
|
type: originalDoc.head(),
|
|
class: '',
|
|
children: []
|
|
}
|
|
|
|
|
|
const root = path.dirname(context.filePath)
|
|
const originalFilepath = context.filePath
|
|
context.filePath = includePath
|
|
|
|
process.chdir(path.dirname(originalFilepath))
|
|
while (await next()) {
|
|
if (!head()) continue
|
|
const block = head()
|
|
|
|
if (!knownNodes[block]) continue
|
|
node.children.push(await knownNodes[block](includedDoc, level(), context))
|
|
}
|
|
|
|
process.chdir(path.dirname(originalFilepath))
|
|
|
|
return node
|
|
}
|