More progress on parser.

This commit is contained in:
Joshua Bemenderfer 2023-02-12 09:00:19 -05:00
parent bc2fc78c96
commit 4e10b07561
9 changed files with 93 additions and 31 deletions

View File

@ -7,10 +7,11 @@
"build": "eleventy" "build": "eleventy"
}, },
"devDependencies": { "devDependencies": {
"@terrace/js": "workspace:*",
"@11ty/eleventy": "^2.0.0", "@11ty/eleventy": "^2.0.0",
"@11ty/eleventy-plugin-vite": "^4.0.0", "@11ty/eleventy-plugin-vite": "^4.0.0",
"@tailwindcss/typography": "^0.5.9", "@tailwindcss/typography": "^0.5.9",
"@terrace/js": "workspace:*",
"marked": "^4.2.12",
"tailwindcss": "^3.2.6", "tailwindcss": "^3.2.6",
"vite": "^3.2.3" "vite": "^3.2.3"
} }

17
docs/parser/helpers.js Normal file
View File

@ -0,0 +1,17 @@
module.exports.contentAsText = async function(doc, rootLevel) {
const { level, next, line, head } = doc
const linesAsArray = []
let contentDepth = -1
while(await next(rootLevel)) {
if (!line()) continue
if (contentDepth === -1) contentDepth = level()
const indent = ''.padStart(level() - contentDepth, ' ')
linesAsArray.push(indent + line())
}
return linesAsArray.join('\n')
}

View File

@ -0,0 +1,20 @@
const { contentAsText } = require('../helpers')
const languages = ['terrace', 'json', 'yaml', 'toml', 'javascript', 'typescript', 'c', 'python']
module.exports = async (doc, rootLevel) => {
const { next, level, line, head, tail } = doc
const codeExample = {
type: 'code-example',
languages: {}
}
while (await next(rootLevel)) {
const languageLevel = level()
if (languages.includes(head())) {
codeExample.languages[head()] = await contentAsText(doc, languageLevel)
}
}
return codeExample
}

View File

@ -0,0 +1,7 @@
const parseNode = require('./node.js')
module.exports.section = parseNode
module.exports.div = parseNode
module.exports.logo = doc => ({ type: `logo` })
module.exports.markdown = require('./markdown.js')
module.exports['code-example'] = require('./code-example.js')

View File

@ -0,0 +1,9 @@
const { contentAsText } = require('../helpers.js')
const marked = require('marked')
module.exports = async (...args) => {
return {
type: `markdown`,
text: marked.parse(await contentAsText(...args))
}
}

26
docs/parser/nodes/node.js Normal file
View File

@ -0,0 +1,26 @@
const knownNodes = require('./index.js')
module.exports = async function (doc, rootLevel) {
const { next, line, match, tail, level, head } = doc
const node = {
type: head(),
class: '',
children: []
}
while (await next(rootLevel)) {
if (!head()) continue
const block = head()
if (match('class')) {
node.class = tail()
continue
}
if (!knownNodes[block]) continue
node.children.push(await knownNodes[block](doc, level()))
}
return node
}

View File

@ -1,13 +1,14 @@
const parseSection = require('./section.js') const knownNodes = require('./nodes/index.js')
module.exports = async function(doc) { module.exports = async function(doc) {
const { next, line, match, tail, level, head } = doc const { next, line, match, tail, level, head } = doc
const pageData = { const pageData = {
type: `page`,
title: '', title: '',
description: [], description: [],
layout: '', layout: '',
sections: [] children: []
} }
while(await next()) { while(await next()) {
@ -21,7 +22,7 @@ module.exports = async function(doc) {
} }
} }
else if (match('section')) { else if (match('section')) {
pageData.sections.push(await parseSection(doc, level())) pageData.children.push(await knownNodes.section(doc, level()))
} }
} }

View File

@ -1,27 +0,0 @@
const parseSection = require('./section.js')
const knownBlocks = {
'logo': () => {},
'div': () => {},
'markdown': () => {},
}
module.exports = async function(doc, rootLevel) {
const { next, line, match, tail, level, head } = doc
const section = {
class: '',
children: []
}
while (await next(rootLevel)) {
if (!head()) continue
const block = head()
if (!knownBlocks[block]) continue
// TODO: Start Parsing
section.children.push(line())
}
return section
}

8
pnpm-lock.yaml generated
View File

@ -18,6 +18,7 @@ importers:
'@11ty/eleventy-plugin-vite': ^4.0.0 '@11ty/eleventy-plugin-vite': ^4.0.0
'@tailwindcss/typography': ^0.5.9 '@tailwindcss/typography': ^0.5.9
'@terrace/js': workspace:* '@terrace/js': workspace:*
marked: ^4.2.12
tailwindcss: ^3.2.6 tailwindcss: ^3.2.6
vite: ^3.2.3 vite: ^3.2.3
devDependencies: devDependencies:
@ -25,6 +26,7 @@ importers:
'@11ty/eleventy-plugin-vite': 4.0.0 '@11ty/eleventy-plugin-vite': 4.0.0
'@tailwindcss/typography': 0.5.9_tailwindcss@3.2.6 '@tailwindcss/typography': 0.5.9_tailwindcss@3.2.6
'@terrace/js': link:../packages/js '@terrace/js': link:../packages/js
marked: 4.2.12
tailwindcss: 3.2.6 tailwindcss: 3.2.6
vite: 3.2.5 vite: 3.2.5
@ -3104,6 +3106,12 @@ packages:
uc.micro: 1.0.6 uc.micro: 1.0.6
dev: true dev: true
/marked/4.2.12:
resolution: {integrity: sha512-yr8hSKa3Fv4D3jdZmtMMPghgVt6TWbk86WQaWhDloQjRSQhMMYCAro7jP7VDJrjjdV8pxVxMssXS8B8Y5DZ5aw==}
engines: {node: '>= 12'}
hasBin: true
dev: true
/maximatch/0.1.0: /maximatch/0.1.0:
resolution: {integrity: sha512-9ORVtDUFk4u/NFfo0vG/ND/z7UQCVZBL539YW0+U1I7H1BkZwizcPx5foFv7LCPcBnm2U6RjFnQOsIvN4/Vm2A==} resolution: {integrity: sha512-9ORVtDUFk4u/NFfo0vG/ND/z7UQCVZBL539YW0+U1I7H1BkZwizcPx5foFv7LCPcBnm2U6RjFnQOsIvN4/Vm2A==}
engines: {node: '>=0.10.0'} engines: {node: '>=0.10.0'}