Skip to content

Commit

Permalink
Add numbered list support
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaswilburn committed Nov 29, 2023
1 parent ea207a6 commit 60c9cbb
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions paraphrase/htmlToMarkdown.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function recurse(node) {
function recurse(node, settings = { bullet: "*" }) {
var converted = "";
for (var node of node.childNodes) {
switch (node.tagName) {
Expand All @@ -8,36 +8,40 @@ function recurse(node) {
case "H4":
case "H5":
var level = node.tagName.match(/\d+/) * 1;
converted += `${"".padStart(level, "#")} ${recurse(node)}\n\n`;
converted += `${"".padStart(level, "#")} ${recurse(node, settings)}\n\n`;
break;

case "P":
case "BR":
case "UL":
if (node.getAttribute("role") != "presentation") converted += "\n\n";
converted += recurse(node);
converted += recurse(node, settings);
break;

case "OL":
converted += recurse(node, { ...settings, bullet: "1." });
break;

case "LI":
converted += `\n* ${recurse(node)}`;
converted += `\n${settings.bullet} ${recurse(node, settings)}`;
break;

case "A":
converted += `[${recurse(node)}](${node.href})`;
converted += `[${recurse(node, settings)}](${node.href})`;
break;

case "B":
case "STRONG":
if (node.style.fontWeight == "normal") {
converted += recurse(node);
converted += recurse(node, settings);
} else {
converted += `**${recurse(node)}**`;
converted += `**${recurse(node, settings)}**`;
}
break;

case "I":
case "EM":
converted += `*${recurse(node)}*`;
converted += `*${recurse(node, settings)}*`;
break;

case "SPAN":
Expand All @@ -56,14 +60,14 @@ function recurse(node) {
for (var child of node.childNodes) {
tail.append(child.cloneNode(true));
}
converted += recurse(span);
converted += recurse(span, settings);
break;

default:
if (node.nodeType == Node.TEXT_NODE) {
converted += node.textContent;
} else {
converted += recurse(node);
converted += recurse(node, settings);
}
}
}
Expand Down

0 comments on commit 60c9cbb

Please sign in to comment.