diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-04-20 10:00:54 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-04-20 10:00:54 +0000 |
commit | 3cccd102ba543e02725d247893729e5c73b38295 (patch) | |
tree | f36a04ec38517f5deaaacb5acc7d949688d1e187 /app/assets/javascripts/lib/gfm/index.js | |
parent | 205943281328046ef7b4528031b90fbda70c75ac (diff) | |
download | gitlab-ce-3cccd102ba543e02725d247893729e5c73b38295.tar.gz |
Add latest changes from gitlab-org/gitlab@14-10-stable-eev14.10.0-rc42
Diffstat (limited to 'app/assets/javascripts/lib/gfm/index.js')
-rw-r--r-- | app/assets/javascripts/lib/gfm/index.js | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/app/assets/javascripts/lib/gfm/index.js b/app/assets/javascripts/lib/gfm/index.js new file mode 100644 index 00000000000..07388f1fdfa --- /dev/null +++ b/app/assets/javascripts/lib/gfm/index.js @@ -0,0 +1,38 @@ +import { unified } from 'unified'; +import remarkParse from 'remark-parse'; +import remarkRehype from 'remark-rehype'; +import rehypeRaw from 'rehype-raw'; + +const createParser = () => { + return unified().use(remarkParse).use(remarkRehype, { allowDangerousHtml: true }).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 { value } = await createParser().use(compilerFactory(renderer)).process(markdown); + + return value; +}; |