Something went wrong. Try again.
Synapse is a study workspace. Organize courses read and annotate the material, train with generated exercises and watch your progress forked from denishovart.com/synapse
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647// Markdown → HTML rendering (marked) + HTML sanitising (DOMPurify).// Used to display/convert stored content (notes, lexicon definitions) before// loading it into the Tiptap editor. The public API (renderMarkdown, toHtml,// sanitizeHtml, looksLikeHtml) is unchanged from the previous hand-rolled// renderer so callers don't move.
import { Marked } from 'marked';import DOMPurify from 'dompurify';import './markdown.css';
const marked = new Marked({ gfm: true, breaks: true });
export function renderMarkdown(md: string | null | undefined): string { if (!md || !md.trim()) return ''; return (marked.parse(md, { async: false }) as string).trim();}
// Content is treated as HTML (already produced by Tiptap) when it opens with a// known block tag; otherwise it's legacy markdown.export function looksLikeHtml(s: string | null | undefined): boolean { return !!s && /^\s*<(p|h[1-6]|ul|ol|blockquote|pre|div|span)[\s>]/i.test(s);}
// Prepare stored content (legacy markdown or Tiptap HTML) for the editor/preview.export function toHtml(stored: string | null | undefined): string { if (!stored) return ''; return looksLikeHtml(stored) ? stored : renderMarkdown(stored);}
// Sanitise HTML before rendering via {@html}. DOMPurify strips scripts, event// handlers and javascript: URLs; we additionally force external links to open// safely in a new tab.export function sanitizeHtml(html: string | null | undefined): string { if (!html) return ''; const clean = DOMPurify.sanitize(html, { ADD_ATTR: ['target', 'rel'] }); const tpl = document.createElement('template'); tpl.innerHTML = clean; tpl.content.querySelectorAll('a[href]').forEach((a) => { const href = a.getAttribute('href') || ''; if (/^(https?:|mailto:)/i.test(href)) { a.setAttribute('target', '_blank'); a.setAttribute('rel', 'noopener noreferrer'); } }); return tpl.innerHTML;}