quicknotes/frontend/src/lib/markdown.ts

24 lines
911 B
TypeScript

import { marked } from 'marked';
export function stripMarkdown(text: string): string {
// Remove common markdown syntax
return text
.replace(/[*_~`#]+/g, '') // Remove formatting characters
.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1') // Replace links with just their text
.replace(/!\[[^\]]*\]\([^)]+\)/g, '') // Remove images
.replace(/^>+\s*/gm, '') // Remove blockquotes
.replace(/^[-+*]\s+/gm, '') // Remove list markers
.replace(/^\d+\.\s+/gm, '') // Remove numbered list markers
.replace(/^#{1,6}\s+/gm, '') // Remove heading markers
.replace(/\n{2,}/g, '\n') // Normalize multiple newlines
.trim();
}
export async function renderMarkdown(text: string): Promise<string> {
// Replace [[link]] with anchor tags having data-title attribute
const linkedText = text.replace(
/\[\[([^\]]+)\]\]/g,
'<a href="#" class="note-link" data-title="$1">$1</a>'
);
return marked(linkedText);
}