arunkumar
4 years ago
5 changed files with 70 additions and 2 deletions
@ -1,2 +1,2 @@ |
|||||
test_cases.txt |
|
||||
node_modules/ |
node_modules/ |
||||
|
temp/ |
@ -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; |
@ -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; |
Loading…
Reference in new issue