Skip to content

Latest commit

 

History

History
71 lines (56 loc) · 1.85 KB

README.md

File metadata and controls

71 lines (56 loc) · 1.85 KB

Muninn Detect

npm Build Status License

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.

Usage

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)
);

License

Distributed under the MIT License. See LICENSE for more information.