From 4e10b075618f241f5ae64ccf4e4fbe2e81e597a7 Mon Sep 17 00:00:00 2001 From: Joshua Bemenderfer Date: Sun, 12 Feb 2023 09:00:19 -0500 Subject: [PATCH] More progress on parser. --- docs/package.json | 3 ++- docs/parser/helpers.js | 17 +++++++++++++++++ docs/parser/nodes/code-example.js | 20 ++++++++++++++++++++ docs/parser/nodes/index.js | 7 +++++++ docs/parser/nodes/markdown.js | 9 +++++++++ docs/parser/nodes/node.js | 26 ++++++++++++++++++++++++++ docs/parser/page.js | 7 ++++--- docs/parser/section.js | 27 --------------------------- pnpm-lock.yaml | 8 ++++++++ 9 files changed, 93 insertions(+), 31 deletions(-) create mode 100644 docs/parser/helpers.js create mode 100644 docs/parser/nodes/code-example.js create mode 100644 docs/parser/nodes/index.js create mode 100644 docs/parser/nodes/markdown.js create mode 100644 docs/parser/nodes/node.js delete mode 100644 docs/parser/section.js diff --git a/docs/package.json b/docs/package.json index a7d002b..597b9f0 100644 --- a/docs/package.json +++ b/docs/package.json @@ -7,10 +7,11 @@ "build": "eleventy" }, "devDependencies": { - "@terrace/js": "workspace:*", "@11ty/eleventy": "^2.0.0", "@11ty/eleventy-plugin-vite": "^4.0.0", "@tailwindcss/typography": "^0.5.9", + "@terrace/js": "workspace:*", + "marked": "^4.2.12", "tailwindcss": "^3.2.6", "vite": "^3.2.3" } diff --git a/docs/parser/helpers.js b/docs/parser/helpers.js new file mode 100644 index 0000000..a4518b7 --- /dev/null +++ b/docs/parser/helpers.js @@ -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') +} diff --git a/docs/parser/nodes/code-example.js b/docs/parser/nodes/code-example.js new file mode 100644 index 0000000..34d2a0a --- /dev/null +++ b/docs/parser/nodes/code-example.js @@ -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 +} diff --git a/docs/parser/nodes/index.js b/docs/parser/nodes/index.js new file mode 100644 index 0000000..afaaf9b --- /dev/null +++ b/docs/parser/nodes/index.js @@ -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') diff --git a/docs/parser/nodes/markdown.js b/docs/parser/nodes/markdown.js new file mode 100644 index 0000000..7005bb8 --- /dev/null +++ b/docs/parser/nodes/markdown.js @@ -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)) + } +} diff --git a/docs/parser/nodes/node.js b/docs/parser/nodes/node.js new file mode 100644 index 0000000..ff453f1 --- /dev/null +++ b/docs/parser/nodes/node.js @@ -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 +} diff --git a/docs/parser/page.js b/docs/parser/page.js index e4b9fd1..36aa075 100644 --- a/docs/parser/page.js +++ b/docs/parser/page.js @@ -1,13 +1,14 @@ -const parseSection = require('./section.js') +const knownNodes = require('./nodes/index.js') module.exports = async function(doc) { const { next, line, match, tail, level, head } = doc const pageData = { + type: `page`, title: '', description: [], layout: '', - sections: [] + children: [] } while(await next()) { @@ -21,7 +22,7 @@ module.exports = async function(doc) { } } else if (match('section')) { - pageData.sections.push(await parseSection(doc, level())) + pageData.children.push(await knownNodes.section(doc, level())) } } diff --git a/docs/parser/section.js b/docs/parser/section.js deleted file mode 100644 index a11e437..0000000 --- a/docs/parser/section.js +++ /dev/null @@ -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 -} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2161136..33d8417 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -18,6 +18,7 @@ importers: '@11ty/eleventy-plugin-vite': ^4.0.0 '@tailwindcss/typography': ^0.5.9 '@terrace/js': workspace:* + marked: ^4.2.12 tailwindcss: ^3.2.6 vite: ^3.2.3 devDependencies: @@ -25,6 +26,7 @@ importers: '@11ty/eleventy-plugin-vite': 4.0.0 '@tailwindcss/typography': 0.5.9_tailwindcss@3.2.6 '@terrace/js': link:../packages/js + marked: 4.2.12 tailwindcss: 3.2.6 vite: 3.2.5 @@ -3104,6 +3106,12 @@ packages: uc.micro: 1.0.6 dev: true + /marked/4.2.12: + resolution: {integrity: sha512-yr8hSKa3Fv4D3jdZmtMMPghgVt6TWbk86WQaWhDloQjRSQhMMYCAro7jP7VDJrjjdV8pxVxMssXS8B8Y5DZ5aw==} + engines: {node: '>= 12'} + hasBin: true + dev: true + /maximatch/0.1.0: resolution: {integrity: sha512-9ORVtDUFk4u/NFfo0vG/ND/z7UQCVZBL539YW0+U1I7H1BkZwizcPx5foFv7LCPcBnm2U6RjFnQOsIvN4/Vm2A==} engines: {node: '>=0.10.0'}