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 `