From e76fee2c68fa2a8e13634aee6a1dda4a8694a5c9 Mon Sep 17 00:00:00 2001 From: wholteza Date: Tue, 19 Sep 2023 20:22:26 +0200 Subject: [PATCH] Fixed processing of text rows into children --- .gitignore | 3 +- markdown.js | 117 +++++++++++++++++++++++++-------- notes/1-initial-commit/note.md | 9 +-- 3 files changed, 96 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index 9eb1583..bfc5219 100644 --- a/.gitignore +++ b/.gitignore @@ -65,4 +65,5 @@ node_modules/ # dotenv environment variables file .env -.dist \ No newline at end of file +.dist +temp \ No newline at end of file diff --git a/markdown.js b/markdown.js index 4ef8a9a..cb926a5 100644 --- a/markdown.js +++ b/markdown.js @@ -396,8 +396,7 @@ class Italic extends Symbol { * @returns {boolean} */ static canParse(line) { - const trimmedLine = line.trim(); - return trimmedLine.startsWith("_"); + return line.startsWith("_"); } /** @@ -454,8 +453,7 @@ class Bold extends Symbol { * @returns {boolean} */ static canParse(line) { - const trimmedLine = line.trim(); - return trimmedLine.startsWith("**"); + return line.startsWith("**"); } /** @@ -636,11 +634,27 @@ class MultiLineCode extends Symbol { } } -class Text extends Symbol { +class TextRow extends Symbol { /** * @type {string} */ text = ""; + /** + * @type {Symbol[]} + */ + children = []; + /** + * @type {Symbol[]} + */ + allowedChildren = [ + Bold, + Italic, + ImageLink, + Link, + MultiLineCode, + SingleLineCode, + TextRow, + ]; /** * * @param {string} line @@ -656,7 +670,69 @@ class Text extends Symbol { * @returns {Symbol} */ static create(lineFeed) { - const instance = new Text(); + const instance = new TextRow(); + instance.text = lineFeed.claim(); + return instance; + } + + /** + * + * @param {string} assetDirectory + * @returns {void} + */ + process(assetDirectory) { + for (let i = 0; i < this.text.length; i++) { + if (i === this.text.length - 1) { + const plainText = new PlainText(); + plainText.text = this.text; + this.children = [plainText]; + break; + } + const line = this.text.slice(i); + const symbols = toSymbols(line, assetDirectory, this.allowedChildren); + const symbolsIsOnlyOneTextRow = + symbols.length === 1 && symbols[0] instanceof TextRow; + if (!symbols.length || symbolsIsOnlyOneTextRow) { + continue; + } + const plainText = new PlainText(); + plainText.text = this.text.slice(0, i); + this.children = [plainText, ...symbols]; + break; + } + this.children.forEach((c) => c.process(assetDirectory)); + } + + render() { + return this.children.map((c) => c.render()).join(""); + } +} + +class PlainText extends Symbol { + /** + * @type {string} + */ + text = ""; + /** + * @type {Symbol[]} + */ + children = []; + /** + * + * @param {string} line + * @returns {boolean} + */ + static canParse(line) { + return true; + } + + /** + * + * @param {LineFeed} lineFeed + * @returns {Symbol} + */ + static create(lineFeed) { + const instance = new PlainText(); instance.text = lineFeed.claim(); return instance; } @@ -747,7 +823,6 @@ const getAmountOfTokenInBeginningOfFile = (token, string) => { * @type {Symbol[]} */ const AllSymbols = [ - Paragraph, Heading, UnorderedListItem, OrderedListItem, @@ -757,24 +832,9 @@ const AllSymbols = [ Link, MultiLineCode, SingleLineCode, - Text, -]; -/** - * @type {Symbol[]} - */ -const AllSymbolsExceptParagraph = [ - Paragraph, - Heading, - UnorderedListItem, - OrderedListItem, - Bold, - Italic, - ImageLink, - Link, - MultiLineCode, - SingleLineCode, - Text, + TextRow, ]; + /** * * @param {string} markdown @@ -782,11 +842,16 @@ const AllSymbolsExceptParagraph = [ * @returns {string} */ export const toHtml = (markdown, assetDirectory) => { + // Stage one, markdown to symbols const symbols = toSymbols(markdown, assetDirectory); + // Stage two, expanding symbols console.log("starting processing"); symbols.forEach((s) => s.process(assetDirectory)); + // Stage three, operations based on symbol relationship + + // Stage four, rendering const html = symbols.map((s) => s.render()).join(""); return `
${html}
`; }; @@ -798,13 +863,13 @@ export const toHtml = (markdown, assetDirectory) => { * @param {Symbol[]} allowedSymbols * @returns {Symbol[]} */ -const toSymbols = (markdown, assetDirectory, allowedSymbols) => { +const toSymbols = (markdown, assetDirectory, allowedSymbols = AllSymbols) => { const lineFeed = new LineFeed(markdown); const symbols = []; while (!lineFeed.isEmpty()) { const line = lineFeed.peek(); - const factory = AllSymbols.find((factory) => factory.canParse(line)); + const factory = allowedSymbols.find((factory) => factory.canParse(line)); const symbol = factory.create(lineFeed, assetDirectory); symbols.push(symbol); } diff --git a/notes/1-initial-commit/note.md b/notes/1-initial-commit/note.md index ababd3b..da34e64 100644 --- a/notes/1-initial-commit/note.md +++ b/notes/1-initial-commit/note.md @@ -2,8 +2,7 @@ _2023-09-15_ -I've been thinking a long time about having a place to publicly publish things. -I'm not really into doing that on social media since it would lock my content in their format and make it hard to move anywhere else so i thought i would just write my "things" in plain markdown and then find a way of hosting them online. +I've been thinking a long time about having a place to publicly publish things. I'm not really into doing that on social media since it would lock my content in their format and make it hard to move anywhere else so i thought i would just write my "things" in plain markdown and then find a way of hosting them online. Sure there's a lot of static site generators out there and a couple of them could probably be configured to work the way i want it to. However to me there is a joy in using things that I have built on my own so first i will try building something from scratch. @@ -37,6 +36,8 @@ _Italics_ _Italics_ then text +Text then _italics_ + **Bold** **Bold** then text @@ -74,7 +75,3 @@ const y = () => { [link to the index](https://blog.zacke.dev) with text after it ![alt-text](@asset/git.png) - -rstd -rastd -rstd