Reputation: 561
I have a task where I need to format div
data from html like this:
<div>
<h4>Some Title</h4>
<p><a href="somelink.com"><strong>Bold and then</strong> normal</a></p>
<p><a href="somelink2.com">Just normal</a></p>
</div>
To this:
#### Some Title
[**Bold and then** normal](somelink.com) //two spaces after <a> tag for line breaks
[Just normal](somelink2.com)
I have function which replace all <strong>
and <em>
tags, but I dont know what to do with <a>
tags in my case.
const toMarkdown = (text) => {
let one = text.replace(/<em>/g,'*').replace(/<\/em>/g,'*');
let two = one.replace(/<strong>/g,'**').replace(/<\/strong>/g,'**');
return two
}
const html = `<div>
<h4>Some Title</h4>
<p><a href="somelink.com"><strong>Bold and then</strong> normal</a></p>
<p><a href="somelink2.com">Just normal</a></p>
</div>`
const toMarkdown = (text) => {
let one = text.replace(/<em>/g, '*').replace(/<\/em>/g, '*');
let two = one.replace(/<strong>/g, '**').replace(/<\/strong>/g, '**');
return two
}
console.log(toMarkdown(html))
Upvotes: 0
Views: 46
Reputation: 178421
I would do this
const html = `<div><h4>Some Title</h4>
<p><a href="somelink.com"><strong>Bold and then</strong> normal</a> <em>bold</em></p>
<p><a href="somelink2.com">Just normal</a></p>
</div>`
const toMarkdown = (text) => {
let obj = document.createElement("div");
obj.innerHTML = text;
[...obj.querySelectorAll("strong, em")].forEach(ele => ele.parentNode.replaceChild(document.createTextNode("*"+ele.textContent+"*"),ele));
[...obj.querySelectorAll("a")].forEach(ele => ele.parentNode.replaceChild(document.createTextNode("["+ele.textContent+"]("+ele.href+")"),ele));
[...obj.querySelectorAll("h4")].forEach(ele => ele.parentNode.replaceChild(document.createTextNode("####"+ele.textContent),ele));
[...obj.querySelectorAll("p")].forEach(ele => ele.parentNode.replaceChild(document.createTextNode(ele.textContent),ele)); // ignore?
[...obj.querySelectorAll("div")].forEach(ele => ele.parentNode.replaceChild(document.createTextNode(ele.textContent),ele));
return obj
}
console.log(toMarkdown(html).innerHTML)
Upvotes: 1