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:
@@ -156,12 +156,43 @@ export const syntaxes: Record<string, Syntax> = {
|
|||||||
zenscript: { name: 'ZenScript', extension: '.zs' }
|
zenscript: { name: 'ZenScript', extension: '.zs' }
|
||||||
};
|
};
|
||||||
|
|
||||||
// only include langs that need special display names
|
|
||||||
// e.g. cpp -> C++ or toml -> TOML, langs like java are automatically capitalized
|
|
||||||
|
|
||||||
export const roles = {
|
export const roles = {
|
||||||
0: 'User',
|
0: 'User',
|
||||||
1: 'Moderator',
|
1: 'Moderator',
|
||||||
255: 'Administrator',
|
255: 'Administrator',
|
||||||
403: 'Banned'
|
403: 'Banned'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const trustedDomains =
|
||||||
|
[
|
||||||
|
'img.youtube.com',
|
||||||
|
'i.ytimg.com',
|
||||||
|
|
||||||
|
'github.com',
|
||||||
|
'githubusercontent.com',
|
||||||
|
'avatars.githubusercontent.com',
|
||||||
|
'camo.githubusercontent.com',
|
||||||
|
'raw.githubusercontent.com',
|
||||||
|
'github.githubassets.com',
|
||||||
|
|
||||||
|
'cdn.discordapp.com',
|
||||||
|
'media.discordapp.net',
|
||||||
|
|
||||||
|
'i.redd.it',
|
||||||
|
'v.redd.it',
|
||||||
|
'external-preview.redd.it',
|
||||||
|
'preview.redd.it',
|
||||||
|
|
||||||
|
'i.imgur.com',
|
||||||
|
'imgur.com',
|
||||||
|
|
||||||
|
'pbs.twimg.com',
|
||||||
|
'video.twimg.com',
|
||||||
|
'twitter.com',
|
||||||
|
'x.com',
|
||||||
|
|
||||||
|
'images.cloudflare.com',
|
||||||
|
'cloudflare-ipfs.com',
|
||||||
|
|
||||||
|
'upload.wikimedia.org'
|
||||||
|
];
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { marked } from 'marked';
|
import { marked } from 'marked';
|
||||||
import createDOMPurify from 'dompurify';
|
import createDOMPurify from 'dompurify';
|
||||||
import type { WindowLike } from 'dompurify';
|
import type { WindowLike } from 'dompurify';
|
||||||
|
import { trustedDomains } from '$lib/consts';
|
||||||
|
|
||||||
const tagMap: Record<string, string> = {
|
const tagMap: Record<string, string> = {
|
||||||
IMG: 'image',
|
IMG: 'image',
|
||||||
@@ -9,7 +10,7 @@ const tagMap: Record<string, string> = {
|
|||||||
IFRAME: 'iframe'
|
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);
|
const html = await marked.parse(md);
|
||||||
let dom: WindowLike & { document: Document };
|
let dom: WindowLike & { document: Document };
|
||||||
|
|
||||||
@@ -35,12 +36,15 @@ export async function parseMarkdown(md: string, preventExternal: boolean = true)
|
|||||||
if (!src) return;
|
if (!src) return;
|
||||||
|
|
||||||
let hostname = 'external server';
|
let hostname = 'external server';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
hostname = new URL(src).hostname;
|
hostname = new URL(src).hostname;
|
||||||
} catch {
|
} catch {
|
||||||
//might be a relative url
|
//might be a relative url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (allowTrustedDomains && trustedDomains.includes(hostname)) return;
|
||||||
|
|
||||||
const placeholder = dom.document.createElement('span');
|
const placeholder = dom.document.createElement('span');
|
||||||
placeholder.className = 'media-placeholder';
|
placeholder.className = 'media-placeholder';
|
||||||
placeholder.setAttribute(
|
placeholder.setAttribute(
|
||||||
|
|||||||
Reference in New Issue
Block a user