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

Function transformElements

src/extractors/root-extractor.js:15–40  ·  view source on GitHub ↗
($content, $, { transforms })

Source from the content-addressed store, hash-verified

13
14// Transform matching elements
15export function transformElements($content, $, { transforms }) {
16 if (!transforms) return $content;
17
18 Reflect.ownKeys(transforms).forEach(key => {
19 const $matches = $(key, $content);
20 const value = transforms[key];
21
22 // If value is a string, convert directly
23 if (typeof value === 'string') {
24 $matches.each((index, node) => {
25 convertNodeTo($(node), $, transforms[key]);
26 });
27 } else if (typeof value === 'function') {
28 // If value is function, apply function to node
29 $matches.each((index, node) => {
30 const result = value($(node), $);
31 // If function returns a string, convert node to that value
32 if (typeof result === 'string') {
33 convertNodeTo($(node), $, result);
34 }
35 });
36 }
37 });
38
39 return $content;
40}
41
42function findMatchingSelector($, selectors, extractHtml, allowMultiple) {
43 return selectors.find(selector => {

Callers 2

transformAndCleanFunction · 0.85

Calls 2

convertNodeToFunction · 0.90
$Function · 0.85

Tested by

no test coverage detected