fix base url issues

This commit is contained in:
James Brumond 2023-08-19 16:03:08 -07:00
parent 4cfeb23982
commit 619762a5cd
Signed by: james
GPG Key ID: E8F2FC44BAA3357A

View File

@ -2,10 +2,20 @@
import { marked } from 'marked';
import { MarkdownOptions } from './render';
export const placeholder_base_url = 'https://markdown2html.base-url.placeholder.invalid';
export function base_url_walk_tokens(token: marked.Token, options: MarkdownOptions) {
if (options.base_url) {
const base_url = options.base_url.startsWith('http://') || options.base_url.startsWith('https://')
? options.base_url
: placeholder_base_url + options.base_url;
if (token.type === 'link' || token.type === 'image') {
token.href = (new URL(token.href, options.base_url)).toString();
token.href = (new URL(token.href, base_url)).toString();
if (token.href.startsWith(placeholder_base_url)) {
token.href = token.href.slice(placeholder_base_url.length);
}
}
}
}