mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +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 type { WindowLike } from 'dompurify';
|
||||||
import { trustedDomains } from '$lib/consts';
|
import { trustedDomains } from '$lib/consts';
|
||||||
|
|
||||||
|
export const externalMediaWarningStorageKey = 'sharpbin:ignore-external-media-warning';
|
||||||
|
|
||||||
const tagMap: Record<string, string> = {
|
const tagMap: Record<string, string> = {
|
||||||
IMG: 'image',
|
IMG: 'image',
|
||||||
VIDEO: 'video',
|
VIDEO: 'video',
|
||||||
@@ -10,6 +12,18 @@ const tagMap: Record<string, string> = {
|
|||||||
IFRAME: 'iframe'
|
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(
|
export async function parseMarkdown(
|
||||||
md: string,
|
md: string,
|
||||||
preventExternal: boolean = true,
|
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;'
|
'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'))
|
if (element.getAttribute('alt'))
|
||||||
button.setAttribute('data-alt', element.getAttribute('alt') || '');
|
button.setAttribute('data-alt', element.getAttribute('alt') || '');
|
||||||
if (element.getAttribute('title'))
|
if (element.getAttribute('title'))
|
||||||
@@ -89,9 +111,17 @@ export async function parseMarkdown(
|
|||||||
if (element.getAttribute('height'))
|
if (element.getAttribute('height'))
|
||||||
button.setAttribute('data-height', 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(warning);
|
||||||
placeholder.appendChild(subWarning);
|
placeholder.appendChild(subWarning);
|
||||||
placeholder.appendChild(button);
|
placeholder.appendChild(actions);
|
||||||
|
|
||||||
const parent = element.parentNode;
|
const parent = element.parentNode;
|
||||||
if (parent && parent.nodeName === 'A') {
|
if (parent && parent.nodeName === 'A') {
|
||||||
|
|||||||
@@ -33,7 +33,11 @@
|
|||||||
tooltip,
|
tooltip,
|
||||||
isBinaryData
|
isBinaryData
|
||||||
} from '$lib/utils/misc';
|
} 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 { syntaxes, expiresOptions } from '$lib/consts';
|
||||||
import { resolve } from '$app/paths';
|
import { resolve } from '$app/paths';
|
||||||
import { decryptAES, encryptAES } from '$lib/utils/encryption';
|
import { decryptAES, encryptAES } from '$lib/utils/encryption';
|
||||||
@@ -62,6 +66,7 @@
|
|||||||
let copiedPaste = false;
|
let copiedPaste = false;
|
||||||
let highlightWorker: Worker | null = null;
|
let highlightWorker: Worker | null = null;
|
||||||
let highlightRequestId = 0;
|
let highlightRequestId = 0;
|
||||||
|
let ignoreExternalMediaWarning = false;
|
||||||
|
|
||||||
let scrollY = 0;
|
let scrollY = 0;
|
||||||
|
|
||||||
@@ -132,7 +137,7 @@
|
|||||||
|
|
||||||
switch (data.paste.syntax) {
|
switch (data.paste.syntax) {
|
||||||
case 'markdown':
|
case 'markdown':
|
||||||
pasteContent = await parseMarkdown(contentToRender);
|
pasteContent = await parseMarkdown(contentToRender, !ignoreExternalMediaWarning);
|
||||||
contentRendered = true;
|
contentRendered = true;
|
||||||
decryptStatus = '';
|
decryptStatus = '';
|
||||||
break;
|
break;
|
||||||
@@ -357,6 +362,13 @@
|
|||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
|
|
||||||
|
if (target.classList.contains('ignore-media-warning-btn')) {
|
||||||
|
ignoreExternalMediaWarning = true;
|
||||||
|
setIgnoreExternalMediaWarning(true);
|
||||||
|
renderPaste();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (target.classList.contains('load-media-btn')) {
|
if (target.classList.contains('load-media-btn')) {
|
||||||
const src = target.getAttribute('data-src');
|
const src = target.getAttribute('data-src');
|
||||||
const tag = target.getAttribute('data-tag');
|
const tag = target.getAttribute('data-tag');
|
||||||
@@ -422,6 +434,8 @@
|
|||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
try {
|
try {
|
||||||
|
ignoreExternalMediaWarning = getIgnoreExternalMediaWarning();
|
||||||
|
|
||||||
if (data?.paste && data.paste.visibility === 2) {
|
if (data?.paste && data.paste.visibility === 2) {
|
||||||
contentRendered = false;
|
contentRendered = false;
|
||||||
decryptStatus = 'Preparing decryption...';
|
decryptStatus = 'Preparing decryption...';
|
||||||
|
|||||||
@@ -83,7 +83,8 @@ select {
|
|||||||
color: #3b82f6;
|
color: #3b82f6;
|
||||||
text-decoration: underline;
|
text-decoration: underline;
|
||||||
}
|
}
|
||||||
.markdown .load-media-btn:hover {
|
.markdown .load-media-btn:hover,
|
||||||
|
.markdown .ignore-media-warning-btn:hover {
|
||||||
background-color: #525252 !important;
|
background-color: #525252 !important;
|
||||||
}
|
}
|
||||||
.markdown pre {
|
.markdown pre {
|
||||||
|
|||||||
Reference in New Issue
Block a user