Skip to content

Muninn Detect is an assistant designed to test whether an HTML element adheres to specified rules.

License

Notifications You must be signed in to change notification settings

wopehq/muninn-detect

Repository files navigation

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.

About

Muninn Detect is an assistant designed to test whether an HTML element adheres to specified rules.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published