Skip to content

Commit

Permalink
🎨 Improve HTML clipping siyuan-note/siyuan#13345
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Dec 3, 2024
1 parent ab026d6 commit c900e98
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/Readability.js
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,9 @@ Readability.prototype = {
**/
_grabArticle: function (page) {
this.log("**** grabArticle ****");

this._replaceCustomTags(this._doc);

var doc = this._doc;
var isPaging = page !== null;
page = page ? page : this._doc.body;
Expand Down Expand Up @@ -2369,6 +2372,43 @@ Readability.prototype = {
&& (!node.hasAttribute("aria-hidden") || node.getAttribute("aria-hidden") != "true" || (node.className && node.className.indexOf && node.className.indexOf("fallback-image") !== -1));
},

_replaceCustomTags: function (node) {
// List of block-level elements
const blockLevelElements = [
'address', 'article', 'aside', 'blockquote', 'canvas', 'dd', 'div', 'dl', 'dt', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hr', 'li', 'main', 'nav', 'noscript', 'ol', 'output', 'p', 'pre', 'section', 'table', 'tfoot', 'ul', 'video'
];

// Find all custom elements
const customElements = node.querySelectorAll('*');

// Iterate over the found custom elements
customElements.forEach(customElement => {
if (customElement.tagName.includes('-') || customElement instanceof HTMLUnknownElement) {
let hasBlockLevelChild = false;

// Check if the custom element has any block-level child elements
for (let child of customElement.childNodes) {
if (child.nodeType === Node.ELEMENT_NODE && blockLevelElements.includes(child.tagName.toLowerCase())) {
hasBlockLevelChild = true;
break;
}
}

// Create a replacement element (div or span)
const replacement = document.createElement(hasBlockLevelChild ? 'div' : 'span');

// Move all child nodes to the replacement element
while (customElement.firstChild) {
replacement.appendChild(customElement.firstChild);
}

// Replace the custom element with the replacement element
customElement.parentNode.replaceChild(replacement, customElement);
this.log(`Replaced custom element ${customElement.tagName} with ${replacement.tagName}`);
}
});
},

/**
* Runs readability.
*
Expand Down

0 comments on commit c900e98

Please sign in to comment.