(
article,
{ $, cleanConditionally = true, title = '', url = '', defaultCleaner = true }
)
| 13 | |
| 14 | // Clean our article content, returning a new, cleaned node. |
| 15 | export default function extractCleanNode( |
| 16 | article, |
| 17 | { $, cleanConditionally = true, title = '', url = '', defaultCleaner = true } |
| 18 | ) { |
| 19 | // Rewrite the tag name to div if it's a top level node like body or |
| 20 | // html to avoid later complications with multiple body tags. |
| 21 | rewriteTopLevel(article, $); |
| 22 | |
| 23 | // Drop small images and spacer images |
| 24 | // Only do this is defaultCleaner is set to true; |
| 25 | // this can sometimes be too aggressive. |
| 26 | if (defaultCleaner) cleanImages(article, $); |
| 27 | |
| 28 | // Make links absolute |
| 29 | makeLinksAbsolute(article, $, url); |
| 30 | |
| 31 | // Mark elements to keep that would normally be removed. |
| 32 | // E.g., stripJunkTags will remove iframes, so we're going to mark |
| 33 | // YouTube/Vimeo videos as elements we want to keep. |
| 34 | markToKeep(article, $, url); |
| 35 | |
| 36 | // Drop certain tags like <title>, etc |
| 37 | // This is -mostly- for cleanliness, not security. |
| 38 | stripJunkTags(article, $); |
| 39 | |
| 40 | // H1 tags are typically the article title, which should be extracted |
| 41 | // by the title extractor instead. If there's less than 3 of them (<3), |
| 42 | // strip them. Otherwise, turn 'em into H2s. |
| 43 | cleanHOnes(article, $); |
| 44 | |
| 45 | // Clean headers |
| 46 | cleanHeaders(article, $, title); |
| 47 | |
| 48 | // We used to clean UL's and OL's here, but it was leading to |
| 49 | // too many in-article lists being removed. Consider a better |
| 50 | // way to detect menus particularly and remove them. |
| 51 | // Also optionally running, since it can be overly aggressive. |
| 52 | if (defaultCleaner) cleanTags(article, $, cleanConditionally); |
| 53 | |
| 54 | // Remove empty paragraph nodes |
| 55 | removeEmpty(article, $); |
| 56 | |
| 57 | // Remove unnecessary attributes |
| 58 | cleanAttributes(article, $); |
| 59 | |
| 60 | return article; |
| 61 | } |
no test coverage detected