MCPcopy Create free account
hub / github.com/postlight/parser / extractCleanNode

Function extractCleanNode

src/cleaners/content.js:15–61  ·  view source on GitHub ↗
(
  article,
  { $, cleanConditionally = true, title = '', url = '', defaultCleaner = true }
)

Source from the content-addressed store, hash-verified

13
14// Clean our article content, returning a new, cleaned node.
15export 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}

Callers 1

content.test.jsFile · 0.85

Calls 10

rewriteTopLevelFunction · 0.90
cleanImagesFunction · 0.90
makeLinksAbsoluteFunction · 0.90
markToKeepFunction · 0.90
stripJunkTagsFunction · 0.90
cleanHOnesFunction · 0.90
cleanHeadersFunction · 0.90
cleanTagsFunction · 0.90
removeEmptyFunction · 0.90
cleanAttributesFunction · 0.90

Tested by

no test coverage detected