debian-mirror-gitlab/app/assets/javascripts/lib/gfm/index.js

123 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-08-13 15:12:31 +05:30
import { pick } from 'lodash';
2022-08-27 11:52:29 +05:30
import normalize from 'mdurl/encode';
2022-06-21 17:19:12 +05:30
import { unified } from 'unified';
import remarkParse from 'remark-parse';
2022-08-27 11:52:29 +05:30
import remarkFrontmatter from 'remark-frontmatter';
2022-07-23 23:45:48 +05:30
import remarkGfm from 'remark-gfm';
import remarkRehype, { all } from 'remark-rehype';
2022-06-21 17:19:12 +05:30
import rehypeRaw from 'rehype-raw';
2022-10-11 01:57:18 +05:30
import glfmTableOfContents from './glfm_extensions/table_of_contents';
import * as glfmMdastToHastHandlers from './mdast_to_hast_handlers/glfm_mdast_to_hast_handlers';
2022-06-21 17:19:12 +05:30
2022-08-27 11:52:29 +05:30
const skipFrontmatterHandler = (language) => (h, node) =>
h(node.position, 'frontmatter', { language }, [{ type: 'text', value: node.value }]);
2022-08-13 15:12:31 +05:30
const skipRenderingHandlers = {
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),
),
code: (h, node) =>
h(node.position, 'codeBlock', { language: node.lang, meta: node.meta }, [
{ type: 'text', value: node.value },
]),
2022-08-27 11:52:29 +05:30
definition: (h, node) => {
const title = node.title ? ` "${node.title}"` : '';
return h(
node.position,
'referenceDefinition',
{ identifier: node.identifier, url: node.url, title: node.title },
[{ type: 'text', value: `[${node.identifier}]: ${node.url}${title}` }],
);
},
linkReference: (h, node) => {
const definition = h.definition(node.identifier);
return h(
node.position,
'a',
{
href: normalize(definition.url ?? ''),
identifier: node.identifier,
isReference: 'true',
title: definition.title,
},
all(h, node),
);
},
imageReference: (h, node) => {
const definition = h.definition(node.identifier);
return h(
node.position,
'img',
{
src: normalize(definition.url ?? ''),
alt: node.alt,
identifier: node.identifier,
isReference: 'true',
title: definition.title,
},
all(h, node),
);
},
2022-10-11 01:57:18 +05:30
tableOfContents: (h, node) => h(node.position, 'tableOfContents'),
2022-08-27 11:52:29 +05:30
toml: skipFrontmatterHandler('toml'),
yaml: skipFrontmatterHandler('yaml'),
json: skipFrontmatterHandler('json'),
2022-08-13 15:12:31 +05:30
};
2022-10-11 01:57:18 +05:30
const createParser = ({ skipRendering }) => {
2022-07-23 23:45:48 +05:30
return unified()
.use(remarkParse)
.use(remarkGfm)
2022-08-27 11:52:29 +05:30
.use(remarkFrontmatter, ['yaml', 'toml', { type: 'json', marker: ';' }])
2022-10-11 01:57:18 +05:30
.use(glfmTableOfContents)
2022-07-23 23:45:48 +05:30
.use(remarkRehype, {
allowDangerousHtml: true,
handlers: {
2022-10-11 01:57:18 +05:30
...glfmMdastToHastHandlers,
2022-08-13 15:12:31 +05:30
...pick(skipRenderingHandlers, skipRendering),
2022-07-23 23:45:48 +05:30
},
})
.use(rehypeRaw);
2022-06-21 17:19:12 +05:30
};
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
2022-10-11 01:57:18 +05:30
* @param {Function} params.renderer A function that accepts mdast
2022-06-21 17:19:12 +05:30
* 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
2022-10-11 01:57:18 +05:30
* @returns {Promise} Returns a promise with the result of rendering
2022-06-21 17:19:12 +05:30
* the MDast tree
*/
2022-08-13 15:12:31 +05:30
export const render = async ({ markdown, renderer, skipRendering = [] }) => {
const { result } = await createParser({ skipRendering })
.use(compilerFactory(renderer))
.process(markdown);
2022-06-21 17:19:12 +05:30
2022-07-16 23:28:13 +05:30
return result;
2022-06-21 17:19:12 +05:30
};