// ══════════════════════════════════════════════════════════════════════════════ // Markdown Syntax Dictionary // Complete Markdown syntax patterns and recognition rules // ══════════════════════════════════════════════════════════════════════════════ /** * Markdown Syntax Dictionary * Based on CommonMark + GitHub Flavored Markdown (GFM) */ export const MarkdownSyntax = { // ════════════════════════════════════════════════════════════════════════════ // Block-level Elements // ════════════════════════════════════════════════════════════════════════════ blocks: { // Headings: # H1, ## H2, ### H3, #### H4, ##### H5, ###### H6 heading: { patterns: [ /^(#{1,6})\s+(.+)$/, // ATX style: # Heading /^(.+)\n={3,}\s*$/, // Setext H1: underline with === /^(.+)\n-{3,}\s*$/ // Setext H2: underline with --- ], detect: (line, nextLine) => { // ATX headings const atx = line.match(/^(#{1,6})\s+(.+)$/); if (atx) { return { type: 'heading', level: atx[1].length, content: atx[2].trim(), style: 'atx' }; } // Setext headings (require next line) 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; } }, // Horizontal Rules: ---, ***, ___ 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; } }, // Code Blocks: ```lang or indented 4 spaces codeBlock: { patterns: [ /^```(\w*)\s*$/, // Fenced code block start /^~~~(\w*)\s*$/, // Alternative fence /^ (.+)$/, // Indented code (4 spaces) /^\t(.+)$/ // Indented code (tab) ], detect: (line) => { // Fenced code block 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: '~~~' }; } // Indented code if (line.match(/^ (.+)$/) || line.match(/^\t(.+)$/)) { return { type: 'code-indented', content: line.replace(/^ |\t/, '') }; } return null; } }, // Blockquotes: > Quote blockquote: { patterns: [ /^>\s?(.*)$/, // > Quote /^> ?> ?(.*)$/ // Nested: >> Quote ], detect: (line) => { const match = line.match(/^(>+)\s?(.*)$/); if (match) { return { type: 'blockquote', level: match[1].length, content: match[2] }; } return null; } }, // Lists: Unordered (-, *, +) and Ordered (1., 2.) list: { patterns: [ /^(\s*)([-*+])\s+(.+)$/, // Unordered: -, *, + /^(\s*)(\d{1,9})[.)]\s+(.+)$/ // Ordered: 1., 2), etc. ], detect: (line) => { // Unordered list const unordered = line.match(/^(\s*)([-*+])\s+(.+)$/); if (unordered) { return { type: 'list', ordered: false, indent: unordered[1].length, marker: unordered[2], content: unordered[3] }; } // Ordered list 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; } }, // Task Lists (GFM): - [ ] Task or - [x] Done 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; } }, // Tables (GFM): | Header | Header | table: { patterns: [ /^\|(.+)\|$/, // Table row /^\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)+\|?$/ // Separator ], 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 Blocks: