mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: bypass user confirmation when loading external assets in markdown for trusted domains
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { marked } from 'marked';
|
||||
import createDOMPurify from 'dompurify';
|
||||
import type { WindowLike } from 'dompurify';
|
||||
import { trustedDomains } from '$lib/consts';
|
||||
|
||||
const tagMap: Record<string, string> = {
|
||||
IMG: 'image',
|
||||
@@ -9,7 +10,7 @@ const tagMap: Record<string, string> = {
|
||||
IFRAME: 'iframe'
|
||||
};
|
||||
|
||||
export async function parseMarkdown(md: string, preventExternal: boolean = true): Promise<string> {
|
||||
export async function parseMarkdown(md: string, preventExternal: boolean = true, allowTrustedDomains: boolean = true): Promise<string> {
|
||||
const html = await marked.parse(md);
|
||||
let dom: WindowLike & { document: Document };
|
||||
|
||||
@@ -35,12 +36,15 @@ export async function parseMarkdown(md: string, preventExternal: boolean = true)
|
||||
if (!src) return;
|
||||
|
||||
let hostname = 'external server';
|
||||
|
||||
try {
|
||||
hostname = new URL(src).hostname;
|
||||
} catch {
|
||||
//might be a relative url
|
||||
}
|
||||
|
||||
if (allowTrustedDomains && trustedDomains.includes(hostname)) return;
|
||||
|
||||
const placeholder = dom.document.createElement('span');
|
||||
placeholder.className = 'media-placeholder';
|
||||
placeholder.setAttribute(
|
||||
|
||||
Reference in New Issue
Block a user