Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
5304e20c94
|
|||
865016bb26
|
|||
1dcc58c4b2
|
|||
78f99afef0
|
|||
9b25d2f22c
|
|||
a9e51d837d
|
|||
0800247b65
|
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@doc-utils/markdown2html",
|
"name": "@doc-utils/markdown2html",
|
||||||
"version": "0.2.1",
|
"version": "0.2.5",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@doc-utils/markdown2html",
|
"name": "@doc-utils/markdown2html",
|
||||||
"version": "0.2.1",
|
"version": "0.2.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bytefield-svg": "^1.6.1",
|
"bytefield-svg": "^1.6.1",
|
||||||
"dompurify": "^2.3.6",
|
"dompurify": "^2.3.6",
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@doc-utils/markdown2html",
|
"name": "@doc-utils/markdown2html",
|
||||||
"version": "0.2.1",
|
"version": "0.2.5",
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"registry": "https://gitea.home.jbrumond.me/api/packages/doc-utils/npm/"
|
"registry": "https://gitea.home.jbrumond.me/api/packages/doc-utils/npm/"
|
||||||
},
|
},
|
||||||
|
@@ -26,14 +26,31 @@ export interface MarkdownExtension {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function render_markdown_to_html(markdown: string, options: MarkdownOptions = { }) {
|
export async function render_markdown_to_html(markdown: string, options: MarkdownOptions = { }) {
|
||||||
const marked_options: marked.MarkedOptions & { async: true } = {
|
const marked_options = marked_opts(options);
|
||||||
async: true,
|
const unsafe_html = options.inline
|
||||||
|
? marked.parseInline(markdown, marked_options)
|
||||||
|
: marked.parse(markdown, marked_options);
|
||||||
|
|
||||||
|
return sanitize_html(await resolve_async_bindings(unsafe_html), options.custom_elements);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function render_markdown_to_html_inline_sync(markdown: string, options: MarkdownOptions = { }) {
|
||||||
|
const marked_options = marked_opts(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>(options: MarkdownOptions) : marked.MarkedOptions {
|
||||||
|
return {
|
||||||
breaks: options.breaks || false,
|
breaks: options.breaks || false,
|
||||||
renderer: create_renderer(options),
|
renderer: create_renderer(options),
|
||||||
mangle: false,
|
mangle: false,
|
||||||
headerIds: false,
|
headerIds: false,
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function setup_marked(options: MarkdownOptions, marked_options: marked.MarkedOptions) {
|
||||||
marked.use({
|
marked.use({
|
||||||
walkTokens(token) {
|
walkTokens(token) {
|
||||||
base_url_walk_tokens(token, options);
|
base_url_walk_tokens(token, options);
|
||||||
@@ -60,45 +77,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);
|
|
||||||
}
|
}
|
||||||
|
@@ -30,16 +30,14 @@ export function create_renderer(opts: MarkdownOptions) {
|
|||||||
function heading(renderer: marked.Renderer, opts: MarkdownOptions) {
|
function heading(renderer: marked.Renderer, opts: MarkdownOptions) {
|
||||||
return function(orig_text: string, level: 1 | 2 | 3 | 4 | 5 | 6, raw: string) {
|
return function(orig_text: string, level: 1 | 2 | 3 | 4 | 5 | 6, raw: string) {
|
||||||
let { text, id, html_attrs } = parse_attributes(raw);
|
let { text, id, html_attrs } = parse_attributes(raw);
|
||||||
|
if (id) {
|
||||||
|
text += `\n<a class="heading-anchor" href="#${id}">`
|
||||||
|
+ `\n\t\t${icons.link}`
|
||||||
|
+ `\n\t\t<span style="display: none">Section titled ${text}</span>`
|
||||||
|
+ `\n\t</a>`;
|
||||||
|
}
|
||||||
|
|
||||||
return `
|
return `\n<h${level} ${html_attrs.join(' ')}>\n\t${text}\n</h${level}>`;
|
||||||
<h${level} ${html_attrs.join(' ')}>
|
|
||||||
${text}
|
|
||||||
<a class="heading-anchor" href="#${id}">
|
|
||||||
${icons.link}
|
|
||||||
<span style="display: none">Section titled ${text}</span>
|
|
||||||
</a>
|
|
||||||
</h${level}>
|
|
||||||
`;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user