JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 100709
  • Score
    100M100P100Q187170F
  • License MIT

Find and replace text in a HAST tree

Package Exports

  • hast-util-find-and-replace

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (hast-util-find-and-replace) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

hast-util-find-and-replace Build Status Coverage Status

Find and replace text in a HAST tree.

Installation

npm:

npm install hast-util-find-and-replace

Usage

var h = require('hastscript');
var inspect = require('unist-util-inspect');
var findAndReplace = require('hast-util-find-and-replace');

var tree = h('p', [
  'Some ',
  h('em', 'emphasis'),
  ', ',
  h('strong', 'importance'),
  ', and ',
  h('code', 'code'),
  '.'
]);

findAndReplace(tree, 'and', 'or');

findAndReplace(tree, {emphasis: 'em', importance: 'strong'});

findAndReplace(tree, {code: function ($0) {
  return h('a', {href: '//example.com#' + $0}, $0);
}});

console.log(inspect(tree));

Yields:

element[9] [tagName="p"]
├─ text: "Some "
├─ element[1] [tagName="em"]
│  └─ text: "em"
├─ text: ", "
├─ element[1] [tagName="strong"]
│  └─ text: "strong"
├─ text: ", "
├─ text: "or"
├─ text: " "
├─ element[1] [tagName="code"]
│  └─ element[1] [tagName="a"][properties={"href":"//example.com#code"}]
│     └─ text: "code"
└─ text: "."

API

findAndReplace(tree, find[, replace][, options])

Find and replace text in a HAST tree. The algorithm searches for complete values in text nodes. Partial matches are not supported.

Signatures
  • findAndReplace(tree, find, replace[, options])
  • findAndReplace(tree, search[, options])
Parameters
  • tree (Node) — HAST tree.

  • find (string or RegExp) — Value to find and remove. When string, escaped and made into a global RegExp.

  • replace (string or Function) — Value to insert. When string, turned into a text node. When Function, invoked with the results of calling RegExp.exec as arguments, in which case it can return a Node or a string, in which case it’s wrapped in a text node.

  • search (Object or Array) — Perform multiple find-and-replace’s. When Array, each entry is a tuple (array) of a find and replace. When Object, each key is a find (in string form) and each value is replace.

  • options (Object, optional):

    • ignore (Array, default: ['title', 'script', 'style', 'svg', 'math']) — Tag-names of elements not to search.
Returns

The given, modified, tree.

License

MIT © Titus Wormer