9 Commits

Author SHA1 Message Date
5304e20c94 0.2.5 2023-05-18 21:56:19 -07:00
865016bb26 do not render in async mode 2023-05-18 21:56:18 -07:00
1dcc58c4b2 0.2.4 2023-05-18 21:38:06 -07:00
78f99afef0 do not render anchors to nowhere 2023-05-18 21:38:05 -07:00
9b25d2f22c 0.2.3 2023-05-18 21:33:38 -07:00
a9e51d837d update inline render to use same options 2023-05-18 21:33:37 -07:00
0800247b65 0.2.2 2023-05-18 21:28:31 -07:00
704447f7c0 0.2.1 2023-05-18 21:27:16 -07:00
e40db445a5 disable newly deprecated stuff 2023-05-18 21:27:09 -07:00
4 changed files with 31 additions and 60 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "@doc-utils/markdown2html",
"version": "0.2.0",
"version": "0.2.5",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@doc-utils/markdown2html",
"version": "0.2.0",
"version": "0.2.5",
"dependencies": {
"bytefield-svg": "^1.6.1",
"dompurify": "^2.3.6",

View File

@@ -1,6 +1,6 @@
{
"name": "@doc-utils/markdown2html",
"version": "0.2.0",
"version": "0.2.5",
"publishConfig": {
"registry": "https://gitea.home.jbrumond.me/api/packages/doc-utils/npm/"
},

View File

@@ -26,12 +26,31 @@ 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(options);
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,
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);
@@ -58,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);
}

View File

@@ -28,23 +28,16 @@ export function create_renderer(opts: MarkdownOptions) {
}
function heading(renderer: marked.Renderer, opts: MarkdownOptions) {
return function(orig_text: string, level: 1 | 2 | 3 | 4 | 5 | 6, raw: string, slugger) {
return function(orig_text: string, level: 1 | 2 | 3 | 4 | 5 | 6, raw: string) {
let { text, id, html_attrs } = parse_attributes(raw);
if (! id) {
id = slugger.slug(text);
html_attrs.push(`id="${id}"`);
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 `
<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}>
`;
return `\n<h${level} ${html_attrs.join(' ')}>\n\t${text}\n</h${level}>`;
};
}