diff --git a/lib/convert.js b/lib/convert.js index dee31e4..a92ec3e 100644 --- a/lib/convert.js +++ b/lib/convert.js @@ -68,16 +68,45 @@ function getChapterString(rawChapterDoc) { } function cleanContentEl(el) { - // Remove Next Chapter and Previous Chapter

s + // Remove initial Next Chapter and Previous Chapter

el.removeChild(el.firstElementChild); - el.removeChild(el.lastElementChild); - // Remove redundant dir="ltr" + // Remove everything after the last

(e.g. analytics

s) + const lastP = el.querySelector("p:last-of-type"); + while (el.lastElementChild !== lastP) { + el.removeChild(el.lastElementChild); + } + + // Remove empty

s or Last Chapter/Next Chapter

s + while (isEmptyOrGarbage(el.lastElementChild)) { + el.removeChild(el.lastElementChild); + } + + // Remove redundant dir="ltr" and align="LEFT" and style="text-align: left;" Array.prototype.forEach.call(el.children, function (child) { if (child.getAttribute("dir") === "ltr") { child.removeAttribute("dir"); } + if ((child.getAttribute("align") || "").toLowerCase() === "left") { + child.removeAttribute("align"); + } + if (child.getAttribute("style") === "text-align:left;") { + child.removeAttribute("style"); + } + }); + + // Remove empty s and s + const ems = el.querySelectorAll("em, i"); + Array.prototype.forEach.call(ems, function (em) { + if (em.textContent.trim() === "") { + em.parentNode.removeChild(em); + } }); return el; } + +function isEmptyOrGarbage(el) { + const text = el.textContent.trim(); + return text === "" || text.startsWith("Last Chapter") || text.startsWith("Next Chapter"); +}