0.1.0 • Published 2 months ago

muninn-detect v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

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.