|
| 1 | +import os from 'os'; |
| 2 | + |
| 3 | +const platform = os.platform(); |
| 4 | +const newLine = platform === 'win32' ? '\r\n' : '\n'; |
| 5 | + |
| 6 | + |
| 7 | +// Common flags for regex patterns |
| 8 | +const REGEX_FLAGS = 'gim'; |
| 9 | + |
| 10 | +// Define regex patterns as constants |
| 11 | +const REGEXP_HEADER = new RegExp(`${newLine}(#+)(.*)`, 'gim'); |
| 12 | +const REGEXP_H2 = /^## (.*)$/gim; |
| 13 | +const REGEXP_H3 = /^### (.*)$/gim; |
| 14 | + |
| 15 | +const REGEXP_IMAGE = /!\[([^\[]+)\]\(([^\)]+)\)/g; |
| 16 | +const REGEXP_LINK = /\[([^\[]+)\]\(([^\)]+)\)/g; |
| 17 | +const REGEXP_STRONG = /(\*\*|__)(.*?)(\*?)\1/g; |
| 18 | +const REGEXP_DEL = /~~(.*?)~~/g; |
| 19 | +const REGEXP_Q = /:\"(.*?)\"/g; |
| 20 | +const REGEXP_CODE = /`(.*?)`/g; |
| 21 | + |
| 22 | +const REGEXP_BLOCKQUOTE = new RegExp(`${newLine}(>|\\>)(.*)`, 'g'); |
| 23 | +const REGEXP_HR = new RegExp(`${newLine}-{5,}`, 'g'); |
| 24 | +const REGEXP_PARAGRAPH = new RegExp(`${newLine}(.+?)${newLine}`, 'g'); |
| 25 | + |
| 26 | +const REGEXP_BR = new RegExp(`((${newLine}){2,})`, 'g'); |
| 27 | +const REGEXP_EMPTY_BLOCKQUOTE = /<\/blockquote><blockquote>/g; |
| 28 | +const REGEXP_EM = /(\s|>)(\*|_)(.*?)\2(\s|<)/g; |
| 29 | + |
| 30 | +// Export as an organized object |
| 31 | +export const MarkdownTagsRegexes = { |
| 32 | + REGEXP_HEADER, |
| 33 | + REGEXP_H2, |
| 34 | + REGEXP_H3, |
| 35 | + REGEXP_IMAGE, |
| 36 | + REGEXP_LINK, |
| 37 | + REGEXP_STRONG, |
| 38 | + REGEXP_DEL, |
| 39 | + REGEXP_Q, |
| 40 | + REGEXP_CODE, |
| 41 | + REGEXP_BLOCKQUOTE, |
| 42 | + REGEXP_HR, |
| 43 | + REGEXP_PARAGRAPH, |
| 44 | + REGEXP_BR, |
| 45 | + REGEXP_EMPTY_BLOCKQUOTE, |
| 46 | + REGEXP_EM, |
| 47 | +}; |
0 commit comments