mdast-util-heading-range

2018-11-14 admin

mdast-util-heading-range是什么

什么是mdast-util-heading-range,Markdown heading as ranges in mdast

mdast-util-heading-range使用教程帮助文档

mdast-util-heading-range Build Status Coverage Status Chat

Markdown heading as ranges in mdast.

Installation

npm:

npm install mdast-util-heading-range

Usage

Say we have the following file, example.md:

# Foo

Bar.

# Baz

And our script, example.js, looks as follows:

var vfile = require('to-vfile')
var remark = require('remark')
var heading = require('mdast-util-heading-range')

remark()
  .use(plugin)
  .process(vfile.readSync('example.md'), function(err, file) {
    if (err) throw err
    console.log(String(file))
  })

function plugin() {
  return transform

  function transform(tree) {
    heading(tree, 'foo', mutate)
  }

  function mutate(start, nodes, end) {
    return [
      start,
      {type: 'paragraph', children: [{type: 'text', value: 'Qux.'}]},
      end
    ]
  }
}

Now, running node example yields:

# Foo

Qux.

# Baz

API

heading(tree, test|options, onrun)

Search tree (Node) and transform a section without affecting other parts with onrun (Function). A Section is a heading that passes test, until the next heading of the same or lower depth, or the end of the document. If ignoreFinalDefinitions: true, final definitions “in” the section are excluded.

options
options.test

Heading to look for (string, RegExp, Function). When string, wrapped in new RegExp('^(' + value + ')$', 'i'); when RegExp, wrapped in function (value) {expression.test(value)}

options.ignoreFinalDefinitions

Ignore final definitions otherwise in the section (boolean, default: false).

function test(value, node)

Function invoked for each heading with its content (string) and node itself (Heading) to check if it’s the one to look for.

Returns

Boolean?, true if this is the heading to use.

function onrun(start, nodes, end?, scope)

Callback invoked when a range is found.

Parameters
start

Start of range (Heading).

nodes

Nodes between start and end (Array.<Node>).

end

End of range, if any (Node?).

scope

Extra info (Object):

  • parent (Node) — Parent of the range
  • start (number) — Index of start in parent
  • end (number?) — Index of end in parent

Contribute

See contributing.md in syntax-tree/mdast for ways to get started.

This organisation has a Code of Conduct. By interacting with this repository, organisation, or community you agree to abide by its terms.

License

MIT © Titus Wormer

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-45386.html

文章标题:mdast-util-heading-range

回到顶部