Get includes working relatively.

This commit is contained in:
Joshua Bemenderfer
2023-03-06 22:22:48 -05:00
parent f42225bd13
commit 79d044ebac
9 changed files with 68 additions and 28 deletions

View File

@@ -2,13 +2,16 @@ 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))
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 { next, line, match, tail, level, head } = doc
const pageData = {
const page = {
type: `Page`,
path: inputPath,
title: '',
description: [],
layout: '',
@@ -16,27 +19,37 @@ export default async function(inputPath) {
children: []
}
const context = {
page,
filePath
}
const originalCWD = process.cwd()
while(await next()) {
if (!line()) continue
if (match('title')) pageData.title = tail()
else if (match('layout')) pageData.layout = tail()
if (match('title')) page.title = tail()
else if (match('layout')) page.layout = tail()
else if (match('description')) {
const l = level()
while(await next(l)) {
pageData.description.push(line(l))
page.description.push(line(l))
}
}
else if (match('Section')) {
pageData.children.push(await knownNodes.Section(doc, level(), pageData))
page.children.push(await knownNodes.Section(doc, level(), context))
}
else if (match('Include')) {
page.children.push(await knownNodes.Include(doc, level(), context))
}
}
process.chdir(originalCWD)
// Structure headings into tree.
pageData.headings.forEach((heading, index) => {
page.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]
if (page.headings[i].level === heading.level - 1) {
parent = page.headings[i]
break
}
}
@@ -44,7 +57,7 @@ export default async function(inputPath) {
if (parent) parent.children.push(heading)
})
pageData.headings = pageData.headings.filter(h => h.level === 2)
page.headings = page.headings.filter(h => h.level === 2)
return pageData
return page
}

View File

@@ -1,6 +1,6 @@
import slugify from '@sindresorhus/slugify'
export default async function (doc, rootLevel, pageData) {
export default async function (doc, rootLevel, context) {
const { next, line, match, tail, level, head } = doc
const headingLevel = +tail().split(' ')[0]
@@ -22,7 +22,7 @@ export default async function (doc, rootLevel, pageData) {
if (match('href')) node.href = tail()
}
pageData.headings.push(node)
context.page.headings.push(node)
return node
}

View File

@@ -2,10 +2,12 @@ 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, ...args) {
const includedDoc = useDocument(createFileReader(originalDoc.tail()))
export default async function (originalDoc, rootLevel, context) {
const includePath = originalDoc.tail()
const includedDoc = useDocument(createFileReader(includePath))
const { next, head, tail, level } = includedDoc
const node = {
@@ -14,13 +16,21 @@ export default async function (originalDoc, rootLevel, ...args) {
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(), ...args))
node.children.push(await knownNodes[block](includedDoc, level(), context))
}
process.chdir(path.dirname(originalFilepath))
return node
}