summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/lib/gfm/index.js
blob: b4f941294de4fa5cf984dc4033f2d5944db160aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import remarkGfm from 'remark-gfm';
import remarkRehype, { all } from 'remark-rehype';
import rehypeRaw from 'rehype-raw';

const createParser = () => {
  return unified()
    .use(remarkParse)
    .use(remarkGfm)
    .use(remarkRehype, {
      allowDangerousHtml: true,
      handlers: {
        footnoteReference: (h, node) =>
          h(
            node.position,
            'footnoteReference',
            { identifier: node.identifier, label: node.label },
            [],
          ),
        footnoteDefinition: (h, node) =>
          h(
            node.position,
            'footnoteDefinition',
            { identifier: node.identifier, label: node.label },
            all(h, node),
          ),
      },
    })
    .use(rehypeRaw);
};

const compilerFactory = (renderer) =>
  function compiler() {
    Object.assign(this, {
      Compiler(tree) {
        return renderer(tree);
      },
    });
  };

/**
 * Parses a Markdown string and provides the result Abstract
 * Syntax Tree (AST) to a renderer function to convert the
 * tree in any desired representation
 *
 * @param {String} params.markdown Markdown to parse
 * @param {(tree: MDast -> any)} params.renderer A function that accepts mdast
 * AST tree and returns an object of any type that represents the result of
 * rendering the tree. See the references below to for more information
 * about MDast.
 *
 * MDastTree documentation https://github.com/syntax-tree/mdast
 * @returns {Promise<any>} Returns a promise with the result of rendering
 * the MDast tree
 */
export const render = async ({ markdown, renderer }) => {
  const { result } = await createParser().use(compilerFactory(renderer)).process(markdown);

  return result;
};