Skip to content

daaxar/walker

Repository files navigation

Walker

Object walker for HTML parsed

How to use it

const Walker = require('@daaxar/walker');
const Parser = require('node-html-parser');

const parsed = Parser.parse('<section><h1>Title</h1><strong>This is a strong text</strong></section>');

const walker = Walker([
  {
    condition: (node) => node.nodeType === 1 && node.tagName === 'h1',
    action: (node, container, next) => {
      const text = [];
      next(node.childNodes, text);
      container.push(`# ${text}`);
    },
  },
  {
    condition: (node) => node.nodeType === 1 && node.tagName === 'strong',
    action: (node, container, next) => {
      const text = [];
      next(node.childNodes, text);
      container.push(`**${text}**`);
    },
  },
  {
    condition: (node) => node.nodeType === 1,
    action: (node, container, next) => next(node.childNodes),
  },
  {
    condition: () => true,
    action: (node, container) => {
      container.push(node.rawText);
    },
  },
]);

const obj = walker(parsed, []);
const md = obj.join('\n\n');

console.log(md);

Library

Table of Contents

WalkerFactory

Walker factory

Parameters

walkerInstance

Walker instances

Parameters

  • args ...any
  • node any
  • container any

About

Walk through html parsed object

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages