Compare commits
2 Commits
0800247b65
...
9b25d2f22c
Author | SHA1 | Date | |
---|---|---|---|
9b25d2f22c | |||
a9e51d837d |
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@doc-utils/markdown2html",
|
||||
"version": "0.2.2",
|
||||
"version": "0.2.3",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@doc-utils/markdown2html",
|
||||
"version": "0.2.2",
|
||||
"version": "0.2.3",
|
||||
"dependencies": {
|
||||
"bytefield-svg": "^1.6.1",
|
||||
"dompurify": "^2.3.6",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@doc-utils/markdown2html",
|
||||
"version": "0.2.2",
|
||||
"version": "0.2.3",
|
||||
"publishConfig": {
|
||||
"registry": "https://gitea.home.jbrumond.me/api/packages/doc-utils/npm/"
|
||||
},
|
||||
|
@ -26,14 +26,32 @@ export interface MarkdownExtension {
|
||||
}
|
||||
|
||||
export async function render_markdown_to_html(markdown: string, options: MarkdownOptions = { }) {
|
||||
const marked_options: marked.MarkedOptions & { async: true } = {
|
||||
async: true,
|
||||
const marked_options = marked_opts(true, options);
|
||||
const unsafe_html = options.inline
|
||||
? marked.parseInline(markdown, marked_options)
|
||||
: await marked.parse(markdown, marked_options).then(resolve_async_bindings);
|
||||
|
||||
return sanitize_html(unsafe_html, options.custom_elements);
|
||||
}
|
||||
|
||||
export function render_markdown_to_html_inline_sync(markdown: string, options: MarkdownOptions = { }) {
|
||||
const marked_options = marked_opts(false, options);
|
||||
setup_marked(options, marked_options);
|
||||
const unsafe_html = marked.parseInline(markdown, marked_options);
|
||||
return sanitize_html(unsafe_html, options.custom_elements);
|
||||
}
|
||||
|
||||
function marked_opts<T extends boolean>(async: T, options: MarkdownOptions) : marked.MarkedOptions & { async: T } {
|
||||
return {
|
||||
async,
|
||||
breaks: options.breaks || false,
|
||||
renderer: create_renderer(options),
|
||||
mangle: false,
|
||||
headerIds: false,
|
||||
};
|
||||
}
|
||||
|
||||
function setup_marked(options: MarkdownOptions, marked_options: marked.MarkedOptions) {
|
||||
marked.use({
|
||||
walkTokens(token) {
|
||||
base_url_walk_tokens(token, options);
|
||||
@ -60,45 +78,4 @@ export async function render_markdown_to_html(markdown: string, options: Markdow
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const unsafe_html = options.inline
|
||||
? marked.parseInline(markdown, marked_options)
|
||||
: await marked.parse(markdown, marked_options).then(resolve_async_bindings);
|
||||
|
||||
return sanitize_html(unsafe_html, options.custom_elements);
|
||||
}
|
||||
|
||||
export function render_markdown_to_html_inline_sync(markdown: string, options: MarkdownOptions = { }) {
|
||||
const marked_options: marked.MarkedOptions = {
|
||||
baseUrl: options.base_url,
|
||||
breaks: options.breaks || false,
|
||||
renderer: create_renderer(options),
|
||||
};
|
||||
|
||||
marked.use({
|
||||
extensions: [
|
||||
katex_block_ext(marked_options.renderer, options),
|
||||
katex_inline_ext(marked_options.renderer, options),
|
||||
footnote_ref_ext(marked_options.renderer, options),
|
||||
footnote_list_ext(marked_options.renderer, options),
|
||||
mark_ext(marked_options.renderer, options),
|
||||
description_list_ext(marked_options.renderer, options),
|
||||
section_ext(marked_options.renderer, options),
|
||||
icon_ext(marked_options.renderer, options),
|
||||
breadcrumb_nav_ext(marked_options.renderer, options),
|
||||
...(options.extensions || [ ]).map((ext) => {
|
||||
return ext(marked_options.renderer, options);
|
||||
}),
|
||||
],
|
||||
tokenizer: {
|
||||
url(src) {
|
||||
// disable auto-linking; more can be added here to auto-link only sometimes
|
||||
// see: https://github.com/markedjs/marked/issues/882#issuecomment-781628889
|
||||
return null;
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const unsafe_html = marked.parseInline(markdown, marked_options);
|
||||
return sanitize_html(unsafe_html, options.custom_elements);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user