|
|
|
|
|
|
|
|
|
|
| |
| |
| |
|
|
| export const MarkdownSyntax = {
|
|
|
|
|
|
|
|
|
| blocks: {
|
|
|
| heading: {
|
| patterns: [
|
| /^(#{1,6})\s+(.+)$/,
|
| /^(.+)\n={3,}\s*$/,
|
| /^(.+)\n-{3,}\s*$/
|
| ],
|
| detect: (line, nextLine) => {
|
|
|
| const atx = line.match(/^(#{1,6})\s+(.+)$/);
|
| if (atx) {
|
| return {
|
| type: 'heading',
|
| level: atx[1].length,
|
| content: atx[2].trim(),
|
| style: 'atx'
|
| };
|
| }
|
|
|
|
|
| if (nextLine) {
|
| if (nextLine.match(/^={3,}\s*$/)) {
|
| return { type: 'heading', level: 1, content: line.trim(), style: 'setext' };
|
| }
|
| if (nextLine.match(/^-{3,}\s*$/) && !line.match(/^\s*$/)) {
|
| return { type: 'heading', level: 2, content: line.trim(), style: 'setext' };
|
| }
|
| }
|
|
|
| return null;
|
| }
|
| },
|
|
|
|
|
| hr: {
|
| patterns: [
|
| /^-{3,}\s*$/,
|
| /^\*{3,}\s*$/,
|
| /^_{3,}\s*$/,
|
| /^- {0,2}- {0,2}-/,
|
| /^\* {0,2}\* {0,2}\*/,
|
| /^_ {0,2}_ {0,2}_/
|
| ],
|
| detect: (line) => {
|
| for (const pattern of MarkdownSyntax.blocks.hr.patterns) {
|
| if (pattern.test(line.trim())) {
|
| return { type: 'hr' };
|
| }
|
| }
|
| return null;
|
| }
|
| },
|
|
|
|
|
| codeBlock: {
|
| patterns: [
|
| /^```(\w*)\s*$/,
|
| /^~~~(\w*)\s*$/,
|
| /^ (.+)$/,
|
| /^\t(.+)$/
|
| ],
|
| detect: (line) => {
|
|
|
| const fenced = line.match(/^```(\w*)\s*$/);
|
| if (fenced) {
|
| return { type: 'code-fence-start', language: fenced[1] || 'text' };
|
| }
|
|
|
| const tilde = line.match(/^~~~(\w*)\s*$/);
|
| if (tilde) {
|
| return { type: 'code-fence-start', language: tilde[1] || 'text', fence: '~~~' };
|
| }
|
|
|
|
|
| if (line.match(/^ (.+)$/) || line.match(/^\t(.+)$/)) {
|
| return { type: 'code-indented', content: line.replace(/^ |\t/, '') };
|
| }
|
|
|
| return null;
|
| }
|
| },
|
|
|
|
|
| blockquote: {
|
| patterns: [
|
| /^>\s?(.*)$/,
|
| /^> ?> ?(.*)$/
|
| ],
|
| detect: (line) => {
|
| const match = line.match(/^(>+)\s?(.*)$/);
|
| if (match) {
|
| return {
|
| type: 'blockquote',
|
| level: match[1].length,
|
| content: match[2]
|
| };
|
| }
|
| return null;
|
| }
|
| },
|
|
|
|
|
| list: {
|
| patterns: [
|
| /^(\s*)([-*+])\s+(.+)$/,
|
| /^(\s*)(\d{1,9})[.)]\s+(.+)$/
|
| ],
|
| detect: (line) => {
|
|
|
| const unordered = line.match(/^(\s*)([-*+])\s+(.+)$/);
|
| if (unordered) {
|
| return {
|
| type: 'list',
|
| ordered: false,
|
| indent: unordered[1].length,
|
| marker: unordered[2],
|
| content: unordered[3]
|
| };
|
| }
|
|
|
|
|
| const ordered = line.match(/^(\s*)(\d{1,9})[.)]\s+(.+)$/);
|
| if (ordered) {
|
| return {
|
| type: 'list',
|
| ordered: true,
|
| indent: ordered[1].length,
|
| number: parseInt(ordered[2]),
|
| content: ordered[3]
|
| };
|
| }
|
|
|
| return null;
|
| }
|
| },
|
|
|
|
|
| taskList: {
|
| patterns: [
|
| /^(\s*)([-*+])\s+\[([ xX])\]\s+(.+)$/
|
| ],
|
| detect: (line) => {
|
| const match = line.match(/^(\s*)([-*+])\s+\[([ xX])\]\s+(.+)$/);
|
| if (match) {
|
| return {
|
| type: 'task-list',
|
| indent: match[1].length,
|
| checked: match[3].toLowerCase() === 'x',
|
| content: match[4]
|
| };
|
| }
|
| return null;
|
| }
|
| },
|
|
|
|
|
| table: {
|
| patterns: [
|
| /^\|(.+)\|$/,
|
| /^\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)+\|?$/
|
| ],
|
| detect: (line) => {
|
| if (line.match(/^\|(.+)\|$/)) {
|
| return { type: 'table-row', cells: line.split('|').filter(c => c.trim()) };
|
| }
|
| if (line.match(/^\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)+\|?$/)) {
|
| return { type: 'table-separator' };
|
| }
|
| return null;
|
| }
|
| },
|
|
|
|
|
| html: {
|
| patterns: [
|
| /^<([a-z][a-z0-9-]*)\b[^>]*>/i,
|
| /^<\/([a-z][a-z0-9-]*)\s*>/i,
|
| /^<!--/,
|
| /^<\?/,
|
| /^<![A-Z]/,
|
| /^<!\[CDATA\[/
|
| ],
|
| detect: (line) => {
|
| for (const pattern of MarkdownSyntax.blocks.html.patterns) {
|
| if (pattern.test(line.trim())) {
|
| return { type: 'html', content: line };
|
| }
|
| }
|
| return null;
|
| }
|
| },
|
|
|
|
|
| metadata: {
|
| patterns: [
|
| /^---\s*$/,
|
| /^\+\+\+\s*$/,
|
| /^;;;\s*$/
|
| ],
|
| detect: (line) => {
|
| if (line.trim() === '---') return { type: 'metadata', format: 'yaml' };
|
| if (line.trim() === '+++') return { type: 'metadata', format: 'toml' };
|
| if (line.trim() === ';;;') return { type: 'metadata', format: 'json' };
|
| return null;
|
| }
|
| }
|
| },
|
|
|
|
|
|
|
|
|
|
|
| inline: {
|
|
|
| emphasis: {
|
| patterns: [
|
| { regex: /\*\*\*(.+?)\*\*\*/g, type: 'bold-italic', tag: 'strong-em' },
|
| { regex: /___(.+?)___/g, type: 'bold-italic', tag: 'strong-em' },
|
| { regex: /\*\*(.+?)\*\*/g, type: 'bold', tag: 'strong' },
|
| { regex: /__(.+?)__/g, type: 'bold', tag: 'strong' },
|
| { regex: /\*(.+?)\*/g, type: 'italic', tag: 'em' },
|
| { regex: /_(.+?)_/g, type: 'italic', tag: 'em' }
|
| ]
|
| },
|
|
|
|
|
| strikethrough: {
|
| patterns: [
|
| { regex: /~~(.+?)~~/g, type: 'strikethrough', tag: 'del' }
|
| ]
|
| },
|
|
|
|
|
| code: {
|
| patterns: [
|
| { regex: /``(.+?)``/g, type: 'code', tag: 'code' },
|
| { regex: /`(.+?)`/g, type: 'code', tag: 'code' }
|
| ]
|
| },
|
|
|
|
|
| link: {
|
| patterns: [
|
| { regex: /\[([^\]]+)\]\(([^)\s]+)(?:\s+"([^"]+)")?\)/g, type: 'link' },
|
| { regex: /\[([^\]]+)\]\[([^\]]+)\]/g, type: 'link-ref' },
|
| { regex: /\[([^\]]+)\]/g, type: 'link-shortcut' },
|
| { regex: /<(https?:\/\/[^>]+)>/g, type: 'autolink' },
|
| { regex: /<([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})>/g, type: 'email' }
|
| ]
|
| },
|
|
|
|
|
| image: {
|
| patterns: [
|
| { regex: /!\[([^\]]*)\]\(([^)\s]+)(?:\s+"([^"]+)")?\)/g, type: 'image' },
|
| { regex: /!\[([^\]]*)\]\[([^\]]+)\]/g, type: 'image-ref' }
|
| ]
|
| },
|
|
|
|
|
| lineBreak: {
|
| patterns: [
|
| { regex: / \n/g, type: 'soft-break' },
|
| { regex: /\\\n/g, type: 'hard-break' },
|
| { regex: /<br\s*\/?>/gi, type: 'html-break' }
|
| ]
|
| },
|
|
|
|
|
| escape: {
|
| characters: ['\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '#', '+', '-', '.', '!', '|']
|
| },
|
|
|
|
|
| emoji: {
|
| pattern: /:([a-z0-9_+-]+):/g
|
| },
|
|
|
|
|
| mention: {
|
| pattern: /@([a-zA-Z0-9_-]+)/g
|
| },
|
|
|
|
|
| hashtag: {
|
| pattern: /#([a-zA-Z0-9_-]+)/g
|
| }
|
| },
|
|
|
|
|
|
|
|
|
|
|
| special: {
|
|
|
| footnote: {
|
| definition: /^\[\^([^\]]+)\]:\s+(.+)$/,
|
| reference: /\[\^([^\]]+)\]/g
|
| },
|
|
|
|
|
| abbreviation: {
|
| pattern: /^\*\[([^\]]+)\]:\s+(.+)$/
|
| },
|
|
|
|
|
|
|
|
|
| definitionList: {
|
| term: /^([^\n:]+)\s*$/,
|
| definition: /^:\s+(.+)$/
|
| },
|
|
|
|
|
| math: {
|
| inline: /\$([^$]+)\$/g,
|
| block: /\$\$([^$]+)\$\$/g
|
| }
|
| }
|
| };
|
|
|
| |
| |
|
|
| export function detectBlockType(line, nextLine = null) {
|
|
|
| for (const [blockName, block] of Object.entries(MarkdownSyntax.blocks)) {
|
| const result = block.detect(line, nextLine);
|
| if (result) {
|
| return result;
|
| }
|
| }
|
| return { type: 'paragraph' };
|
| }
|
|
|
| |
| |
|
|
| function escapeHtml(str) {
|
| if (str == null) return '';
|
| return String(str)
|
| .replace(/&/g, '&')
|
| .replace(/</g, '<')
|
| .replace(/>/g, '>')
|
| .replace(/"/g, '"')
|
| .replace(/'/g, ''');
|
| }
|
|
|
| export function parseInline(text) {
|
| if (!text) return '';
|
|
|
| let result = escapeHtml(text);
|
|
|
|
|
| result = result.replace(/!\[([^\]]*)\]\(([^)\s]+)(?:\s+"([^"]+)")?\)/g, (match, alt, url, title) => {
|
| const altAttr = escapeHtml(alt || '');
|
| const srcAttr = escapeHtml(url || '');
|
| const titleAttr = title ? ` title="${escapeHtml(title)}"` : '';
|
| return `<img src="${srcAttr}" alt="${altAttr}"${titleAttr}>`;
|
| });
|
|
|
|
|
| result = result.replace(/\[([^\]]+)\]\(([^)\s]+)(?:\s+"([^"]+)")?\)/g, (match, label, url, title) => {
|
| const textContent = escapeHtml(label || '');
|
| const hrefAttr = escapeHtml(url || '#');
|
| const titleAttr = title ? ` title="${escapeHtml(title)}"` : '';
|
| return `<a href="${hrefAttr}"${titleAttr}>${textContent}</a>`;
|
| });
|
|
|
|
|
| result = result.replace(/<(https?:\/\/[^&]+)>/g, (match, url) => {
|
| const hrefAttr = escapeHtml(url);
|
| return `<a href="${hrefAttr}">${hrefAttr}</a>`;
|
| });
|
|
|
|
|
| result = result.replace(/<([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})>/g, (match, email) => {
|
| const mailto = `mailto:${email}`;
|
| return `<a href="${escapeHtml(mailto)}">${escapeHtml(email)}</a>`;
|
| });
|
|
|
|
|
| const replacements = [
|
| { regex: /\*\*\*(.+?)\*\*\*/g, wrap: content => `<strong><em>${content}</em></strong>` },
|
| { regex: /___(.+?)___/g, wrap: content => `<strong><em>${content}</em></strong>` },
|
| { regex: /\*\*(.+?)\*\*/g, wrap: content => `<strong>${content}</strong>` },
|
| { regex: /__(.+?)__/g, wrap: content => `<strong>${content}</strong>` },
|
| { regex: /\*(.+?)\*/g, wrap: content => `<em>${content}</em>` },
|
| { regex: /_(.+?)_/g, wrap: content => `<em>${content}</em>` },
|
| { regex: /~~(.+?)~~/g, wrap: content => `<del>${content}</del>` },
|
| { regex: /``(.+?)``/g, wrap: content => `<code>${content}</code>` },
|
| { regex: /`(.+?)`/g, wrap: content => `<code>${content}</code>` }
|
| ];
|
|
|
| replacements.forEach(({ regex, wrap }) => {
|
| result = result.replace(regex, (match, content) => wrap(content));
|
| });
|
|
|
|
|
| result = result.replace(/ \n/g, '<br>');
|
| result = result.replace(/\\\n/g, '<br>');
|
| result = result.replace(/<br\s*\/?>/gi, '<br>');
|
|
|
| return result;
|
| }
|
|
|
| |
| |
|
|
| export function getSyntaxInfo(category) {
|
| const info = {
|
| blocks: 'Block-level elements (headings, lists, code blocks, etc.)',
|
| inline: 'Inline elements (emphasis, links, images, etc.)',
|
| special: 'Special constructs (footnotes, math, definitions, etc.)'
|
| };
|
|
|
| return info[category] || 'Unknown category';
|
| }
|
|
|