From b1db4d13169571fefc8d3b4d5d280bc2e9505f5a Mon Sep 17 00:00:00 2001 From: wholteza Date: Sat, 16 Sep 2023 21:22:31 +0200 Subject: [PATCH] Bold and italic support --- markdown.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/markdown.js b/markdown.js index e9e72a2..45af79f 100644 --- a/markdown.js +++ b/markdown.js @@ -90,6 +90,70 @@ class Heading extends Symbol { } } +class Italic extends Symbol { + /** + * @type {string} + */ + text = ""; + /** + * + * @param {string} line + * @returns {boolean} + */ + static canParse(line) { + const trimmedLine = line.trim(); + return trimmedLine.startsWith("_") && trimmedLine.endsWith("_"); + } + + /** + * + * @param {LineFeed} lineFeed + * @returns {Symbol} + */ + static create(lineFeed) { + const instance = new Italic(); + const line = lineFeed.claim().trim(); + const lineWithoutUnderscores = line.slice(1, line.length - 1); + instance.text = lineWithoutUnderscores; + return instance; + } + render() { + return `${this.text}`; + } +} + +class Bold extends Symbol { + /** + * @type {string} + */ + text = ""; + /** + * + * @param {string} line + * @returns {boolean} + */ + static canParse(line) { + const trimmedLine = line.trim(); + return trimmedLine.startsWith("**") && trimmedLine.endsWith("**"); + } + + /** + * + * @param {LineFeed} lineFeed + * @returns {Symbol} + */ + static create(lineFeed) { + const instance = new Bold(); + const line = lineFeed.claim().trim(); + const lineWithoutUnderscores = line.slice(2, line.length - 2); + instance.text = lineWithoutUnderscores; + return instance; + } + render() { + return `${this.text}`; + } +} + class CatchAll extends Symbol { /** * @type {string} @@ -119,7 +183,7 @@ class CatchAll extends Symbol { } } -const Factories = [Heading, CatchAll]; +const Factories = [Heading, Bold, Italic, CatchAll]; export const toHtml = (markdown) => { const symbols = toSymbols(markdown);