(text: string, html: string)
| 1 | // Check if text contains markdown syntax |
| 2 | export const checkMarkdownSyntax = (text: string, html: string) => { |
| 3 | if (!text) return false |
| 4 | if (!html) return true |
| 5 | // Split text into paragraphs |
| 6 | const rows = text.split(/\r\n|\n/) || '' |
| 7 | |
| 8 | // Count of all valid paragraphs |
| 9 | let rowCount = 0 |
| 10 | // Count of paragraphs with markdown syntax |
| 11 | let validCount = 0 |
| 12 | // Flag for code block |
| 13 | let isCodeblock = false |
| 14 | |
| 15 | // Extract ordered list texts from html |
| 16 | const root = new DOMParser().parseFromString(html, 'text/html') |
| 17 | const lis = root.querySelectorAll('li') |
| 18 | const orderTexts: string[] = [] |
| 19 | lis.forEach(li => { |
| 20 | const text = li.textContent ?? '' |
| 21 | if (li.parentElement?.nodeName === 'OL' || /\d\.\s+/.test(text)) { |
| 22 | orderTexts.push(text) |
| 23 | } |
| 24 | }) |
| 25 | |
| 26 | // Check each paragraph |
| 27 | for (let i = 0; i < rows.length; i++) { |
| 28 | const rowText = rows[i] |
| 29 | if (!rowText.trim()) continue |
| 30 | if (rowText.startsWith('```')) { |
| 31 | if (!isCodeblock) { |
| 32 | isCodeblock = true |
| 33 | validCount++ |
| 34 | rowCount++ |
| 35 | } else { |
| 36 | isCodeblock = false |
| 37 | } |
| 38 | continue |
| 39 | } |
| 40 | if (isCodeblock) continue |
| 41 | rowCount++ |
| 42 | |
| 43 | // Check if row contains markdown syntax |
| 44 | if (/^(#|\*|-|\+|\[ \]|\[x\]|>){1,}\s+/.test(rowText)) { |
| 45 | validCount++ |
| 46 | } else if (/^\d\.\s+/.test(rowText)) { |
| 47 | if ( |
| 48 | !orderTexts.includes(rowText) && |
| 49 | !orderTexts.includes(rowText.replace(/^\d\./, '').trim()) |
| 50 | ) { |
| 51 | validCount++ |
| 52 | } |
| 53 | } else if (/^(---|\*\*\*|\+\+\+)/.test(rowText)) { |
| 54 | validCount++ |
| 55 | } else if (/(\*|~|\^|_|\`|\]\(https?:\/\/)/.test(rowText)) { |
| 56 | validCount++ |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // Return true if more than half of the paragraphs contain markdown syntax |
no outgoing calls
no test coverage detected
searching dependent graphs…