remarkjs / remark-rehype
Showing 2 of 5 files from the diff.
Newly tracked file
lib/index.js created.
Other files ignored by Codecov
package.json has changed.
tsconfig.json has changed.
readme.md has changed.

@@ -0,0 +1,71 @@
Loading
1 +
/**
2 +
 * @typedef {import('hast').Root} HastRoot
3 +
 * @typedef {import('mdast').Root} MdastRoot
4 +
 * @typedef {import('mdast-util-to-hast').Options} Options
5 +
 * @typedef {import('unified').Processor<any, any, any, any>} Processor
6 +
 *
7 +
 * @typedef {import('mdast-util-to-hast')} DoNotTouchAsThisImportIncludesRawInTree
8 +
 */
9 +
10 +
import {toHast} from 'mdast-util-to-hast'
11 +
12 +
// Note: the `<MdastRoot, HastRoot>` overload doesn’t seem to work :'(
13 +
14 +
/**
15 +
 * Plugin that turns markdown into HTML to support rehype.
16 +
 *
17 +
 * *   If a destination processor is given, that processor runs with a new HTML
18 +
 *     (hast) tree (bridge-mode).
19 +
 *     As the given processor runs with a hast tree, and rehype plugins support
20 +
 *     hast, that means rehype plugins can be used with the given processor.
21 +
 *     The hast tree is discarded in the end.
22 +
 *     It’s highly unlikely that you want to do this.
23 +
 * *   The common case is to not pass a destination processor, in which case the
24 +
 *     current processor continues running with a new HTML (hast) tree
25 +
 *     (mutate-mode).
26 +
 *     As the current processor continues with a hast tree, and rehype plugins
27 +
 *     support hast, that means rehype plugins can be used after
28 +
 *     `remark-rehype`.
29 +
 *     It’s likely that this is what you want to do.
30 +
 *
31 +
 * @param destination
32 +
 *   Optional unified processor.
33 +
 * @param options
34 +
 *   Options passed to `mdast-util-to-hast`.
35 +
 */
36 +
const remarkRehype =
37 +
  /** @type {(import('unified').Plugin<[Processor, Options?]|[null|undefined, Options?]|[Options]|[], MdastRoot>)} */
38 +
  (
39 +
    function (destination, options) {
40 +
      return destination && 'run' in destination
41 +
        ? bridge(destination, options)
42 +
        : mutate(destination || options)
43 +
    }
44 +
  )
45 +
46 +
export default remarkRehype
47 +
48 +
/**
49 +
 * Bridge-mode.
50 +
 * Runs the destination with the new hast tree.
51 +
 *
52 +
 * @type {import('unified').Plugin<[Processor, Options?], MdastRoot>}
53 +
 */
54 +
function bridge(destination, options) {
55 +
  return (node, file, next) => {
56 +
    destination.run(toHast(node, options), file, (error) => {
57 +
      next(error)
58 +
    })
59 +
  }
60 +
}
61 +
62 +
/**
63 +
 * Mutate-mode.
64 +
 * Further plugins run on the hast tree.
65 +
 *
66 +
 * @type {import('unified').Plugin<[Options?]|void[], MdastRoot, HastRoot>}
67 +
 */
68 +
function mutate(options) {
69 +
  // @ts-expect-error: assume a corresponding node is returned by `toHast`.
70 +
  return (node) => toHast(node, options)
71 +
}

@@ -1,63 +1,8 @@
Loading
1 1
/**
2 -
 * @typedef {import('unist').Node} Node
3 -
 * @typedef {import('hast').Root} HastRoot
4 -
 * @typedef {import('mdast').Root} MdastRoot
5 -
 * @typedef {import('mdast-util-to-hast').Options} Options
6 -
 * @typedef {import('unified').Processor<any, any, any, any>} Processor
7 -
 *
8 -
 * @typedef {import('mdast-util-to-hast')} DoNotTouchAsThisImportIncludesRawInTree
2 +
 * @typedef {import('./lib/index.js').Options} Options
3 +
 * @typedef {import('./lib/index.js').Processor} Processor
9 4
 */
10 5
11 -
import {toHast} from 'mdast-util-to-hast'
12 -
13 -
// Note: the `<MdastRoot, HastRoot>` overload doesn’t seem to work :'(
14 -
15 -
/**
16 -
 * Plugin to bridge or mutate to rehype.
17 -
 *
18 -
 * If a destination is given, runs the destination with the new hast tree
19 -
 * (bridge-mode).
20 -
 * Without destination, returns the hast tree: further plugins run on that tree
21 -
 * (mutate-mode).
22 -
 *
23 -
 * @param destination
24 -
 *   Optional unified processor.
25 -
 * @param options
26 -
 *   Options passed to `mdast-util-to-hast`.
27 -
 */
28 -
const remarkRehype =
29 -
  /** @type {(import('unified').Plugin<[Processor, Options?]|[null|undefined, Options?]|[Options]|[], MdastRoot>)} */
30 -
  (
31 -
    function (destination, options) {
32 -
      return destination && 'run' in destination
33 -
        ? bridge(destination, options)
34 -
        : mutate(destination || options)
35 -
    }
36 -
  )
6 +
import remarkRehype from './lib/index.js'
37 7
38 8
export default remarkRehype
39 -
40 -
/**
41 -
 * Bridge-mode.
42 -
 * Runs the destination with the new hast tree.
43 -
 *
44 -
 * @type {import('unified').Plugin<[Processor, Options?], MdastRoot>}
45 -
 */
46 -
function bridge(destination, options) {
47 -
  return (node, file, next) => {
48 -
    destination.run(toHast(node, options), file, (error) => {
49 -
      next(error)
50 -
    })
51 -
  }
52 -
}
53 -
54 -
/**
55 -
 * Mutate-mode.
56 -
 * Further transformers run on the nlcst tree.
57 -
 *
58 -
 * @type {import('unified').Plugin<[Options?]|void[], MdastRoot, HastRoot>}
59 -
 */
60 -
function mutate(options) {
61 -
  // @ts-expect-error: assume a corresponding node is returned for `toHast`.
62 -
  return (node) => toHast(node, options)
63 -
}
Files Coverage
index.js 100.00%
lib/index.js 100.00%
Project Totals (2 files) 100.00%
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading