updated marked to v5; better baseurl handling
This commit is contained in:
11
src/base-url.ts
Normal file
11
src/base-url.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
|
||||
import { marked } from 'marked';
|
||||
import { MarkdownOptions } from './render';
|
||||
|
||||
export function base_url_walk_tokens(token: marked.Token, options: MarkdownOptions) {
|
||||
if (options.base_url) {
|
||||
if (token.type === 'link' || token.type === 'image') {
|
||||
token.href = (new URL(token.href, options.base_url)).toString();
|
||||
}
|
||||
}
|
||||
}
|
@@ -10,6 +10,7 @@ import { footnote_list_ext, footnote_ref_ext } from './footnotes';
|
||||
import { description_list_ext } from './description-list';
|
||||
import { resolve_async_bindings } from './async-steps';
|
||||
import { breadcrumb_nav_ext } from './breadcrumb-nav';
|
||||
import { base_url_walk_tokens } from './base-url';
|
||||
|
||||
export interface MarkdownOptions {
|
||||
base_url?: string;
|
||||
@@ -25,13 +26,16 @@ export interface MarkdownExtension {
|
||||
}
|
||||
|
||||
export async function render_markdown_to_html(markdown: string, options: MarkdownOptions = { }) {
|
||||
const marked_options: marked.MarkedOptions = {
|
||||
baseUrl: options.base_url,
|
||||
const marked_options: marked.MarkedOptions & { async: true } = {
|
||||
async: true,
|
||||
breaks: options.breaks || false,
|
||||
renderer: create_renderer(options),
|
||||
};
|
||||
|
||||
marked.use({
|
||||
walkTokens(token) {
|
||||
base_url_walk_tokens(token, options);
|
||||
},
|
||||
extensions: [
|
||||
katex_block_ext(marked_options.renderer, options),
|
||||
katex_inline_ext(marked_options.renderer, options),
|
||||
@@ -57,15 +61,7 @@ export async function render_markdown_to_html(markdown: string, options: Markdow
|
||||
|
||||
const unsafe_html = options.inline
|
||||
? marked.parseInline(markdown, marked_options)
|
||||
: await new Promise<string>((resolve, reject) => {
|
||||
marked.parse(markdown, marked_options, (error, unsafe_html) => {
|
||||
if (error) {
|
||||
return reject(error);
|
||||
}
|
||||
|
||||
resolve_async_bindings(unsafe_html).then(resolve, reject);
|
||||
});
|
||||
});
|
||||
: await marked.parse(markdown, marked_options).then(resolve_async_bindings);
|
||||
|
||||
return sanitize_html(unsafe_html, options.custom_elements);
|
||||
}
|
||||
|
Reference in New Issue
Block a user