muninn-detect
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

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.

Package Sidebar

Install

npm i muninn-detect

Weekly Downloads

1

Version

0.1.0

License

MIT

Unpacked Size

23.9 kB

Total Files

23

Last publish

Collaborators

  • aykutkardas