($els: Cheerio<any>, node?: IHtmlNode)
| 198 | } |
| 199 | |
| 200 | function checkNodes($els: Cheerio<any>, node?: IHtmlNode) { |
| 201 | $els.each((_, child) => { |
| 202 | const $child = $(child); |
| 203 | const rule = Object.entries(options.selectorRules).find(([selector]) => |
| 204 | $child.is(selector), |
| 205 | )?.[1]; |
| 206 | const result = rule?.({ $node: $child, $, getContent }); |
| 207 | // Wrapper |
| 208 | if (result?.queue && !result.nesting) { |
| 209 | checkNodes(result.queue, node); |
| 210 | return; |
| 211 | } |
| 212 | const level = getLevel(child.tagName); |
| 213 | if (!result) { |
| 214 | if (level <= Levels.H6) { |
| 215 | skippingHeading = level; |
| 216 | } |
| 217 | return; |
| 218 | } |
| 219 | if (skippingHeading > Levels.None && level > skippingHeading) return; |
| 220 | if (!$child.is(options.selector)) return; |
| 221 | skippingHeading = Levels.None; |
| 222 | const isHeading = level <= Levels.H6; |
| 223 | let data = { |
| 224 | // If the child is an inline element and expected to be a separate node, |
| 225 | // data from the closest `<p>` should be included, e.g. `<p data-lines><img /></p>` |
| 226 | ...$child.closest('p').data(), |
| 227 | ...$child.data(), |
| 228 | }; |
| 229 | let html = result.html || ''; |
| 230 | if ($child.is('ol>li') && node?.children) { |
| 231 | const start = +($child.parent().attr('start') || 1); |
| 232 | const listIndex = start + node.children.length; |
| 233 | html = `${listIndex}. ${html}`; |
| 234 | data = { |
| 235 | ...data, |
| 236 | listIndex, |
| 237 | }; |
| 238 | } |
| 239 | const childNode = addChild({ |
| 240 | parent: node || getCurrentHeading(level), |
| 241 | nesting: !!result.queue || isHeading, |
| 242 | tagName: child.tagName, |
| 243 | level, |
| 244 | html, |
| 245 | comments: result.comments, |
| 246 | data, |
| 247 | }); |
| 248 | if (isHeading) headingStack.push(childNode); |
| 249 | if (result.queue) checkNodes(result.queue, childNode); |
| 250 | }); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | export function convertNode(htmlRoot: IHtmlNode) { |
no test coverage detected