Clean up better
This commit is contained in:
parent
8bb41473d2
commit
64de4a27e5
1 changed files with 32 additions and 3 deletions
|
|
@ -68,16 +68,45 @@ function getChapterString(rawChapterDoc) {
|
|||
}
|
||||
|
||||
function cleanContentEl(el) {
|
||||
// Remove Next Chapter and Previous Chapter <p>s
|
||||
// Remove initial Next Chapter and Previous Chapter <p>
|
||||
el.removeChild(el.firstElementChild);
|
||||
el.removeChild(el.lastElementChild);
|
||||
|
||||
// Remove redundant dir="ltr"
|
||||
// Remove everything after the last <p> (e.g. analytics <div>s)
|
||||
const lastP = el.querySelector("p:last-of-type");
|
||||
while (el.lastElementChild !== lastP) {
|
||||
el.removeChild(el.lastElementChild);
|
||||
}
|
||||
|
||||
// Remove empty <p>s or Last Chapter/Next Chapter <p>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 <em>s and <i>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");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue