Browse Source

ast to html, ast to string functions added

develop
arunkumar 4 years ago
parent
commit
a00ac98f68
  1. 2
      .gitignore
  2. 37
      lib/compileHTML.js
  3. 24
      lib/compileText.js
  4. 6
      package-lock.json
  5. 1
      package.json

2
.gitignore

@ -1,2 +1,2 @@
test_cases.txt
node_modules/
temp/

37
lib/compileHTML.js

@ -0,0 +1,37 @@
function CompileHTML(ast) {
let str = "<p>";
const OpenTags = {
bold: () => `<span class="bold">`,
text: () => `<span class="text">`,
italics: () => `<span class="italics">`,
strikethrough: () => `<span class="strikethrough">`,
underline: () => `<span class="underline">`,
url: url => `<a href="${url}" class="url">`,
};
const CloseTags = {
text: "</span>",
bold: "</span>",
italics: "</span>",
strikethrough: "</span>",
underline: "</span>",
url: "</a>",
};
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 += "</p>";
return str;
}
module.exports = CompileHTML;

24
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;

6
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",

1
package.json

@ -10,6 +10,7 @@
"license": "ISC",
"devDependencies": {
"chai": "^4.2.0",
"html-format": "^1.0.1",
"mocha": "^8.2.1"
}
}

Loading…
Cancel
Save