From a00ac98f68093fb1b018ceb6265a342a0d02a50c Mon Sep 17 00:00:00 2001 From: arunkumar Date: Thu, 21 Jan 2021 01:07:38 +0530 Subject: [PATCH] ast to html, ast to string functions added --- .gitignore | 4 ++-- lib/compileHTML.js | 37 +++++++++++++++++++++++++++++++++++++ lib/compileText.js | 24 ++++++++++++++++++++++++ package-lock.json | 6 ++++++ package.json | 1 + 5 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 lib/compileHTML.js create mode 100644 lib/compileText.js diff --git a/.gitignore b/.gitignore index 051e46c..5e44615 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -test_cases.txt -node_modules/ \ No newline at end of file +node_modules/ +temp/ \ No newline at end of file diff --git a/lib/compileHTML.js b/lib/compileHTML.js new file mode 100644 index 0000000..24ca34d --- /dev/null +++ b/lib/compileHTML.js @@ -0,0 +1,37 @@ +function CompileHTML(ast) { + let str = "

"; + const OpenTags = { + bold: () => ``, + text: () => ``, + italics: () => ``, + strikethrough: () => ``, + underline: () => ``, + url: url => ``, + }; + + const CloseTags = { + text: "", + bold: "", + italics: "", + strikethrough: "", + underline: "", + url: "", + }; + const mapper = node => { + node.forEach(element => { + if (typeof element === "string") { + str += `${element}`; + } else if (typeof element === "object") { + str += OpenTags[element.type]("url" in element ? element.url : null); + mapper(element.children); + str += CloseTags[element.type]; + } + }); + return ""; + }; + mapper(ast); + str += "

"; + return str; +} + +module.exports = CompileHTML; diff --git a/lib/compileText.js b/lib/compileText.js new file mode 100644 index 0000000..33e5cf3 --- /dev/null +++ b/lib/compileText.js @@ -0,0 +1,24 @@ +function CompileText(ast) { + let str = ""; + const mapper = node => { + node.forEach(element => { + if (typeof element === "string") { + str = str + element; + } else if (typeof element === "object") { + let result = ""; + if (element.type === "url") { + result = mapper(element.children); + str += mapper(element.children) + " " + element.url; + } else { + result = mapper(element.children); + str = str + result; + } + } + }); + return ""; + }; + mapper(ast); + return str; +} + +module.exports = CompileText; diff --git a/package-lock.json b/package-lock.json index e44c175..8427038 100644 --- a/package-lock.json +++ b/package-lock.json @@ -344,6 +344,12 @@ "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==", "dev": true }, + "html-format": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/html-format/-/html-format-1.0.1.tgz", + "integrity": "sha512-ePp+h+akaQiLeCGPefWQ4QJXVXhWx4sU4ZxJVFlaY0AeVgh/tnHGTL27ao09JrdEEelXYMAWi4ynKKheck4tdw==", + "dev": true + }, "inflight": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", diff --git a/package.json b/package.json index 1175e31..d260ba4 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "license": "ISC", "devDependencies": { "chai": "^4.2.0", + "html-format": "^1.0.1", "mocha": "^8.2.1" } }