mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-05 18:48:41 +00:00
feat(markdown): add dont warn again feature
This commit is contained in:
@@ -3,6 +3,8 @@ import createDOMPurify from 'dompurify';
|
||||
import type { WindowLike } from 'dompurify';
|
||||
import { trustedDomains } from '$lib/consts';
|
||||
|
||||
export const externalMediaWarningStorageKey = 'sharpbin:ignore-external-media-warning';
|
||||
|
||||
const tagMap: Record<string, string> = {
|
||||
IMG: 'image',
|
||||
VIDEO: 'video',
|
||||
@@ -10,6 +12,18 @@ const tagMap: Record<string, string> = {
|
||||
IFRAME: 'iframe'
|
||||
};
|
||||
|
||||
export function getIgnoreExternalMediaWarning(): boolean {
|
||||
if (typeof window === 'undefined') return false;
|
||||
|
||||
return window.localStorage.getItem(externalMediaWarningStorageKey) === 'true';
|
||||
}
|
||||
|
||||
export function setIgnoreExternalMediaWarning(ignore: boolean) {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
window.localStorage.setItem(externalMediaWarningStorageKey, ignore ? 'true' : 'false');
|
||||
}
|
||||
|
||||
export async function parseMarkdown(
|
||||
md: string,
|
||||
preventExternal: boolean = true,
|
||||
@@ -80,6 +94,14 @@ export async function parseMarkdown(
|
||||
'background-color: #404040; color: white; padding: 0.25rem 0.75rem; border-radius: 0.25rem; cursor: pointer; text-decoration: none;'
|
||||
);
|
||||
|
||||
const ignoreButton = dom.document.createElement('button');
|
||||
ignoreButton.textContent = "Don't warn again";
|
||||
ignoreButton.className = 'ignore-media-warning-btn';
|
||||
ignoreButton.setAttribute(
|
||||
'style',
|
||||
'background-color: transparent; color: #d4d4d8; padding: 0.25rem 0.75rem; border: 1px solid #525252; border-radius: 0.25rem; cursor: pointer; text-decoration: none;'
|
||||
);
|
||||
|
||||
if (element.getAttribute('alt'))
|
||||
button.setAttribute('data-alt', element.getAttribute('alt') || '');
|
||||
if (element.getAttribute('title'))
|
||||
@@ -89,9 +111,17 @@ export async function parseMarkdown(
|
||||
if (element.getAttribute('height'))
|
||||
button.setAttribute('data-height', element.getAttribute('height') || '');
|
||||
|
||||
const actions = dom.document.createElement('span');
|
||||
actions.setAttribute(
|
||||
'style',
|
||||
'display: flex; gap: 0.5rem; justify-content: center; flex-wrap: wrap;'
|
||||
);
|
||||
actions.appendChild(button);
|
||||
actions.appendChild(ignoreButton);
|
||||
|
||||
placeholder.appendChild(warning);
|
||||
placeholder.appendChild(subWarning);
|
||||
placeholder.appendChild(button);
|
||||
placeholder.appendChild(actions);
|
||||
|
||||
const parent = element.parentNode;
|
||||
if (parent && parent.nodeName === 'A') {
|
||||
|
||||
@@ -33,7 +33,11 @@
|
||||
tooltip,
|
||||
isBinaryData
|
||||
} from '$lib/utils/misc';
|
||||
import { parseMarkdown } from '$lib/utils/markdown';
|
||||
import {
|
||||
getIgnoreExternalMediaWarning,
|
||||
parseMarkdown,
|
||||
setIgnoreExternalMediaWarning
|
||||
} from '$lib/utils/markdown';
|
||||
import { syntaxes, expiresOptions } from '$lib/consts';
|
||||
import { resolve } from '$app/paths';
|
||||
import { decryptAES, encryptAES } from '$lib/utils/encryption';
|
||||
@@ -62,6 +66,7 @@
|
||||
let copiedPaste = false;
|
||||
let highlightWorker: Worker | null = null;
|
||||
let highlightRequestId = 0;
|
||||
let ignoreExternalMediaWarning = false;
|
||||
|
||||
let scrollY = 0;
|
||||
|
||||
@@ -132,7 +137,7 @@
|
||||
|
||||
switch (data.paste.syntax) {
|
||||
case 'markdown':
|
||||
pasteContent = await parseMarkdown(contentToRender);
|
||||
pasteContent = await parseMarkdown(contentToRender, !ignoreExternalMediaWarning);
|
||||
contentRendered = true;
|
||||
decryptStatus = '';
|
||||
break;
|
||||
@@ -357,6 +362,13 @@
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
|
||||
if (target.classList.contains('ignore-media-warning-btn')) {
|
||||
ignoreExternalMediaWarning = true;
|
||||
setIgnoreExternalMediaWarning(true);
|
||||
renderPaste();
|
||||
return;
|
||||
}
|
||||
|
||||
if (target.classList.contains('load-media-btn')) {
|
||||
const src = target.getAttribute('data-src');
|
||||
const tag = target.getAttribute('data-tag');
|
||||
@@ -422,6 +434,8 @@
|
||||
|
||||
onMount(async () => {
|
||||
try {
|
||||
ignoreExternalMediaWarning = getIgnoreExternalMediaWarning();
|
||||
|
||||
if (data?.paste && data.paste.visibility === 2) {
|
||||
contentRendered = false;
|
||||
decryptStatus = 'Preparing decryption...';
|
||||
|
||||
@@ -83,7 +83,8 @@ select {
|
||||
color: #3b82f6;
|
||||
text-decoration: underline;
|
||||
}
|
||||
.markdown .load-media-btn:hover {
|
||||
.markdown .load-media-btn:hover,
|
||||
.markdown .ignore-media-warning-btn:hover {
|
||||
background-color: #525252 !important;
|
||||
}
|
||||
.markdown pre {
|
||||
|
||||
Reference in New Issue
Block a user