diff --git a/src/index.d.ts b/src/index.d.ts deleted file mode 100644 index 9e1191f..0000000 --- a/src/index.d.ts +++ /dev/null @@ -1,111 +0,0 @@ -/** - * Matches markdown headers (lines starting with one or more # characters). - * Example: `# Heading 1`, `## Heading 2` - */ -export const REGEXP_HEADER: RegExp; - -/** - * Matches markdown level 2 headers (lines starting with exactly ##). - * Example: `## Heading 2` - */ -export const REGEXP_H2: RegExp; - -/** - * Matches markdown level 3 headers (lines starting with exactly ###). - * Example: `### Heading 3` - */ -export const REGEXP_H3: RegExp; - -/** - * Matches markdown image syntax. - * Example: `` - */ -export const REGEXP_IMAGE: RegExp; - -/** - * Matches markdown link syntax. - * Example: `[link text](https://example.com)` - */ -export const REGEXP_LINK: RegExp; - -/** - * Matches bold/strong text using `**` or `__` delimiters. - * Example: `**bold**`, `__bold__` - */ -export const REGEXP_STRONG: RegExp; - -/** - * Matches strikethrough text using `~~` delimiters. - * Example: `~~strikethrough~~` - */ -export const REGEXP_DEL: RegExp; - -/** - * Matches quoted text using `::"...":` syntax. - * Example: `::"quoted text":` - */ -export const REGEXP_Q: RegExp; - -/** - * Matches inline code using backtick delimiters. - * Example: `` `code` `` - */ -export const REGEXP_CODE: RegExp; - -/** - * Matches unordered list items (lines starting with `*`). - * Example: `* list item` - */ -export const REGEXP_UL_LIST: RegExp; - -/** - * Matches ordered list items (lines starting with a number and period). - * Example: `1. list item` - */ -export const REGEXP_OL_LIST: RegExp; - -/** - * Matches blockquote lines starting with `>` or `>`. - * Example: `> blockquote text` - */ -export const REGEXP_BLOCKQUOTE: RegExp; - -/** - * Matches horizontal rule lines (5 or more dashes). - * Example: `-----` - */ -export const REGEXP_HR: RegExp; - -/** - * Matches paragraph content between newlines. - */ -export const REGEXP_PARAGRAPH: RegExp; - -/** - * Matches empty unordered list HTML tags (consecutive `
` - */ -export const REGEXP_EMPTY_BLOCKQUOTE: RegExp; - -/** - * Matches italic/emphasis text using `*` or `_` delimiters. - * Example: `*italic*`, `_italic_` - */ -export const REGEXP_EM: RegExp; diff --git a/src/index.js b/src/index.js deleted file mode 100644 index cb39867..0000000 --- a/src/index.js +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Legacy CommonJS/ESM entry kept for compatibility during migration. - * Prefer the TypeScript source (src/index.ts) and the built dist/ package. - */ -export { - REGEXP_HEADER, - REGEXP_H2, - REGEXP_H3, - REGEXP_IMAGE, - REGEXP_LINK, - REGEXP_STRONG, - REGEXP_DEL, - REGEXP_Q, - REGEXP_CODE, - REGEXP_BLOCKQUOTE, - REGEXP_HR, - REGEXP_PARAGRAPH, - REGEXP_BR, - REGEXP_EMPTY_BLOCKQUOTE, - REGEXP_ITALIC, -} from './tags/index.js'; -export { - REGEXP_UL_LIST, - REGEXP_OL_LIST, - REGEXP_EMPTY_UL, - REGEXP_EMPTY_OL, -} from './lists/index.js'; diff --git a/src/index.ts b/src/index.ts index 6c4bba2..76b2762 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,10 @@ /** * markdown-regex * - * A set of ready-to-use RegExp constants for parsing and extracting - * elements from Markdown content. - * + * Ready-to-use RegExp constants for parsing Markdown. * Zero runtime dependencies. Works in Node, browsers, and bundlers. + * + * @packageDocumentation */ export { diff --git a/src/list.js b/src/list.js deleted file mode 100644 index 24ce169..0000000 --- a/src/list.js +++ /dev/null @@ -1,23 +0,0 @@ -import os from "os"; - -const platform = os.platform(); - -const newLine = platform === "win32" ? "\r\n" : "\n"; - -const REGEXP_UL_LIST = new RegExp( - `${newLine}(((\\s{4})?\\*(.*?)${newLine}){1,})`, - "g" -); -const REGEXP_OL_LIST = new RegExp(`${newLine}[0-9]+\\.(.*)`, "g"); - -// @TODO make all regEx the same style -const REGEXP_EMPTY_UL = /<\/ul>\s?/g; -const REGEXP_EMPTY_OL = /<\/ol>\s?
/g; - -export { - REGEXP_UL_LIST, - REGEXP_OL_LIST, - // ----------- - REGEXP_EMPTY_UL, - REGEXP_EMPTY_OL, -}; diff --git a/src/lists/empty-ol.ts b/src/lists/empty-ol.ts index 45d7a49..c1333ac 100644 --- a/src/lists/empty-ol.ts +++ b/src/lists/empty-ol.ts @@ -1,7 +1,8 @@ /** - * Matches consecutive ordered list HTML tags with optional whitespace. - * Useful for cleaning up generated HTML. + * Matches consecutive empty ordered-list HTML tags. + * Useful when cleaning generated HTML. * - * Example:
or
+ * @example
+ * @example
*/ export const REGEXP_EMPTY_OL = /<\/ol>\s?
/g; diff --git a/src/lists/empty-ul.ts b/src/lists/empty-ul.ts index 85f5280..54aa3e0 100644 --- a/src/lists/empty-ul.ts +++ b/src/lists/empty-ul.ts @@ -1,7 +1,8 @@ /** - * Matches consecutive unordered list HTML tags with optional whitespace. - * Useful for cleaning up generated HTML. + * Matches consecutive empty unordered-list HTML tags. + * Useful when cleaning generated HTML. * - * Example:
or
+ * @example
+ * @example
*/ export const REGEXP_EMPTY_UL = /<\/ul>\s?
/g; diff --git a/src/lists/ol.ts b/src/lists/ol.ts index 9c18748..90168dd 100644 --- a/src/lists/ol.ts +++ b/src/lists/ol.ts @@ -1,4 +1,6 @@ -import { NEWLINE } from '../utils'; - -/** Matches ordered list items: 1. item */ -export const REGEXP_OL_LIST = new RegExp(NEWLINE + '[0-9]+\\.(.*)', 'g'); +/** + * Matches an ordered list item (number + period after a newline). + * + * @example 1. first item + */ +export const REGEXP_OL_LIST = /(?:\r\n|\r|\n)\d+\.(.*)/g; diff --git a/src/lists/ul.ts b/src/lists/ul.ts index 800189c..d122d58 100644 --- a/src/lists/ul.ts +++ b/src/lists/ul.ts @@ -1,15 +1,9 @@ -import { NEWLINE } from '../utils'; - /** - * Matches unordered list items (lines starting with *). - * Supports optional leading indentation (4 spaces). - * Platform-agnostic newline handling. + * Matches an unordered list item (`*` after a newline). + * Optional 4-space indent is allowed. * - * Example: - * * item one - * * item two + * @example + * * item one + * * item two */ -export const REGEXP_UL_LIST = new RegExp( - `${NEWLINE}(((?:\\s{4})?\\*(.*?)${NEWLINE}){1,})`, - 'g' -); +export const REGEXP_UL_LIST = /(?:\r\n|\r|\n)(?:\s{4})?\*(.*)/g; diff --git a/src/tags.js b/src/tags.js deleted file mode 100644 index 6d3ed7d..0000000 --- a/src/tags.js +++ /dev/null @@ -1,54 +0,0 @@ -import os from "os"; - -const platform = os.platform(); - -const newLine = platform === "win32" ? "\r\n" : "\n"; - -// TODO: make all regex to feel similar -const REGEXP_HEADER = new RegExp(`${newLine}(#+)(.*)`, "g"); - -const REGEXP_H3 = /^### (.*$)/gim; -const REGEXP_H2 = /^## (.*$)/gim; -// ----------- - -// TODO: make all regex to feel similar -const REGEXP_IMAGE = /!\[([^\[]+)\]\(([^\)]+)\)/g; -// TODO ADD REF PARAMETER TO URL -const REGEXP_LINK = /\[([^\[]+)\]\(([^\)]+)\)/g; -const REGEXP_STRONG = /(\*\*|__)(.*?)(\*?)\1/g; -const REGEXP_DEL = /\~\~(.*?)\~\~/g; -const REGEXP_Q = /\:\"(.*?)\"\:/g; -const REGEXP_CODE = /`(.*?)`/g; - -// ----------- -const REGEXP_BLOCKQUOTE = new RegExp(`${newLine}(>|\\>)(.*)`, "g"); -const REGEXP_HR = new RegExp(`${newLine}-{5,}`, "g"); - -const REGEXP_PARAGRAPH = new RegExp(`${newLine}(.+?)${newLine}`, "g"); - -// ----------- -const REGEXP_BR = new RegExp(`((${newLine}){2,})`, "g"); - -const REGEXP_EMPTY_BLOCKQUOTE = /<\/blockquote>
/g; - -const REGEXP_EM = /(\s|>)(\*|_)(.*?)\2(\s|<)/g; - -export { - REGEXP_HEADER, - REGEXP_H2, - REGEXP_H3, - REGEXP_IMAGE, - REGEXP_LINK, - REGEXP_STRONG, - REGEXP_DEL, - REGEXP_Q, - REGEXP_CODE, - // ----------- - REGEXP_BLOCKQUOTE, - REGEXP_HR, - REGEXP_PARAGRAPH, - // ----------- - REGEXP_BR, - REGEXP_EMPTY_BLOCKQUOTE, - REGEXP_EM, -}; diff --git a/src/tags/blockquote.ts b/src/tags/blockquote.ts index 7ddc951..75a0693 100644 --- a/src/tags/blockquote.ts +++ b/src/tags/blockquote.ts @@ -1,9 +1,7 @@ -import { NEWLINE } from '../utils'; - /** - * Matches blockquote lines starting with > or >. - * Platform-agnostic newline handling. + * Matches blockquote lines starting with `>` or `>` after a newline. * - * Example: > blockquote text + * @example + * > quoted paragraph */ -export const REGEXP_BLOCKQUOTE = new RegExp(`${NEWLINE}(>|>)(.*)`, 'g'); +export const REGEXP_BLOCKQUOTE = /(?:\r\n|\r|\n)(?:>|>)(.*)/g; diff --git a/src/tags/br.ts b/src/tags/br.ts index 44b094f..ebdb338 100644 --- a/src/tags/br.ts +++ b/src/tags/br.ts @@ -1,7 +1,7 @@ -import { NEWLINE } from '../utils'; - /** * Matches two or more consecutive newlines (paragraph / line breaks). - * Platform-agnostic. + * Understands `\n`, `\r\n` and `\r`. + * + * @example a\\n\\nb */ -export const REGEXP_BR = new RegExp(`((${NEWLINE}){2,})`, 'g'); +export const REGEXP_BR = /(?:\r\n|\r|\n){2,}/g; diff --git a/src/tags/code.ts b/src/tags/code.ts index 4ebe11c..994b652 100644 --- a/src/tags/code.ts +++ b/src/tags/code.ts @@ -1,6 +1,6 @@ /** * Matches inline code using single backtick delimiters. * - * Example: `code` + * @example `code` */ -export const REGEXP_CODE = /`(.*?)`/g; +export const REGEXP_CODE = /`(.+?)`/g; diff --git a/src/tags/del.ts b/src/tags/del.ts index 044d80d..f96b631 100644 --- a/src/tags/del.ts +++ b/src/tags/del.ts @@ -1,6 +1,6 @@ /** - * Matches strikethrough text using ~~ delimiters. + * Matches strikethrough text using `~~` delimiters. * - * Example: ~~strikethrough~~ + * @example ~~deleted~~ */ -export const REGEXP_DEL = /~~(.*?)~~/g; +export const REGEXP_DEL = /~~(.+?)~~/g; diff --git a/src/tags/em.js b/src/tags/em.js deleted file mode 100644 index 90999a5..0000000 --- a/src/tags/em.js +++ /dev/null @@ -1,4 +0,0 @@ -/** - * @deprecated Use REGEXP_ITALIC from './italic.js'. Delete this file. - */ -export { REGEXP_ITALIC as REGEXP_ITALIC_ONLY } from './italic.js'; diff --git a/src/tags/em.ts b/src/tags/em.ts deleted file mode 100644 index 6f5927f..0000000 --- a/src/tags/em.ts +++ /dev/null @@ -1,6 +0,0 @@ -/** - * @deprecated Removed. Use REGEXP_ITALIC from './italic' instead. - * This file only exists so accidental imports fail clearly at review time. - * Delete this file after the hygiene pass (`git rm src/tags/em.ts`). - */ -export { REGEXP_ITALIC as REGEXP_ITALIC_ONLY } from './italic'; diff --git a/src/tags/empty-blockquote.ts b/src/tags/empty-blockquote.ts index 0757740..1fc4125 100644 --- a/src/tags/empty-blockquote.ts +++ b/src/tags/empty-blockquote.ts @@ -1,7 +1,7 @@ /** * Matches consecutive empty blockquote HTML tags. - * Useful for cleaning up generated HTML. + * Useful when cleaning generated HTML. * - * Example:+ * @example*/ export const REGEXP_EMPTY_BLOCKQUOTE = /<\/blockquote>/g; diff --git a/src/tags/header.ts b/src/tags/header.ts index 46c8164..985935a 100644 --- a/src/tags/header.ts +++ b/src/tags/header.ts @@ -1,6 +1,26 @@ -/** ATX headers after start-of-string or a newline. */ +/** + * Matches ATX markdown headers (lines starting with 1–6 `#` characters). + * Matches at the start of the string or after a newline (Unix / Windows / old Mac). + * + * Captures: [1] the newline or start, [2] the `#` run, [3] the heading text. + * + * @example + * # Heading 1 + * ## Heading 2 + * ### Heading 3 + */ export const REGEXP_HEADER = /(^|\r\n|\r|\n)(#{1,6})\s+(.*)/gm; +/** + * Matches level-2 headers only (`## …`). + * + * @example ## Heading 2 + */ export const REGEXP_H2 = /^##\s+(.*)$/gim; +/** + * Matches level-3 headers only (`### …`). + * + * @example ### Heading 3 + */ export const REGEXP_H3 = /^###\s+(.*)$/gim; diff --git a/src/tags/hr.ts b/src/tags/hr.ts index 962cc7d..3c4420e 100644 --- a/src/tags/hr.ts +++ b/src/tags/hr.ts @@ -1,9 +1,6 @@ -import { NEWLINE } from '../utils'; - /** - * Matches horizontal rule lines (5 or more dashes). - * Platform-agnostic newline handling. + * Matches a horizontal rule: 5 or more dashes on their own line. * - * Example: ----- + * @example -----\n */ -export const REGEXP_HR = new RegExp(`${NEWLINE}-{5,}`, 'g'); +export const REGEXP_HR = /(?:\r\n|\r|\n)-{5,}/g; diff --git a/src/tags/image.ts b/src/tags/image.ts index ab8483c..bfc49d4 100644 --- a/src/tags/image.ts +++ b/src/tags/image.ts @@ -1,8 +1,7 @@ /** * Matches markdown image syntax. - * Captures: [1] alt text, [2] url + * Captures: [1] alt text, [2] url. + * + * @example  */ -export const REGEXP_IMAGE = new RegExp( - '!\\[([^\\[]*?)\\]\\(([^)]+?)\\)', - 'g' -); +export const REGEXP_IMAGE = /!\[([^\[]*?)\]\(([^)]+?)\)/g; diff --git a/src/tags/index.js b/src/tags/index.js deleted file mode 100644 index b5df3a9..0000000 --- a/src/tags/index.js +++ /dev/null @@ -1,13 +0,0 @@ -export { REGEXP_HEADER, REGEXP_H2, REGEXP_H3 } from './header.js'; -export { REGEXP_BLOCKQUOTE } from './blockquote.js'; -export { REGEXP_CODE } from './code.js'; -export { REGEXP_LINK } from './link.js'; -export { REGEXP_IMAGE } from './image.js'; -export { REGEXP_STRONG } from './strong.js'; -export { REGEXP_DEL } from './del.js'; -export { REGEXP_ITALIC } from './italic.js'; -export { REGEXP_HR } from './hr.js'; -export { REGEXP_BR } from './br.js'; -export { REGEXP_PARAGRAPH } from './paragraph.js'; -export { REGEXP_Q } from './quote.js'; -export { REGEXP_EMPTY_BLOCKQUOTE } from './empty-blockquote.js'; diff --git a/src/tags/italic.ts b/src/tags/italic.ts index 7e806c3..e0dc7fe 100644 --- a/src/tags/italic.ts +++ b/src/tags/italic.ts @@ -1,2 +1,9 @@ -/** Italic: *text* or _text_, with surrounding whitespace or start/end. */ +/** + * Matches italic/emphasis using `*` or `_` delimiters. + * Requires surrounding whitespace (or start/end / `>` / `<`) so that + * mid-word underscores are less likely to match. + * + * @example *italic* + * @example _italic_ + */ export const REGEXP_ITALIC = /(^|[\s>])(\*|_)(.+?)\2([\s<]|$)/g; diff --git a/src/tags/link.ts b/src/tags/link.ts index 5ac888f..976ac1a 100644 --- a/src/tags/link.ts +++ b/src/tags/link.ts @@ -1,8 +1,7 @@ /** * Matches markdown link syntax. - * Captures: [1] link text, [2] url + * Captures: [1] link text, [2] url. + * + * @example [GitHub](https://github.com) */ -export const REGEXP_LINK = new RegExp( - '\\[([^\\[]*?)\\]\\(([^)]+?)\\)', - 'g' -); +export const REGEXP_LINK = /\[([^\[]*?)\]\(([^)]+?)\)/g; diff --git a/src/tags/paragraph.ts b/src/tags/paragraph.ts index a7991de..cce1d15 100644 --- a/src/tags/paragraph.ts +++ b/src/tags/paragraph.ts @@ -1,7 +1,6 @@ -import { NEWLINE } from '../utils'; - /** - * Matches paragraph content between newlines. - * Platform-agnostic newline handling. + * Matches paragraph-like content sitting between two newlines. + * + * @example \\nhello world\\n */ -export const REGEXP_PARAGRAPH = new RegExp(`${NEWLINE}(.+?)${NEWLINE}`, 'g'); +export const REGEXP_PARAGRAPH = /(?:\r\n|\r|\n)(.+?)(?:\r\n|\r|\n)/g; diff --git a/src/tags/quote.ts b/src/tags/quote.ts index 030f9fd..8b3bf5e 100644 --- a/src/tags/quote.ts +++ b/src/tags/quote.ts @@ -1,7 +1,6 @@ /** - * Matches quoted text using the non-standard :"...": syntax - * used by some email/newsletter pipelines. + * Matches the custom quote syntax used in some newsletter pipelines. * - * Example: :"quoted text": + * @example :"quoted text": */ export const REGEXP_Q = /:"(.*?)":/g; diff --git a/src/tags/strong.ts b/src/tags/strong.ts index 35d9c2c..301628f 100644 --- a/src/tags/strong.ts +++ b/src/tags/strong.ts @@ -1,2 +1,8 @@ -/** Bold: **text** or __text__. */ +/** + * Matches bold/strong text using `**` or `__` delimiters. + * Captures the inner text in group 1 (`**`) or group 2 (`__`). + * + * @example **bold** + * @example __bold__ + */ export const REGEXP_STRONG = /\*\*(.+?)\*\*|__(.+?)__/g; diff --git a/src/utils.js b/src/utils.js deleted file mode 100644 index 701fdc4..0000000 --- a/src/utils.js +++ /dev/null @@ -1,5 +0,0 @@ -import os from "os"; - -const platform = os.platform(); - -export const newLine = platform === "win32" ? "\r\n" : "\n"; diff --git a/src/utils.ts b/src/utils.ts index e8e526c..03994d1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,6 @@ -/** Unix / Windows / old-Mac newline as a RegExp source fragment. */ -export const NEWLINE = String.raw`(?:\r\n|\r|\n)`; - -export function nl(pattern: string, flags = 'g'): RegExp { - return new RegExp(pattern.replace(/\n/g, NEWLINE), flags); -} +/** + * Flexible newline fragment: Unix (`\n`), Windows (`\r\n`), old Mac (`\r`). + * Prefer embedding `(?:\r\n|\r|\n)` directly in regex literals. + * This constant is kept for callers that compose patterns at runtime. + */ +export const NEWLINE = '(?:\r\n|\r|\n)';