Skip to content

Instantly share code, notes, and snippets.

@gijsk
Created September 24, 2025 10:54
Show Gist options
  • Select an option

  • Save gijsk/c3fcb07d3d193a88626e455adafca49c to your computer and use it in GitHub Desktop.

Select an option

Save gijsk/c3fcb07d3d193a88626e455adafca49c to your computer and use it in GitHub Desktop.
Alt phrasing manipulation loop
function postProcessParagraph(p) {
// Trim leading and trailing whitespace.
while (p.firstChild && this._isWhitespace(p.firstChild)) {
p.firstChild.remove();
}
while (p.lastChild && this._isWhitespace(p.lastChild)) {
p.lastChild.remove();
}
// If there's nothing left, drop it.
if (!p.firstChild) {
p.remove();
}
}
// Turn all divs that don't have children block level elements into p's
if (node.tagName === "DIV") {
// Put phrasing content into paragraphs.
var childNode = node.firstChild;
var p = null;
while (childNode) {
var nextSibling = childNode.nextSibling;
if (this._isPhrasingContent(childNode)) {
if (!p) {
p = doc.createElement("p");
node.replaceChild(p, childNode);
}
p.appendChild(childNode);
} else if (p) {
postProcessParagraph(p);
p = null;
}
childNode = nextSibling;
}
if (p) {
postProcessParagraph(p);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment