Muninn Detect is an assistant designed to test whether an HTML element adheres to specified rules. This allows you to define detect rules to identify HTML elements in bulk.
Sample HTML:
<body>
<div class="blocks">
<div class="block">
<div class="title">Title</div>
<div class="description">Description</div>
</div>
<div class="block-video">
<div class="title">Title</div>
<div class="video-wrapper">...</div>
</div>
<div class="block-gallery">
<div class="title">Title</div>
<div class="image-gallery">...</div>
</div>
<div class="block hidden">
<div class="title">Title</div>
<div class="description">Description</div>
</div>
</div>
</body>
Example:
import { load } from 'cheerio';
import { detect } from 'muninn-detect';
const $ = load(/* html content */);
// Get blocks
const blockElements = $('.blocks > div').toArray();
// Define detect rules
const basicBlockDetect = {
hasClassName: 'block',
withInnerSelector: '.description'
};
const videoBlockDetect = {
exactMatchClassName: 'block-video',
withInnerSelector: '.video-wrapper'
};
const imageBlockDetect = {
exactMatchClassName: 'block-gallery',
withInnerSelector: '.image-gallery'
};
// Find a specific block among the stack of blocks
const imageBlock = blockElements.find((el, index) =>
detect(el, imageBlockDetect)
);
Distributed under the MIT License. See LICENSE for more information.