From cfb8e5e62449b6999d680b1356fb69d149b592d6 Mon Sep 17 00:00:00 2001 From: arunkumar Date: Mon, 1 Feb 2021 11:32:04 +0530 Subject: [PATCH] compileReact function added --- constants/index.js | 17 +++++++++++++++ lib/compileHTML.js | 22 ++++++++++++++------ lib/compileReact.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 31 ++++++++++++++++++++++++++++ package.json | 7 +++++-- 5 files changed, 128 insertions(+), 8 deletions(-) create mode 100644 constants/index.js create mode 100644 lib/compileReact.js diff --git a/constants/index.js b/constants/index.js new file mode 100644 index 0000000..2bdbf38 --- /dev/null +++ b/constants/index.js @@ -0,0 +1,17 @@ +const BOLD = "bold"; +const TEXT = "text"; +const ITALICS = "italics"; +const STRIKETHROUGH = "strikethrough"; +const UNDERLINE = "underline"; +const URL = "url"; +const PARAGRAPH = "paragraph"; + +module.exports = { + BOLD, + TEXT, + ITALICS, + STRIKETHROUGH, + UNDERLINE, + URL, + PARAGRAPH, +}; diff --git a/lib/compileHTML.js b/lib/compileHTML.js index 24ca34d..8fb399b 100644 --- a/lib/compileHTML.js +++ b/lib/compileHTML.js @@ -1,12 +1,21 @@ +const { + BOLD, + TEXT, + ITALICS, + STRIKETHROUGH, + UNDERLINE, + URL, +} = require("../constants"); + function CompileHTML(ast) { let str = "

"; const OpenTags = { - bold: () => ``, - text: () => ``, - italics: () => ``, - strikethrough: () => ``, - underline: () => ``, - url: url => ``, + bold: () => ``, + text: () => ``, + italics: () => ``, + strikethrough: () => ``, + underline: () => ``, + url: url => ``, }; const CloseTags = { @@ -17,6 +26,7 @@ function CompileHTML(ast) { underline: "", url: "", }; + const mapper = node => { node.forEach(element => { if (typeof element === "string") { diff --git a/lib/compileReact.js b/lib/compileReact.js new file mode 100644 index 0000000..457a7d6 --- /dev/null +++ b/lib/compileReact.js @@ -0,0 +1,59 @@ +const h = require("react").createElement; +const { + BOLD, + TEXT, + ITALICS, + STRIKETHROUGH, + UNDERLINE, + URL, + PARAGRAPH, +} = require("../constants"); + +function CompileReact(ast) { + const OpenTags = { + bold: "span", + text: "span", + italics: "span", + strikethrough: "span", + underline: "span", + url: "a", + paragraph: "p", + }; + + const classNames = { + bold: BOLD, + text: TEXT, + italics: ITALICS, + strikethrough: STRIKETHROUGH, + underline: UNDERLINE, + url: URL, + paragraph: PARAGRAPH, + }; + + const mapper = node => + node.map((element, index) => { + if (typeof element === "string") { + return element; + } else if (typeof element === "object") { + const makeProps = () => { + let props = {}; + if (element.type === "url") { + props.href = element.url; + } + props.className = classNames[element.type]; + props.key = element.type + "-" + index; + return props; + }; + + return h( + OpenTags[element.type], + makeProps(), + element.children && mapper(element.children), + ); + } + }); + + return h("p", { className: PARAGRAPH }, mapper(ast)); +} + +module.exports = CompileReact; diff --git a/package-lock.json b/package-lock.json index 8427038..c7792ed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -414,6 +414,12 @@ "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=", "dev": true }, + "js-tokens": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", + "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==", + "dev": true + }, "js-yaml": { "version": "3.14.0", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.0.tgz", @@ -442,6 +448,15 @@ "chalk": "^4.0.0" } }, + "loose-envify": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", + "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==", + "dev": true, + "requires": { + "js-tokens": "^3.0.0 || ^4.0.0" + } + }, "minimatch": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", @@ -502,6 +517,12 @@ "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", "dev": true }, + "object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=", + "dev": true + }, "once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -568,6 +589,16 @@ "safe-buffer": "^5.1.0" } }, + "react": { + "version": "17.0.1", + "resolved": "https://registry.npmjs.org/react/-/react-17.0.1.tgz", + "integrity": "sha512-lG9c9UuMHdcAexXtigOZLX8exLWkW0Ku29qPRU8uhF2R9BN96dLCt0psvzPLlHc5OWkgymP3qwTRgbnw5BKx3w==", + "dev": true, + "requires": { + "loose-envify": "^1.1.0", + "object-assign": "^4.1.1" + } + }, "readdirp": { "version": "3.5.0", "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.5.0.tgz", diff --git a/package.json b/package.json index d260ba4..b3515e4 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,10 @@ "license": "ISC", "devDependencies": { "chai": "^4.2.0", - "html-format": "^1.0.1", - "mocha": "^8.2.1" + "mocha": "^8.2.1", + "react": "*" + }, + "peerDependencies": { + "react": "*" } }