(opts)
| 66 | } |
| 67 | |
| 68 | export function select(opts) { |
| 69 | const { $, type, extractionOpts, extractHtml = false } = opts; |
| 70 | // Skip if there's not extraction for this type |
| 71 | if (!extractionOpts) return null; |
| 72 | |
| 73 | // If a string is hardcoded for a type (e.g., Wikipedia |
| 74 | // contributors), return the string |
| 75 | if (typeof extractionOpts === 'string') return extractionOpts; |
| 76 | |
| 77 | const { selectors, defaultCleaner = true, allowMultiple } = extractionOpts; |
| 78 | |
| 79 | const overrideAllowMultiple = type === 'lead_image_url' || allowMultiple; |
| 80 | |
| 81 | const matchingSelector = findMatchingSelector( |
| 82 | $, |
| 83 | selectors, |
| 84 | extractHtml, |
| 85 | overrideAllowMultiple |
| 86 | ); |
| 87 | |
| 88 | if (!matchingSelector) return null; |
| 89 | |
| 90 | function transformAndClean($node) { |
| 91 | makeLinksAbsolute($node, $, opts.url || ''); |
| 92 | cleanBySelectors($node, $, extractionOpts); |
| 93 | transformElements($node, $, extractionOpts); |
| 94 | return $node; |
| 95 | } |
| 96 | |
| 97 | function selectHtml() { |
| 98 | // If the selector type requests html as its return type |
| 99 | // transform and clean the element with provided selectors |
| 100 | let $content; |
| 101 | |
| 102 | // If matching selector is an array, we're considering this a |
| 103 | // multi-match selection, which allows the parser to choose several |
| 104 | // selectors to include in the result. Note that all selectors in the |
| 105 | // array must match in order for this selector to trigger |
| 106 | if (Array.isArray(matchingSelector)) { |
| 107 | $content = $(matchingSelector.join(',')); |
| 108 | const $wrapper = $('<div></div>'); |
| 109 | $content.each((_, element) => { |
| 110 | $wrapper.append(element); |
| 111 | }); |
| 112 | |
| 113 | $content = $wrapper; |
| 114 | } else { |
| 115 | $content = $(matchingSelector); |
| 116 | } |
| 117 | |
| 118 | // Wrap in div so transformation can take place on root element |
| 119 | $content.wrap($('<div></div>')); |
| 120 | $content = $content.parent(); |
| 121 | $content = transformAndClean($content); |
| 122 | if (Cleaners[type]) { |
| 123 | Cleaners[type]($content, { ...opts, defaultCleaner }); |
| 124 | } |
| 125 |
no test coverage detected