quikchat 1.2.4 → 1.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/build-manifest.json +73 -80
- package/dist/quikchat-md-full.cjs.js +536 -255
- package/dist/quikchat-md-full.cjs.js.map +1 -1
- package/dist/quikchat-md-full.cjs.min.js +3 -3
- package/dist/quikchat-md-full.cjs.min.js.map +1 -1
- package/dist/quikchat-md-full.esm.js +536 -255
- package/dist/quikchat-md-full.esm.js.map +1 -1
- package/dist/quikchat-md-full.esm.min.js +3 -3
- package/dist/quikchat-md-full.esm.min.js.map +1 -1
- package/dist/quikchat-md-full.umd.js +536 -255
- package/dist/quikchat-md-full.umd.js.map +1 -1
- package/dist/quikchat-md-full.umd.min.js +3 -3
- package/dist/quikchat-md-full.umd.min.js.map +1 -1
- package/dist/quikchat-md.cjs.js +520 -245
- package/dist/quikchat-md.cjs.js.map +1 -1
- package/dist/quikchat-md.cjs.min.js +3 -3
- package/dist/quikchat-md.cjs.min.js.map +1 -1
- package/dist/quikchat-md.esm.js +520 -245
- package/dist/quikchat-md.esm.js.map +1 -1
- package/dist/quikchat-md.esm.min.js +3 -3
- package/dist/quikchat-md.esm.min.js.map +1 -1
- package/dist/quikchat-md.umd.js +520 -245
- package/dist/quikchat-md.umd.js.map +1 -1
- package/dist/quikchat-md.umd.min.js +3 -3
- package/dist/quikchat-md.umd.min.js.map +1 -1
- package/dist/quikchat.cjs.js +2 -2
- package/dist/quikchat.cjs.js.map +1 -1
- package/dist/quikchat.cjs.min.js +1 -1
- package/dist/quikchat.cjs.min.js.map +1 -1
- package/dist/quikchat.css +351 -120
- package/dist/quikchat.esm.js +2 -2
- package/dist/quikchat.esm.js.map +1 -1
- package/dist/quikchat.esm.min.js +1 -1
- package/dist/quikchat.esm.min.js.map +1 -1
- package/dist/quikchat.min.css +1 -1
- package/dist/quikchat.umd.js +2 -2
- package/dist/quikchat.umd.js.map +1 -1
- package/dist/quikchat.umd.min.js +1 -1
- package/dist/quikchat.umd.min.js.map +1 -1
- package/package.json +6 -5
package/dist/quikchat-md.esm.js
CHANGED
|
@@ -430,7 +430,7 @@ var quikchat = /*#__PURE__*/function () {
|
|
|
430
430
|
value: function _autoGrowTextarea() {
|
|
431
431
|
var el = this._textEntry;
|
|
432
432
|
el.style.height = 'auto';
|
|
433
|
-
var maxHeight =
|
|
433
|
+
var maxHeight = 120;
|
|
434
434
|
el.style.height = Math.min(el.scrollHeight, maxHeight) + 'px';
|
|
435
435
|
el.style.overflowY = el.scrollHeight > maxHeight ? 'auto' : 'hidden';
|
|
436
436
|
}
|
|
@@ -898,7 +898,7 @@ var quikchat = /*#__PURE__*/function () {
|
|
|
898
898
|
key: "version",
|
|
899
899
|
value: function version() {
|
|
900
900
|
return {
|
|
901
|
-
"version": "1.2.
|
|
901
|
+
"version": "1.2.6",
|
|
902
902
|
"license": "BSD-2",
|
|
903
903
|
"url": "https://github/deftio/quikchat"
|
|
904
904
|
};
|
|
@@ -958,35 +958,140 @@ var quikchat = /*#__PURE__*/function () {
|
|
|
958
958
|
|
|
959
959
|
/**
|
|
960
960
|
* quikdown - Lightweight Markdown Parser
|
|
961
|
-
* @version 1.
|
|
961
|
+
* @version 1.2.10
|
|
962
962
|
* @license BSD-2-Clause
|
|
963
963
|
* @copyright DeftIO 2025
|
|
964
964
|
*/
|
|
965
965
|
/**
|
|
966
|
-
*
|
|
967
|
-
*
|
|
968
|
-
*
|
|
969
|
-
*
|
|
970
|
-
*
|
|
971
|
-
*
|
|
972
|
-
*
|
|
973
|
-
*
|
|
974
|
-
*
|
|
975
|
-
*
|
|
966
|
+
* quikdown_classify — Shared line-classification utilities
|
|
967
|
+
* ═════════════════════════════════════════════════════════
|
|
968
|
+
*
|
|
969
|
+
* Pure functions for classifying markdown lines. Used by both the main
|
|
970
|
+
* parser (quikdown.js) and the editor (quikdown_edit.js) so the logic
|
|
971
|
+
* lives in one place.
|
|
972
|
+
*
|
|
973
|
+
* All functions operate on a **trimmed** line (caller must trim).
|
|
974
|
+
* None use regexes with nested quantifiers — every check is either a
|
|
975
|
+
* simple regex or a linear scan, so there is zero ReDoS risk.
|
|
976
976
|
*/
|
|
977
977
|
|
|
978
|
-
// Version will be injected at build time
|
|
979
|
-
const quikdownVersion = '1.1.1';
|
|
980
978
|
|
|
981
|
-
|
|
979
|
+
/**
|
|
980
|
+
* Dash-only HR check — exact parity with the main parser's original
|
|
981
|
+
* regex `/^---+\s*$/`. Only matches lines of three or more dashes
|
|
982
|
+
* with optional trailing whitespace (no interspersed spaces).
|
|
983
|
+
*
|
|
984
|
+
* @param {string} trimmed The line, already trimmed
|
|
985
|
+
* @returns {boolean}
|
|
986
|
+
*/
|
|
987
|
+
function isDashHRLine(trimmed) {
|
|
988
|
+
if (trimmed.length < 3) return false;
|
|
989
|
+
for (let i = 0; i < trimmed.length; i++) {
|
|
990
|
+
const ch = trimmed[i];
|
|
991
|
+
if (ch === '-') continue;
|
|
992
|
+
// Allow trailing whitespace only
|
|
993
|
+
if (ch === ' ' || ch === '\t') {
|
|
994
|
+
for (let j = i + 1; j < trimmed.length; j++) {
|
|
995
|
+
if (trimmed[j] !== ' ' && trimmed[j] !== '\t') return false;
|
|
996
|
+
}
|
|
997
|
+
return i >= 3; // at least 3 dashes before whitespace
|
|
998
|
+
}
|
|
999
|
+
return false;
|
|
1000
|
+
}
|
|
1001
|
+
return true; // all dashes
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
/**
|
|
1005
|
+
* quikdown — A compact, scanner-based markdown parser
|
|
1006
|
+
* ════════════════════════════════════════════════════
|
|
1007
|
+
*
|
|
1008
|
+
* Architecture overview (v1.2.8 — lexer rewrite)
|
|
1009
|
+
* ───────────────────────────────────────────────
|
|
1010
|
+
* Prior to v1.2.8, quikdown used a multi-pass regex pipeline: each block
|
|
1011
|
+
* type (headings, blockquotes, HR, lists, tables) and each inline format
|
|
1012
|
+
* (bold, italic, links, …) was handled by its own global regex applied
|
|
1013
|
+
* sequentially to the full document string. That worked but made the code
|
|
1014
|
+
* hard to extend and debug — a new construct meant adding another regex
|
|
1015
|
+
* pass, and ordering bugs between passes were subtle.
|
|
1016
|
+
*
|
|
1017
|
+
* Starting in v1.2.8 the parser uses a **line-scanning** approach for
|
|
1018
|
+
* block detection and a **per-block inline pass** for formatting:
|
|
1019
|
+
*
|
|
1020
|
+
* ┌─────────────────────────────────────────────────────────┐
|
|
1021
|
+
* │ Phase 1 — Code Extraction │
|
|
1022
|
+
* │ Scan for fenced code blocks (``` / ~~~) and inline │
|
|
1023
|
+
* │ code spans (`…`). Replace with §CB§ / §IC§ place- │
|
|
1024
|
+
* │ holders so code content is never touched by later │
|
|
1025
|
+
* │ phases. │
|
|
1026
|
+
* ├─────────────────────────────────────────────────────────┤
|
|
1027
|
+
* │ Phase 2 — HTML Escaping │
|
|
1028
|
+
* │ Escape &, <, >, ", ' in the remaining text to prevent │
|
|
1029
|
+
* │ XSS. (Skipped when allow_unsafe_html is true.) │
|
|
1030
|
+
* ├─────────────────────────────────────────────────────────┤
|
|
1031
|
+
* │ Phase 3 — Block Scanning │
|
|
1032
|
+
* │ Walk the text **line by line**. At each line, the │
|
|
1033
|
+
* │ scanner checks (in order): │
|
|
1034
|
+
* │ • table rows (|) │
|
|
1035
|
+
* │ • headings (#) │
|
|
1036
|
+
* │ • HR (---) │
|
|
1037
|
+
* │ • blockquotes (>) │
|
|
1038
|
+
* │ • list items (-, *, +, 1.) │
|
|
1039
|
+
* │ • code-block placeholder (§CB…§) │
|
|
1040
|
+
* │ • paragraph text (everything else) │
|
|
1041
|
+
* │ │
|
|
1042
|
+
* │ Block text is run through the **inline formatter** │
|
|
1043
|
+
* │ which handles bold, italic, strikethrough, links, │
|
|
1044
|
+
* │ images, and autolinks. │
|
|
1045
|
+
* │ │
|
|
1046
|
+
* │ Paragraphs are wrapped in <p> tags. Lazy linefeeds │
|
|
1047
|
+
* │ (single \n → <br>) are handled here too. │
|
|
1048
|
+
* ├─────────────────────────────────────────────────────────┤
|
|
1049
|
+
* │ Phase 4 — Code Restoration │
|
|
1050
|
+
* │ Replace §CB§ / §IC§ placeholders with rendered <pre> │
|
|
1051
|
+
* │ / <code> HTML, applying the fence_plugin if present. │
|
|
1052
|
+
* └─────────────────────────────────────────────────────────┘
|
|
1053
|
+
*
|
|
1054
|
+
* Why this design?
|
|
1055
|
+
* • Single pass over lines for block identification — no re-scanning.
|
|
1056
|
+
* • Each block type is a clearly separated branch, easy to add new ones.
|
|
1057
|
+
* • Inline formatting is confined to block text — can't accidentally
|
|
1058
|
+
* match across block boundaries or inside HTML tags.
|
|
1059
|
+
* • Code extraction still uses a simple regex (it's one pattern, not a
|
|
1060
|
+
* chain) because the §-placeholder approach is proven and simple.
|
|
1061
|
+
*
|
|
1062
|
+
* @param {string} markdown The markdown source text
|
|
1063
|
+
* @param {Object} options Configuration (see below)
|
|
1064
|
+
* @returns {string} Rendered HTML
|
|
1065
|
+
*/
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
// ────────────────────────────────────────────────────────────────────
|
|
1069
|
+
// Constants
|
|
1070
|
+
// ────────────────────────────────────────────────────────────────────
|
|
1071
|
+
|
|
1072
|
+
/** Build-time version stamp (injected by tools/updateVersion) */
|
|
1073
|
+
const quikdownVersion = '1.2.10';
|
|
1074
|
+
|
|
1075
|
+
/** CSS class prefix used for all generated elements */
|
|
982
1076
|
const CLASS_PREFIX = 'quikdown-';
|
|
983
|
-
const PLACEHOLDER_CB = '§CB';
|
|
984
|
-
const PLACEHOLDER_IC = '§IC';
|
|
985
1077
|
|
|
986
|
-
|
|
1078
|
+
/** Placeholder sigils — chosen to be extremely unlikely in real text */
|
|
1079
|
+
const PLACEHOLDER_CB = '§CB'; // fenced code blocks
|
|
1080
|
+
const PLACEHOLDER_IC = '§IC'; // inline code spans
|
|
1081
|
+
|
|
1082
|
+
/** HTML entity escape map */
|
|
987
1083
|
const ESC_MAP = {'&':'&','<':'<','>':'>','"':'"',"'":'''};
|
|
988
1084
|
|
|
989
|
-
//
|
|
1085
|
+
// ────────────────────────────────────────────────────────────────────
|
|
1086
|
+
// Style definitions
|
|
1087
|
+
// ────────────────────────────────────────────────────────────────────
|
|
1088
|
+
|
|
1089
|
+
/**
|
|
1090
|
+
* Inline styles for every element quikdown can emit.
|
|
1091
|
+
* When `inline_styles: true` these are injected as style="…" attributes.
|
|
1092
|
+
* When `inline_styles: false` (default) we use class="quikdown-<tag>"
|
|
1093
|
+
* and these same values are emitted by `quikdown.emitStyles()`.
|
|
1094
|
+
*/
|
|
990
1095
|
const QUIKDOWN_STYLES = {
|
|
991
1096
|
h1: 'font-size:2em;font-weight:600;margin:.67em 0;text-align:left',
|
|
992
1097
|
h2: 'font-size:1.5em;font-weight:600;margin:.83em 0',
|
|
@@ -1009,30 +1114,41 @@ const QUIKDOWN_STYLES = {
|
|
|
1009
1114
|
ul: 'margin:.5em 0;padding-left:2em',
|
|
1010
1115
|
ol: 'margin:.5em 0;padding-left:2em',
|
|
1011
1116
|
li: 'margin:.25em 0',
|
|
1012
|
-
// Task list specific styles
|
|
1013
1117
|
'task-item': 'list-style:none',
|
|
1014
1118
|
'task-checkbox': 'margin-right:.5em'
|
|
1015
1119
|
};
|
|
1016
1120
|
|
|
1017
|
-
//
|
|
1121
|
+
// ────────────────────────────────────────────────────────────────────
|
|
1122
|
+
// Attribute factory
|
|
1123
|
+
// ────────────────────────────────────────────────────────────────────
|
|
1124
|
+
|
|
1125
|
+
/**
|
|
1126
|
+
* Creates a `getAttr(tag, additionalStyle?)` helper that returns
|
|
1127
|
+
* either a class="…" or style="…" attribute string depending on mode.
|
|
1128
|
+
*
|
|
1129
|
+
* @param {boolean} inline_styles True → emit style="…"; false → class="…"
|
|
1130
|
+
* @param {Object} styles The QUIKDOWN_STYLES map
|
|
1131
|
+
* @returns {Function}
|
|
1132
|
+
*/
|
|
1018
1133
|
function createGetAttr(inline_styles, styles) {
|
|
1019
1134
|
return function(tag, additionalStyle = '') {
|
|
1020
1135
|
if (inline_styles) {
|
|
1021
1136
|
let style = styles[tag];
|
|
1022
1137
|
if (!style && !additionalStyle) return '';
|
|
1023
|
-
|
|
1024
|
-
//
|
|
1138
|
+
|
|
1139
|
+
// When adding alignment that conflicts with the tag's default,
|
|
1140
|
+
// strip the default text-align first.
|
|
1025
1141
|
if (additionalStyle && additionalStyle.includes('text-align') && style && style.includes('text-align')) {
|
|
1026
1142
|
style = style.replace(/text-align:[^;]+;?/, '').trim();
|
|
1143
|
+
/* istanbul ignore next */
|
|
1027
1144
|
if (style && !style.endsWith(';')) style += ';';
|
|
1028
1145
|
}
|
|
1029
|
-
|
|
1146
|
+
|
|
1030
1147
|
/* istanbul ignore next - defensive: additionalStyle without style doesn't occur with current tags */
|
|
1031
1148
|
const fullStyle = additionalStyle ? (style ? `${style}${additionalStyle}` : additionalStyle) : style;
|
|
1032
1149
|
return ` style="${fullStyle}"`;
|
|
1033
1150
|
} else {
|
|
1034
1151
|
const classAttr = ` class="${CLASS_PREFIX}${tag}"`;
|
|
1035
|
-
// Apply inline styles for alignment even when using CSS classes
|
|
1036
1152
|
if (additionalStyle) {
|
|
1037
1153
|
return `${classAttr} style="${additionalStyle}"`;
|
|
1038
1154
|
}
|
|
@@ -1041,69 +1157,84 @@ function createGetAttr(inline_styles, styles) {
|
|
|
1041
1157
|
};
|
|
1042
1158
|
}
|
|
1043
1159
|
|
|
1160
|
+
// ════════════════════════════════════════════════════════════════════
|
|
1161
|
+
// Main parser function
|
|
1162
|
+
// ════════════════════════════════════════════════════════════════════
|
|
1163
|
+
|
|
1044
1164
|
function quikdown(markdown, options = {}) {
|
|
1165
|
+
// ── Guard: only process non-empty strings ──
|
|
1045
1166
|
if (!markdown || typeof markdown !== 'string') {
|
|
1046
1167
|
return '';
|
|
1047
1168
|
}
|
|
1048
|
-
|
|
1049
|
-
const { fence_plugin, inline_styles = false, bidirectional = false, lazy_linefeeds = false } = options;
|
|
1050
|
-
const styles = QUIKDOWN_STYLES; // Use module-level styles
|
|
1051
|
-
const getAttr = createGetAttr(inline_styles, styles); // Create getAttr once
|
|
1052
1169
|
|
|
1053
|
-
//
|
|
1170
|
+
// ── Unpack options ──
|
|
1171
|
+
const { fence_plugin, inline_styles = false, bidirectional = false, lazy_linefeeds = false, allow_unsafe_html = false } = options;
|
|
1172
|
+
const styles = QUIKDOWN_STYLES;
|
|
1173
|
+
const getAttr = createGetAttr(inline_styles, styles);
|
|
1174
|
+
|
|
1175
|
+
// ── Helpers (closed over options) ──
|
|
1176
|
+
|
|
1177
|
+
/** Escape the five HTML-special characters. */
|
|
1054
1178
|
function escapeHtml(text) {
|
|
1055
1179
|
return text.replace(/[&<>"']/g, m => ESC_MAP[m]);
|
|
1056
1180
|
}
|
|
1057
|
-
|
|
1058
|
-
|
|
1181
|
+
|
|
1182
|
+
/**
|
|
1183
|
+
* Bidirectional marker helper.
|
|
1184
|
+
* When bidirectional mode is on, returns ` data-qd="…"`.
|
|
1185
|
+
* The non-bidirectional branch is a trivial no-op arrow; it is
|
|
1186
|
+
* exercised in the core bundle but never in quikdown_bd.
|
|
1187
|
+
*/
|
|
1188
|
+
/* istanbul ignore next - trivial no-op fallback */
|
|
1059
1189
|
const dataQd = bidirectional ? (marker) => ` data-qd="${escapeHtml(marker)}"` : () => '';
|
|
1060
|
-
|
|
1061
|
-
|
|
1190
|
+
|
|
1191
|
+
/**
|
|
1192
|
+
* Sanitize a URL to block javascript:, vbscript:, and non-image data: URIs.
|
|
1193
|
+
* Returns '#' for blocked URLs.
|
|
1194
|
+
*/
|
|
1062
1195
|
function sanitizeUrl(url, allowUnsafe = false) {
|
|
1063
1196
|
/* istanbul ignore next - defensive programming, regex ensures url is never empty */
|
|
1064
1197
|
if (!url) return '';
|
|
1065
|
-
|
|
1066
|
-
// If unsafe URLs are explicitly allowed, return as-is
|
|
1067
1198
|
if (allowUnsafe) return url;
|
|
1068
|
-
|
|
1199
|
+
|
|
1069
1200
|
const trimmedUrl = url.trim();
|
|
1070
1201
|
const lowerUrl = trimmedUrl.toLowerCase();
|
|
1071
|
-
|
|
1072
|
-
// Block dangerous protocols
|
|
1073
1202
|
const dangerousProtocols = ['javascript:', 'vbscript:', 'data:'];
|
|
1074
|
-
|
|
1203
|
+
|
|
1075
1204
|
for (const protocol of dangerousProtocols) {
|
|
1076
1205
|
if (lowerUrl.startsWith(protocol)) {
|
|
1077
|
-
// Exception: Allow data:image/* for images
|
|
1078
1206
|
if (protocol === 'data:' && lowerUrl.startsWith('data:image/')) {
|
|
1079
1207
|
return trimmedUrl;
|
|
1080
1208
|
}
|
|
1081
|
-
// Return safe empty link for dangerous protocols
|
|
1082
1209
|
return '#';
|
|
1083
1210
|
}
|
|
1084
1211
|
}
|
|
1085
|
-
|
|
1086
1212
|
return trimmedUrl;
|
|
1087
1213
|
}
|
|
1088
1214
|
|
|
1089
|
-
//
|
|
1215
|
+
// ────────────────────────────────────────────────────────────────
|
|
1216
|
+
// Phase 1 — Code Extraction
|
|
1217
|
+
// ────────────────────────────────────────────────────────────────
|
|
1218
|
+
// Why extract code first? Fenced blocks and inline code spans can
|
|
1219
|
+
// contain markdown-like characters (*, _, #, |, etc.) that must NOT
|
|
1220
|
+
// be interpreted as formatting. By pulling them out and replacing
|
|
1221
|
+
// with unique placeholders, the rest of the pipeline never sees them.
|
|
1222
|
+
|
|
1090
1223
|
let html = markdown;
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
//
|
|
1097
|
-
//
|
|
1098
|
-
// Fence must be at start of line
|
|
1224
|
+
const codeBlocks = []; // Array of {lang, code, custom, fence, hasReverse}
|
|
1225
|
+
const inlineCodes = []; // Array of escaped-HTML strings
|
|
1226
|
+
|
|
1227
|
+
// ── Fenced code blocks ──
|
|
1228
|
+
// Matches paired fences: ``` with ``` and ~~~ with ~~~.
|
|
1229
|
+
// The fence must start at column 0 of a line (^ with /m flag).
|
|
1230
|
+
// Group 1 = fence marker, Group 2 = language hint, Group 3 = code body.
|
|
1099
1231
|
html = html.replace(/^(```|~~~)([^\n]*)\n([\s\S]*?)^\1$/gm, (match, fence, lang, code) => {
|
|
1100
1232
|
const placeholder = `${PLACEHOLDER_CB}${codeBlocks.length}§`;
|
|
1101
|
-
|
|
1102
|
-
// Trim the language specification
|
|
1103
1233
|
const langTrimmed = lang ? lang.trim() : '';
|
|
1104
|
-
|
|
1105
|
-
// If custom fence plugin is provided, use it (v1.1.0: object format required)
|
|
1234
|
+
|
|
1106
1235
|
if (fence_plugin && fence_plugin.render && typeof fence_plugin.render === 'function') {
|
|
1236
|
+
// Custom plugin — store raw code (un-escaped) so the plugin
|
|
1237
|
+
// receives the original source.
|
|
1107
1238
|
codeBlocks.push({
|
|
1108
1239
|
lang: langTrimmed,
|
|
1109
1240
|
code: code.trimEnd(),
|
|
@@ -1112,6 +1243,7 @@ function quikdown(markdown, options = {}) {
|
|
|
1112
1243
|
hasReverse: !!fence_plugin.reverse
|
|
1113
1244
|
});
|
|
1114
1245
|
} else {
|
|
1246
|
+
// Default — pre-escape the code for safe HTML output.
|
|
1115
1247
|
codeBlocks.push({
|
|
1116
1248
|
lang: langTrimmed,
|
|
1117
1249
|
code: escapeHtml(code.trimEnd()),
|
|
@@ -1121,66 +1253,97 @@ function quikdown(markdown, options = {}) {
|
|
|
1121
1253
|
}
|
|
1122
1254
|
return placeholder;
|
|
1123
1255
|
});
|
|
1124
|
-
|
|
1125
|
-
//
|
|
1256
|
+
|
|
1257
|
+
// ── Inline code spans ──
|
|
1258
|
+
// Matches a single backtick pair: `content`.
|
|
1259
|
+
// Content is captured and HTML-escaped immediately.
|
|
1126
1260
|
html = html.replace(/`([^`]+)`/g, (match, code) => {
|
|
1127
1261
|
const placeholder = `${PLACEHOLDER_IC}${inlineCodes.length}§`;
|
|
1128
1262
|
inlineCodes.push(escapeHtml(code));
|
|
1129
1263
|
return placeholder;
|
|
1130
1264
|
});
|
|
1131
|
-
|
|
1132
|
-
//
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
//
|
|
1136
|
-
|
|
1137
|
-
//
|
|
1265
|
+
|
|
1266
|
+
// ────────────────────────────────────────────────────────────────
|
|
1267
|
+
// Phase 2 — HTML Escaping
|
|
1268
|
+
// ────────────────────────────────────────────────────────────────
|
|
1269
|
+
// All remaining text (everything except code placeholders) is escaped
|
|
1270
|
+
// to prevent XSS. The `allow_unsafe_html` option skips this for
|
|
1271
|
+
// trusted pipelines that intentionally embed raw HTML.
|
|
1272
|
+
|
|
1273
|
+
if (!allow_unsafe_html) {
|
|
1274
|
+
html = escapeHtml(html);
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
// ────────────────────────────────────────────────────────────────
|
|
1278
|
+
// Phase 3 — Block Scanning + Inline Formatting + Paragraphs
|
|
1279
|
+
// ────────────────────────────────────────────────────────────────
|
|
1280
|
+
// This is the heart of the lexer rewrite. Instead of applying
|
|
1281
|
+
// 10+ global regex passes, we:
|
|
1282
|
+
// 1. Process tables (line walker — tables need multi-line lookahead)
|
|
1283
|
+
// 2. Scan remaining lines for headings, HR, blockquotes
|
|
1284
|
+
// 3. Process lists (line walker — lists need indent tracking)
|
|
1285
|
+
// 4. Apply inline formatting to all text content
|
|
1286
|
+
// 5. Wrap remaining text in <p> tags
|
|
1287
|
+
//
|
|
1288
|
+
// Steps 1 and 3 are line-walkers that process the full text in a
|
|
1289
|
+
// single pass each. Step 2 replaces global regex with a per-line
|
|
1290
|
+
// scanner. Steps 4-5 are applied to the result.
|
|
1291
|
+
//
|
|
1292
|
+
// Total: 3 structured passes instead of 10+ regex passes.
|
|
1293
|
+
|
|
1294
|
+
// ── Step 1: Tables ──
|
|
1295
|
+
// Tables need multi-line lookahead (header → separator → body rows)
|
|
1296
|
+
// so they're handled by a dedicated line-walker first.
|
|
1138
1297
|
html = processTable(html, getAttr);
|
|
1139
|
-
|
|
1140
|
-
//
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
//
|
|
1147
|
-
|
|
1148
|
-
// Merge consecutive blockquotes
|
|
1149
|
-
html = html.replace(/<\/blockquote>\n<blockquote>/g, '\n');
|
|
1150
|
-
|
|
1151
|
-
// Process horizontal rules (allow trailing spaces)
|
|
1152
|
-
html = html.replace(/^---+\s*$/gm, `<hr${getAttr('hr')}>`);
|
|
1153
|
-
|
|
1154
|
-
// Process lists
|
|
1298
|
+
|
|
1299
|
+
// ── Step 2: Headings, HR, Blockquotes ──
|
|
1300
|
+
// These are simple line-level constructs. We scan each line once
|
|
1301
|
+
// and replace matching lines with their HTML representation.
|
|
1302
|
+
html = scanLineBlocks(html, getAttr, dataQd);
|
|
1303
|
+
|
|
1304
|
+
// ── Step 3: Lists ──
|
|
1305
|
+
// Lists need indent-level tracking across lines, so they get their
|
|
1306
|
+
// own line-walker.
|
|
1155
1307
|
html = processLists(html, getAttr, inline_styles, bidirectional);
|
|
1156
|
-
|
|
1157
|
-
//
|
|
1158
|
-
|
|
1159
|
-
//
|
|
1308
|
+
|
|
1309
|
+
// ── Step 4: Inline formatting ──
|
|
1310
|
+
// Apply bold, italic, strikethrough, images, links, and autolinks
|
|
1311
|
+
// to all text content. This runs on the output of steps 1-3, so
|
|
1312
|
+
// it sees text inside headings, blockquotes, table cells, list
|
|
1313
|
+
// items, and paragraph text.
|
|
1314
|
+
|
|
1315
|
+
// Images (must come before links —  vs [text](url))
|
|
1160
1316
|
html = html.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (match, alt, src) => {
|
|
1161
1317
|
const sanitizedSrc = sanitizeUrl(src, options.allow_unsafe_urls);
|
|
1318
|
+
/* istanbul ignore next - bd-only branch */
|
|
1162
1319
|
const altAttr = bidirectional && alt ? ` data-qd-alt="${escapeHtml(alt)}"` : '';
|
|
1320
|
+
/* istanbul ignore next - bd-only branch */
|
|
1163
1321
|
const srcAttr = bidirectional ? ` data-qd-src="${escapeHtml(src)}"` : '';
|
|
1164
1322
|
return `<img${getAttr('img')} src="${sanitizedSrc}" alt="${alt}"${altAttr}${srcAttr}${dataQd('!')}>`;
|
|
1165
1323
|
});
|
|
1166
|
-
|
|
1167
|
-
// Links
|
|
1324
|
+
|
|
1325
|
+
// Links
|
|
1168
1326
|
html = html.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (match, text, href) => {
|
|
1169
|
-
// Sanitize URL to prevent XSS
|
|
1170
1327
|
const sanitizedHref = sanitizeUrl(href, options.allow_unsafe_urls);
|
|
1171
1328
|
const isExternal = /^https?:\/\//i.test(sanitizedHref);
|
|
1172
1329
|
const rel = isExternal ? ' rel="noopener noreferrer"' : '';
|
|
1330
|
+
/* istanbul ignore next - bd-only branch */
|
|
1173
1331
|
const textAttr = bidirectional ? ` data-qd-text="${escapeHtml(text)}"` : '';
|
|
1174
1332
|
return `<a${getAttr('a')} href="${sanitizedHref}"${rel}${textAttr}${dataQd('[')}>${text}</a>`;
|
|
1175
1333
|
});
|
|
1176
|
-
|
|
1177
|
-
// Autolinks
|
|
1334
|
+
|
|
1335
|
+
// Autolinks — bare https?:// URLs become clickable <a> tags
|
|
1178
1336
|
html = html.replace(/(^|\s)(https?:\/\/[^\s<]+)/g, (match, prefix, url) => {
|
|
1179
1337
|
const sanitizedUrl = sanitizeUrl(url, options.allow_unsafe_urls);
|
|
1180
1338
|
return `${prefix}<a${getAttr('a')} href="${sanitizedUrl}" rel="noopener noreferrer">${url}</a>`;
|
|
1181
1339
|
});
|
|
1182
|
-
|
|
1183
|
-
//
|
|
1340
|
+
|
|
1341
|
+
// Protect rendered tags so emphasis regexes don't see attribute
|
|
1342
|
+
// values — fixes #3 (underscores in URLs interpreted as emphasis).
|
|
1343
|
+
const savedTags = [];
|
|
1344
|
+
html = html.replace(/<[^>]+>/g, m => { savedTags.push(m); return `%%T${savedTags.length - 1}%%`; });
|
|
1345
|
+
|
|
1346
|
+
// Bold, italic, strikethrough
|
|
1184
1347
|
const inlinePatterns = [
|
|
1185
1348
|
[/\*\*(.+?)\*\*/g, 'strong', '**'],
|
|
1186
1349
|
[/__(.+?)__/g, 'strong', '__'],
|
|
@@ -1188,60 +1351,66 @@ function quikdown(markdown, options = {}) {
|
|
|
1188
1351
|
[/(?<!_)_(?!_)(.+?)(?<!_)_(?!_)/g, 'em', '_'],
|
|
1189
1352
|
[/~~(.+?)~~/g, 'del', '~~']
|
|
1190
1353
|
];
|
|
1191
|
-
|
|
1192
1354
|
inlinePatterns.forEach(([pattern, tag, marker]) => {
|
|
1193
1355
|
html = html.replace(pattern, `<${tag}${getAttr(tag)}${dataQd(marker)}>$1</${tag}>`);
|
|
1194
1356
|
});
|
|
1195
|
-
|
|
1196
|
-
//
|
|
1357
|
+
|
|
1358
|
+
// Restore protected tags
|
|
1359
|
+
html = html.replace(/%%T(\d+)%%/g, (_, i) => savedTags[i]);
|
|
1360
|
+
|
|
1361
|
+
// ── Step 5: Line breaks + paragraph wrapping ──
|
|
1197
1362
|
if (lazy_linefeeds) {
|
|
1198
|
-
// Lazy linefeeds: single
|
|
1363
|
+
// Lazy linefeeds mode: every single \n becomes <br> EXCEPT:
|
|
1364
|
+
// • Double newlines → paragraph break
|
|
1365
|
+
// • Newlines adjacent to block elements (h, blockquote, pre, hr, table, list)
|
|
1366
|
+
//
|
|
1367
|
+
// Strategy: protect block-adjacent newlines with §N§, convert
|
|
1368
|
+
// the rest, then restore.
|
|
1369
|
+
|
|
1199
1370
|
const blocks = [];
|
|
1200
1371
|
let bi = 0;
|
|
1201
|
-
|
|
1202
|
-
// Protect tables and lists
|
|
1372
|
+
|
|
1373
|
+
// Protect tables and lists from <br> injection
|
|
1203
1374
|
html = html.replace(/<(table|[uo]l)[^>]*>[\s\S]*?<\/\1>/g, m => {
|
|
1204
1375
|
blocks[bi] = m;
|
|
1205
1376
|
return `§B${bi++}§`;
|
|
1206
1377
|
});
|
|
1207
|
-
|
|
1208
|
-
// Handle paragraphs and block elements
|
|
1378
|
+
|
|
1209
1379
|
html = html.replace(/\n\n+/g, '§P§')
|
|
1210
|
-
// After block
|
|
1380
|
+
// After block-level closing tags
|
|
1211
1381
|
.replace(/(<\/(?:h[1-6]|blockquote|pre)>)\n/g, '$1§N§')
|
|
1212
1382
|
.replace(/(<(?:h[1-6]|blockquote|pre|hr)[^>]*>)\n/g, '$1§N§')
|
|
1213
|
-
// Before block
|
|
1383
|
+
// Before block-level opening tags
|
|
1214
1384
|
.replace(/\n(<(?:h[1-6]|blockquote|pre|hr)[^>]*>)/g, '§N§$1')
|
|
1215
1385
|
.replace(/\n(§B\d+§)/g, '§N§$1')
|
|
1216
1386
|
.replace(/(§B\d+§)\n/g, '$1§N§')
|
|
1217
|
-
// Convert
|
|
1387
|
+
// Convert surviving newlines to <br>
|
|
1218
1388
|
.replace(/\n/g, `<br${getAttr('br')}>`)
|
|
1219
1389
|
// Restore
|
|
1220
1390
|
.replace(/§N§/g, '\n')
|
|
1221
1391
|
.replace(/§P§/g, '</p><p>');
|
|
1222
|
-
|
|
1392
|
+
|
|
1223
1393
|
// Restore protected blocks
|
|
1224
1394
|
blocks.forEach((b, i) => html = html.replace(`§B${i}§`, b));
|
|
1225
|
-
|
|
1395
|
+
|
|
1226
1396
|
html = '<p>' + html + '</p>';
|
|
1227
1397
|
} else {
|
|
1228
|
-
// Standard: two spaces
|
|
1229
|
-
html = html.replace(/
|
|
1230
|
-
|
|
1231
|
-
// Paragraphs (double newlines)
|
|
1232
|
-
// Don't add </p> after block elements (they're not in paragraphs)
|
|
1398
|
+
// Standard mode: two trailing spaces → <br>, double newline → new paragraph
|
|
1399
|
+
html = html.replace(/ {2}$/gm, `<br${getAttr('br')}>`);
|
|
1400
|
+
|
|
1233
1401
|
html = html.replace(/\n\n+/g, (match, offset) => {
|
|
1234
|
-
// Check if we're after a block element closing tag
|
|
1235
1402
|
const before = html.substring(0, offset);
|
|
1236
1403
|
if (before.match(/<\/(h[1-6]|blockquote|ul|ol|table|pre|hr)>$/)) {
|
|
1237
|
-
return '<p>';
|
|
1404
|
+
return '<p>';
|
|
1238
1405
|
}
|
|
1239
|
-
return '</p><p>';
|
|
1406
|
+
return '</p><p>';
|
|
1240
1407
|
});
|
|
1241
1408
|
html = '<p>' + html + '</p>';
|
|
1242
1409
|
}
|
|
1243
|
-
|
|
1244
|
-
//
|
|
1410
|
+
|
|
1411
|
+
// ── Step 6: Cleanup ──
|
|
1412
|
+
// Remove <p> wrappers that accidentally enclose block elements.
|
|
1413
|
+
// This is simpler than trying to prevent them during wrapping.
|
|
1245
1414
|
const cleanupPatterns = [
|
|
1246
1415
|
[/<p><\/p>/g, ''],
|
|
1247
1416
|
[/<p>(<h[1-6][^>]*>)/g, '$1'],
|
|
@@ -1255,67 +1424,154 @@ function quikdown(markdown, options = {}) {
|
|
|
1255
1424
|
[/(<\/table>)<\/p>/g, '$1'],
|
|
1256
1425
|
[/<p>(<pre[^>]*>)/g, '$1'],
|
|
1257
1426
|
[/(<\/pre>)<\/p>/g, '$1'],
|
|
1258
|
-
[new RegExp(`<p>(${PLACEHOLDER_CB}\\d+§)
|
|
1427
|
+
[new RegExp(`<p>(${PLACEHOLDER_CB}\\d+§)</p>`, 'g'), '$1']
|
|
1259
1428
|
];
|
|
1260
|
-
|
|
1261
1429
|
cleanupPatterns.forEach(([pattern, replacement]) => {
|
|
1262
1430
|
html = html.replace(pattern, replacement);
|
|
1263
1431
|
});
|
|
1264
|
-
|
|
1265
|
-
//
|
|
1266
|
-
// When a paragraph follows a block element, ensure it has opening <p>
|
|
1432
|
+
|
|
1433
|
+
// When a block element is followed by a newline and then text, open a <p>.
|
|
1267
1434
|
html = html.replace(/(<\/(?:h[1-6]|blockquote|ul|ol|table|pre|hr)>)\n([^<])/g, '$1\n<p>$2');
|
|
1268
|
-
|
|
1269
|
-
//
|
|
1270
|
-
|
|
1271
|
-
//
|
|
1435
|
+
|
|
1436
|
+
// ────────────────────────────────────────────────────────────────
|
|
1437
|
+
// Phase 4 — Code Restoration
|
|
1438
|
+
// ────────────────────────────────────────────────────────────────
|
|
1439
|
+
// Replace placeholders with rendered HTML. For fenced blocks this
|
|
1440
|
+
// means wrapping in <pre><code>…</code></pre> (or calling the
|
|
1441
|
+
// fence_plugin). For inline code it means <code>…</code>.
|
|
1442
|
+
|
|
1272
1443
|
codeBlocks.forEach((block, i) => {
|
|
1273
1444
|
let replacement;
|
|
1274
|
-
|
|
1445
|
+
|
|
1275
1446
|
if (block.custom && fence_plugin && fence_plugin.render) {
|
|
1276
|
-
//
|
|
1447
|
+
// Delegate to the user-provided fence plugin.
|
|
1277
1448
|
replacement = fence_plugin.render(block.code, block.lang);
|
|
1278
|
-
|
|
1279
|
-
// If plugin returns undefined, fall back to default rendering
|
|
1449
|
+
|
|
1280
1450
|
if (replacement === undefined) {
|
|
1451
|
+
// Plugin declined — fall back to default rendering.
|
|
1281
1452
|
const langClass = !inline_styles && block.lang ? ` class="language-${block.lang}"` : '';
|
|
1282
1453
|
const codeAttr = inline_styles ? getAttr('code') : langClass;
|
|
1454
|
+
/* istanbul ignore next - bd-only branch */
|
|
1283
1455
|
const langAttr = bidirectional && block.lang ? ` data-qd-lang="${escapeHtml(block.lang)}"` : '';
|
|
1456
|
+
/* istanbul ignore next - bd-only branch */
|
|
1284
1457
|
const fenceAttr = bidirectional ? ` data-qd-fence="${escapeHtml(block.fence)}"` : '';
|
|
1285
1458
|
replacement = `<pre${getAttr('pre')}${fenceAttr}${langAttr}><code${codeAttr}>${escapeHtml(block.code)}</code></pre>`;
|
|
1286
|
-
} else if (bidirectional) {
|
|
1287
|
-
//
|
|
1288
|
-
replacement = replacement.replace(/^<(\w+)/,
|
|
1459
|
+
} else /* istanbul ignore next - bd-only branch */ if (bidirectional) {
|
|
1460
|
+
// Plugin returned HTML — inject data attributes for roundtrip.
|
|
1461
|
+
replacement = replacement.replace(/^<(\w+)/,
|
|
1289
1462
|
`<$1 data-qd-fence="${escapeHtml(block.fence)}" data-qd-lang="${escapeHtml(block.lang)}" data-qd-source="${escapeHtml(block.code)}"`);
|
|
1290
1463
|
}
|
|
1291
1464
|
} else {
|
|
1292
|
-
// Default rendering
|
|
1465
|
+
// Default rendering — wrap in <pre><code>.
|
|
1293
1466
|
const langClass = !inline_styles && block.lang ? ` class="language-${block.lang}"` : '';
|
|
1294
1467
|
const codeAttr = inline_styles ? getAttr('code') : langClass;
|
|
1468
|
+
/* istanbul ignore next - bd-only branch */
|
|
1295
1469
|
const langAttr = bidirectional && block.lang ? ` data-qd-lang="${escapeHtml(block.lang)}"` : '';
|
|
1470
|
+
/* istanbul ignore next - bd-only branch */
|
|
1296
1471
|
const fenceAttr = bidirectional ? ` data-qd-fence="${escapeHtml(block.fence)}"` : '';
|
|
1297
1472
|
replacement = `<pre${getAttr('pre')}${fenceAttr}${langAttr}><code${codeAttr}>${block.code}</code></pre>`;
|
|
1298
1473
|
}
|
|
1299
|
-
|
|
1474
|
+
|
|
1300
1475
|
const placeholder = `${PLACEHOLDER_CB}${i}§`;
|
|
1301
1476
|
html = html.replace(placeholder, replacement);
|
|
1302
1477
|
});
|
|
1303
|
-
|
|
1304
|
-
// Restore inline code
|
|
1478
|
+
|
|
1479
|
+
// Restore inline code spans
|
|
1305
1480
|
inlineCodes.forEach((code, i) => {
|
|
1306
1481
|
const placeholder = `${PLACEHOLDER_IC}${i}§`;
|
|
1307
1482
|
html = html.replace(placeholder, `<code${getAttr('code')}${dataQd('`')}>${code}</code>`);
|
|
1308
1483
|
});
|
|
1309
|
-
|
|
1484
|
+
|
|
1310
1485
|
return html.trim();
|
|
1311
1486
|
}
|
|
1312
1487
|
|
|
1488
|
+
// ════════════════════════════════════════════════════════════════════
|
|
1489
|
+
// Block-level line scanner
|
|
1490
|
+
// ════════════════════════════════════════════════════════════════════
|
|
1491
|
+
|
|
1313
1492
|
/**
|
|
1314
|
-
*
|
|
1493
|
+
* scanLineBlocks — single-pass line scanner for headings, HR, blockquotes
|
|
1494
|
+
*
|
|
1495
|
+
* Walks the text line by line. For each line it checks (in order):
|
|
1496
|
+
* 1. Heading — starts with 1-6 '#' followed by a space
|
|
1497
|
+
* 2. HR — line is entirely '---…' (3+ dashes, optional trailing space)
|
|
1498
|
+
* 3. Blockquote — starts with '> ' (the > was already HTML-escaped)
|
|
1499
|
+
*
|
|
1500
|
+
* Lines that don't match any block pattern are passed through unchanged.
|
|
1501
|
+
*
|
|
1502
|
+
* This replaces three separate global regex passes from the pre-1.2.8
|
|
1503
|
+
* architecture with one structured scan.
|
|
1504
|
+
*
|
|
1505
|
+
* @param {string} text The document text (HTML-escaped, code extracted)
|
|
1506
|
+
* @param {Function} getAttr Attribute factory (class or style)
|
|
1507
|
+
* @param {Function} dataQd Bidirectional marker factory
|
|
1508
|
+
* @returns {string} Text with block-level elements rendered
|
|
1509
|
+
*/
|
|
1510
|
+
function scanLineBlocks(text, getAttr, dataQd) {
|
|
1511
|
+
const lines = text.split('\n');
|
|
1512
|
+
const result = [];
|
|
1513
|
+
let i = 0;
|
|
1514
|
+
|
|
1515
|
+
while (i < lines.length) {
|
|
1516
|
+
const line = lines[i];
|
|
1517
|
+
|
|
1518
|
+
// ── Heading ──
|
|
1519
|
+
// Count leading '#' characters. Valid heading: 1-6 hashes then a space.
|
|
1520
|
+
// Example: "## Hello World ##" → <h2>Hello World</h2>
|
|
1521
|
+
let hashCount = 0;
|
|
1522
|
+
while (hashCount < line.length && hashCount < 7 && line[hashCount] === '#') {
|
|
1523
|
+
hashCount++;
|
|
1524
|
+
}
|
|
1525
|
+
if (hashCount >= 1 && hashCount <= 6 && line[hashCount] === ' ') {
|
|
1526
|
+
// Extract content after "# " and strip trailing hashes
|
|
1527
|
+
const content = line.slice(hashCount + 1).replace(/\s*#+\s*$/, '');
|
|
1528
|
+
const tag = 'h' + hashCount;
|
|
1529
|
+
result.push(`<${tag}${getAttr(tag)}${dataQd('#'.repeat(hashCount))}>${content}</${tag}>`);
|
|
1530
|
+
i++;
|
|
1531
|
+
continue;
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
// ── Horizontal Rule ──
|
|
1535
|
+
// Three or more dashes, optional trailing whitespace, nothing else.
|
|
1536
|
+
if (isDashHRLine(line)) {
|
|
1537
|
+
result.push(`<hr${getAttr('hr')}>`);
|
|
1538
|
+
i++;
|
|
1539
|
+
continue;
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
// ── Blockquote ──
|
|
1543
|
+
// After Phase 2, the '>' character has been escaped to '>'.
|
|
1544
|
+
// Pattern: "> content" or merged consecutive blockquotes.
|
|
1545
|
+
if (/^>\s+/.test(line)) {
|
|
1546
|
+
result.push(`<blockquote${getAttr('blockquote')}>${line.replace(/^>\s+/, '')}</blockquote>`);
|
|
1547
|
+
i++;
|
|
1548
|
+
continue;
|
|
1549
|
+
}
|
|
1550
|
+
|
|
1551
|
+
// ── Pass-through ──
|
|
1552
|
+
result.push(line);
|
|
1553
|
+
i++;
|
|
1554
|
+
}
|
|
1555
|
+
|
|
1556
|
+
// Merge consecutive blockquotes into a single element.
|
|
1557
|
+
// <blockquote>A</blockquote>\n<blockquote>B</blockquote>
|
|
1558
|
+
// → <blockquote>A\nB</blockquote>
|
|
1559
|
+
let joined = result.join('\n');
|
|
1560
|
+
joined = joined.replace(/<\/blockquote>\n<blockquote>/g, '\n');
|
|
1561
|
+
return joined;
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
// ════════════════════════════════════════════════════════════════════
|
|
1565
|
+
// Table processing (line walker)
|
|
1566
|
+
// ════════════════════════════════════════════════════════════════════
|
|
1567
|
+
|
|
1568
|
+
/**
|
|
1569
|
+
* Inline markdown formatter for table cells.
|
|
1570
|
+
* Handles bold, italic, strikethrough, and code within cell text.
|
|
1571
|
+
* Links / images / autolinks are handled by the global inline pass
|
|
1572
|
+
* (Phase 3 Step 4) which runs after table processing.
|
|
1315
1573
|
*/
|
|
1316
1574
|
function processInlineMarkdown(text, getAttr) {
|
|
1317
|
-
|
|
1318
|
-
// Process inline formatting patterns
|
|
1319
1575
|
const patterns = [
|
|
1320
1576
|
[/\*\*(.+?)\*\*/g, 'strong'],
|
|
1321
1577
|
[/__(.+?)__/g, 'strong'],
|
|
@@ -1324,27 +1580,32 @@ function processInlineMarkdown(text, getAttr) {
|
|
|
1324
1580
|
[/~~(.+?)~~/g, 'del'],
|
|
1325
1581
|
[/`([^`]+)`/g, 'code']
|
|
1326
1582
|
];
|
|
1327
|
-
|
|
1328
1583
|
patterns.forEach(([pattern, tag]) => {
|
|
1329
1584
|
text = text.replace(pattern, `<${tag}${getAttr(tag)}>$1</${tag}>`);
|
|
1330
1585
|
});
|
|
1331
|
-
|
|
1332
1586
|
return text;
|
|
1333
1587
|
}
|
|
1334
1588
|
|
|
1335
1589
|
/**
|
|
1336
|
-
*
|
|
1590
|
+
* processTable — line walker for markdown tables
|
|
1591
|
+
*
|
|
1592
|
+
* Walks through lines looking for runs of pipe-containing lines.
|
|
1593
|
+
* Each run is validated (must contain a separator row: |---|---|)
|
|
1594
|
+
* and rendered as an HTML <table>. Invalid runs are restored as-is.
|
|
1595
|
+
*
|
|
1596
|
+
* @param {string} text Full document text
|
|
1597
|
+
* @param {Function} getAttr Attribute factory
|
|
1598
|
+
* @returns {string} Text with tables rendered
|
|
1337
1599
|
*/
|
|
1338
1600
|
function processTable(text, getAttr) {
|
|
1339
1601
|
const lines = text.split('\n');
|
|
1340
1602
|
const result = [];
|
|
1341
1603
|
let inTable = false;
|
|
1342
1604
|
let tableLines = [];
|
|
1343
|
-
|
|
1605
|
+
|
|
1344
1606
|
for (let i = 0; i < lines.length; i++) {
|
|
1345
1607
|
const line = lines[i].trim();
|
|
1346
|
-
|
|
1347
|
-
// Check if this line looks like a table row (with or without trailing |)
|
|
1608
|
+
|
|
1348
1609
|
if (line.includes('|') && (line.startsWith('|') || /[^\\|]/.test(line))) {
|
|
1349
1610
|
if (!inTable) {
|
|
1350
1611
|
inTable = true;
|
|
@@ -1352,14 +1613,11 @@ function processTable(text, getAttr) {
|
|
|
1352
1613
|
}
|
|
1353
1614
|
tableLines.push(line);
|
|
1354
1615
|
} else {
|
|
1355
|
-
// Not a table line
|
|
1356
1616
|
if (inTable) {
|
|
1357
|
-
// Process the accumulated table
|
|
1358
1617
|
const tableHtml = buildTable(tableLines, getAttr);
|
|
1359
1618
|
if (tableHtml) {
|
|
1360
1619
|
result.push(tableHtml);
|
|
1361
1620
|
} else {
|
|
1362
|
-
// Not a valid table, restore original lines
|
|
1363
1621
|
result.push(...tableLines);
|
|
1364
1622
|
}
|
|
1365
1623
|
inTable = false;
|
|
@@ -1368,8 +1626,8 @@ function processTable(text, getAttr) {
|
|
|
1368
1626
|
result.push(lines[i]);
|
|
1369
1627
|
}
|
|
1370
1628
|
}
|
|
1371
|
-
|
|
1372
|
-
// Handle table at end of
|
|
1629
|
+
|
|
1630
|
+
// Handle table at end of document
|
|
1373
1631
|
if (inTable && tableLines.length > 0) {
|
|
1374
1632
|
const tableHtml = buildTable(tableLines, getAttr);
|
|
1375
1633
|
if (tableHtml) {
|
|
@@ -1378,35 +1636,35 @@ function processTable(text, getAttr) {
|
|
|
1378
1636
|
result.push(...tableLines);
|
|
1379
1637
|
}
|
|
1380
1638
|
}
|
|
1381
|
-
|
|
1639
|
+
|
|
1382
1640
|
return result.join('\n');
|
|
1383
1641
|
}
|
|
1384
1642
|
|
|
1385
1643
|
/**
|
|
1386
|
-
*
|
|
1644
|
+
* buildTable — validate and render a table from accumulated lines
|
|
1645
|
+
*
|
|
1646
|
+
* @param {string[]} lines Array of pipe-containing lines
|
|
1647
|
+
* @param {Function} getAttr Attribute factory
|
|
1648
|
+
* @returns {string|null} HTML table string, or null if invalid
|
|
1387
1649
|
*/
|
|
1388
1650
|
function buildTable(lines, getAttr) {
|
|
1389
|
-
|
|
1390
1651
|
if (lines.length < 2) return null;
|
|
1391
|
-
|
|
1392
|
-
//
|
|
1652
|
+
|
|
1653
|
+
// Find the separator row (---|---|)
|
|
1393
1654
|
let separatorIndex = -1;
|
|
1394
1655
|
for (let i = 1; i < lines.length; i++) {
|
|
1395
|
-
// Support separator with or without leading/trailing pipes
|
|
1396
1656
|
if (/^\|?[\s\-:|]+\|?$/.test(lines[i]) && lines[i].includes('-')) {
|
|
1397
1657
|
separatorIndex = i;
|
|
1398
1658
|
break;
|
|
1399
1659
|
}
|
|
1400
1660
|
}
|
|
1401
|
-
|
|
1402
1661
|
if (separatorIndex === -1) return null;
|
|
1403
|
-
|
|
1662
|
+
|
|
1404
1663
|
const headerLines = lines.slice(0, separatorIndex);
|
|
1405
1664
|
const bodyLines = lines.slice(separatorIndex + 1);
|
|
1406
|
-
|
|
1407
|
-
// Parse alignment from separator
|
|
1665
|
+
|
|
1666
|
+
// Parse alignment from separator cells (:--- = left, :---: = center, ---: = right)
|
|
1408
1667
|
const separator = lines[separatorIndex];
|
|
1409
|
-
// Handle pipes at start/end or not
|
|
1410
1668
|
const separatorCells = separator.trim().replace(/^\|/, '').replace(/\|$/, '').split('|');
|
|
1411
1669
|
const alignments = separatorCells.map(cell => {
|
|
1412
1670
|
const trimmed = cell.trim();
|
|
@@ -1414,31 +1672,28 @@ function buildTable(lines, getAttr) {
|
|
|
1414
1672
|
if (trimmed.endsWith(':')) return 'right';
|
|
1415
1673
|
return 'left';
|
|
1416
1674
|
});
|
|
1417
|
-
|
|
1675
|
+
|
|
1418
1676
|
let html = `<table${getAttr('table')}>\n`;
|
|
1419
|
-
|
|
1420
|
-
//
|
|
1421
|
-
// Note: headerLines will always have length > 0 since separatorIndex starts from 1
|
|
1677
|
+
|
|
1678
|
+
// Header
|
|
1422
1679
|
html += `<thead${getAttr('thead')}>\n`;
|
|
1423
1680
|
headerLines.forEach(line => {
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
html += '</tr>\n';
|
|
1681
|
+
html += `<tr${getAttr('tr')}>\n`;
|
|
1682
|
+
const cells = line.trim().replace(/^\|/, '').replace(/\|$/, '').split('|');
|
|
1683
|
+
cells.forEach((cell, i) => {
|
|
1684
|
+
const alignStyle = alignments[i] && alignments[i] !== 'left' ? `text-align:${alignments[i]}` : '';
|
|
1685
|
+
const processedCell = processInlineMarkdown(cell.trim(), getAttr);
|
|
1686
|
+
html += `<th${getAttr('th', alignStyle)}>${processedCell}</th>\n`;
|
|
1687
|
+
});
|
|
1688
|
+
html += '</tr>\n';
|
|
1433
1689
|
});
|
|
1434
1690
|
html += '</thead>\n';
|
|
1435
|
-
|
|
1436
|
-
//
|
|
1691
|
+
|
|
1692
|
+
// Body
|
|
1437
1693
|
if (bodyLines.length > 0) {
|
|
1438
1694
|
html += `<tbody${getAttr('tbody')}>\n`;
|
|
1439
1695
|
bodyLines.forEach(line => {
|
|
1440
1696
|
html += `<tr${getAttr('tr')}>\n`;
|
|
1441
|
-
// Handle pipes at start/end or not
|
|
1442
1697
|
const cells = line.trim().replace(/^\|/, '').replace(/\|$/, '').split('|');
|
|
1443
1698
|
cells.forEach((cell, i) => {
|
|
1444
1699
|
const alignStyle = alignments[i] && alignments[i] !== 'left' ? `text-align:${alignments[i]}` : '';
|
|
@@ -1449,61 +1704,81 @@ function buildTable(lines, getAttr) {
|
|
|
1449
1704
|
});
|
|
1450
1705
|
html += '</tbody>\n';
|
|
1451
1706
|
}
|
|
1452
|
-
|
|
1707
|
+
|
|
1453
1708
|
html += '</table>';
|
|
1454
1709
|
return html;
|
|
1455
1710
|
}
|
|
1456
1711
|
|
|
1712
|
+
// ════════════════════════════════════════════════════════════════════
|
|
1713
|
+
// List processing (line walker)
|
|
1714
|
+
// ════════════════════════════════════════════════════════════════════
|
|
1715
|
+
|
|
1457
1716
|
/**
|
|
1458
|
-
*
|
|
1717
|
+
* processLists — line walker for ordered, unordered, and task lists
|
|
1718
|
+
*
|
|
1719
|
+
* Scans each line for list markers (-, *, +, 1., 2., etc.) with
|
|
1720
|
+
* optional leading indentation for nesting. Non-list lines close
|
|
1721
|
+
* any open lists and pass through unchanged.
|
|
1722
|
+
*
|
|
1723
|
+
* Task lists (- [ ] / - [x]) are detected and rendered with
|
|
1724
|
+
* checkbox inputs.
|
|
1725
|
+
*
|
|
1726
|
+
* @param {string} text Full document text
|
|
1727
|
+
* @param {Function} getAttr Attribute factory
|
|
1728
|
+
* @param {boolean} inline_styles Whether to use inline styles
|
|
1729
|
+
* @param {boolean} bidirectional Whether to add data-qd markers
|
|
1730
|
+
* @returns {string} Text with lists rendered
|
|
1459
1731
|
*/
|
|
1460
1732
|
function processLists(text, getAttr, inline_styles, bidirectional) {
|
|
1461
|
-
|
|
1462
1733
|
const lines = text.split('\n');
|
|
1463
1734
|
const result = [];
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
// Helper to escape HTML for data-qd attributes
|
|
1467
|
-
|
|
1735
|
+
const listStack = []; // tracks nesting: [{type:'ul', level:0}, …]
|
|
1736
|
+
|
|
1737
|
+
// Helper to escape HTML for data-qd attributes. List markers (`-`, `*`,
|
|
1738
|
+
// `+`, `1.`, etc.) never contain HTML-special chars, so the replace
|
|
1739
|
+
// callback is defensive-only and never actually fires in practice.
|
|
1740
|
+
/* istanbul ignore next - defensive: list markers never trigger escaping */
|
|
1741
|
+
const escapeHtml = (text) => text.replace(/[&<>"']/g,
|
|
1742
|
+
/* istanbul ignore next - defensive: list markers never contain HTML specials */
|
|
1743
|
+
m => ({'&':'&','<':'<','>':'>','"':'"',"'":'''})[m]);
|
|
1744
|
+
/* istanbul ignore next - trivial no-op fallback; not exercised via bd bundle */
|
|
1468
1745
|
const dataQd = bidirectional ? (marker) => ` data-qd="${escapeHtml(marker)}"` : () => '';
|
|
1469
|
-
|
|
1746
|
+
|
|
1470
1747
|
for (let i = 0; i < lines.length; i++) {
|
|
1471
1748
|
const line = lines[i];
|
|
1472
1749
|
const match = line.match(/^(\s*)([*\-+]|\d+\.)\s+(.+)$/);
|
|
1473
|
-
|
|
1750
|
+
|
|
1474
1751
|
if (match) {
|
|
1475
1752
|
const [, indent, marker, content] = match;
|
|
1476
1753
|
const level = Math.floor(indent.length / 2);
|
|
1477
1754
|
const isOrdered = /^\d+\./.test(marker);
|
|
1478
1755
|
const listType = isOrdered ? 'ol' : 'ul';
|
|
1479
|
-
|
|
1480
|
-
//
|
|
1756
|
+
|
|
1757
|
+
// Task list detection (only in unordered lists)
|
|
1481
1758
|
let listItemContent = content;
|
|
1482
1759
|
let taskListClass = '';
|
|
1483
1760
|
const taskMatch = content.match(/^\[([x ])\]\s+(.*)$/i);
|
|
1484
1761
|
if (taskMatch && !isOrdered) {
|
|
1485
1762
|
const [, checked, taskContent] = taskMatch;
|
|
1486
1763
|
const isChecked = checked.toLowerCase() === 'x';
|
|
1487
|
-
const checkboxAttr = inline_styles
|
|
1488
|
-
? ' style="margin-right:.5em"'
|
|
1764
|
+
const checkboxAttr = inline_styles
|
|
1765
|
+
? ' style="margin-right:.5em"'
|
|
1489
1766
|
: ` class="${CLASS_PREFIX}task-checkbox"`;
|
|
1490
1767
|
listItemContent = `<input type="checkbox"${checkboxAttr}${isChecked ? ' checked' : ''} disabled> ${taskContent}`;
|
|
1491
1768
|
taskListClass = inline_styles ? ' style="list-style:none"' : ` class="${CLASS_PREFIX}task-item"`;
|
|
1492
1769
|
}
|
|
1493
|
-
|
|
1494
|
-
// Close deeper levels
|
|
1770
|
+
|
|
1771
|
+
// Close deeper nesting levels
|
|
1495
1772
|
while (listStack.length > level + 1) {
|
|
1496
1773
|
const list = listStack.pop();
|
|
1497
1774
|
result.push(`</${list.type}>`);
|
|
1498
1775
|
}
|
|
1499
|
-
|
|
1500
|
-
// Open new
|
|
1776
|
+
|
|
1777
|
+
// Open new list or switch type at current level
|
|
1501
1778
|
if (listStack.length === level) {
|
|
1502
|
-
// Need to open a new list
|
|
1503
1779
|
listStack.push({ type: listType, level });
|
|
1504
1780
|
result.push(`<${listType}${getAttr(listType)}>`);
|
|
1505
1781
|
} else if (listStack.length === level + 1) {
|
|
1506
|
-
// Check if we need to switch list type
|
|
1507
1782
|
const currentList = listStack[listStack.length - 1];
|
|
1508
1783
|
if (currentList.type !== listType) {
|
|
1509
1784
|
result.push(`</${currentList.type}>`);
|
|
@@ -1512,11 +1787,11 @@ function processLists(text, getAttr, inline_styles, bidirectional) {
|
|
|
1512
1787
|
result.push(`<${listType}${getAttr(listType)}>`);
|
|
1513
1788
|
}
|
|
1514
1789
|
}
|
|
1515
|
-
|
|
1790
|
+
|
|
1516
1791
|
const liAttr = taskListClass || getAttr('li');
|
|
1517
1792
|
result.push(`<li${liAttr}${dataQd(marker)}>${listItemContent}</li>`);
|
|
1518
1793
|
} else {
|
|
1519
|
-
// Not a list item
|
|
1794
|
+
// Not a list item — close all open lists
|
|
1520
1795
|
while (listStack.length > 0) {
|
|
1521
1796
|
const list = listStack.pop();
|
|
1522
1797
|
result.push(`</${list.type}>`);
|
|
@@ -1524,76 +1799,76 @@ function processLists(text, getAttr, inline_styles, bidirectional) {
|
|
|
1524
1799
|
result.push(line);
|
|
1525
1800
|
}
|
|
1526
1801
|
}
|
|
1527
|
-
|
|
1528
|
-
// Close any remaining lists
|
|
1802
|
+
|
|
1803
|
+
// Close any remaining open lists
|
|
1529
1804
|
while (listStack.length > 0) {
|
|
1530
1805
|
const list = listStack.pop();
|
|
1531
1806
|
result.push(`</${list.type}>`);
|
|
1532
1807
|
}
|
|
1533
|
-
|
|
1808
|
+
|
|
1534
1809
|
return result.join('\n');
|
|
1535
1810
|
}
|
|
1536
1811
|
|
|
1812
|
+
// ════════════════════════════════════════════════════════════════════
|
|
1813
|
+
// Static API
|
|
1814
|
+
// ════════════════════════════════════════════════════════════════════
|
|
1815
|
+
|
|
1537
1816
|
/**
|
|
1538
|
-
* Emit CSS
|
|
1539
|
-
*
|
|
1540
|
-
* @param {string}
|
|
1541
|
-
* @
|
|
1817
|
+
* Emit CSS rules for all quikdown elements.
|
|
1818
|
+
*
|
|
1819
|
+
* @param {string} prefix Class prefix (default: 'quikdown-')
|
|
1820
|
+
* @param {string} theme 'light' (default) or 'dark'
|
|
1821
|
+
* @returns {string} CSS text
|
|
1542
1822
|
*/
|
|
1543
1823
|
quikdown.emitStyles = function(prefix = 'quikdown-', theme = 'light') {
|
|
1544
1824
|
const styles = QUIKDOWN_STYLES;
|
|
1545
|
-
|
|
1546
|
-
// Define theme color overrides
|
|
1825
|
+
|
|
1547
1826
|
const themeOverrides = {
|
|
1548
1827
|
dark: {
|
|
1549
|
-
'#f4f4f4': '#2a2a2a',
|
|
1550
|
-
'#f0f0f0': '#2a2a2a',
|
|
1551
|
-
'#f2f2f2': '#2a2a2a',
|
|
1552
|
-
'#ddd': '#3a3a3a',
|
|
1553
|
-
'#06c': '#6db3f2',
|
|
1828
|
+
'#f4f4f4': '#2a2a2a', // pre background
|
|
1829
|
+
'#f0f0f0': '#2a2a2a', // code background
|
|
1830
|
+
'#f2f2f2': '#2a2a2a', // th background
|
|
1831
|
+
'#ddd': '#3a3a3a', // borders
|
|
1832
|
+
'#06c': '#6db3f2', // links
|
|
1554
1833
|
_textColor: '#e0e0e0'
|
|
1555
1834
|
},
|
|
1556
1835
|
light: {
|
|
1557
|
-
_textColor: '#333'
|
|
1836
|
+
_textColor: '#333'
|
|
1558
1837
|
}
|
|
1559
1838
|
};
|
|
1560
|
-
|
|
1839
|
+
|
|
1561
1840
|
let css = '';
|
|
1562
1841
|
for (const [tag, style] of Object.entries(styles)) {
|
|
1563
1842
|
let themedStyle = style;
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
if (!oldColor.startsWith('_')) {
|
|
1570
|
-
themedStyle = themedStyle.replace(new RegExp(oldColor, 'g'), newColor);
|
|
1571
|
-
}
|
|
1572
|
-
}
|
|
1573
|
-
|
|
1574
|
-
// Add text color for certain elements in dark theme
|
|
1575
|
-
const needsTextColor = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'td', 'li', 'blockquote'];
|
|
1576
|
-
if (needsTextColor.includes(tag)) {
|
|
1577
|
-
themedStyle += `;color:${themeOverrides.dark._textColor}`;
|
|
1578
|
-
}
|
|
1579
|
-
} else if (theme === 'light' && themeOverrides.light) {
|
|
1580
|
-
// Add explicit text color for light theme elements too
|
|
1581
|
-
const needsTextColor = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'td', 'li', 'blockquote'];
|
|
1582
|
-
if (needsTextColor.includes(tag)) {
|
|
1583
|
-
themedStyle += `;color:${themeOverrides.light._textColor}`;
|
|
1843
|
+
|
|
1844
|
+
if (theme === 'dark' && themeOverrides.dark) {
|
|
1845
|
+
for (const [oldColor, newColor] of Object.entries(themeOverrides.dark)) {
|
|
1846
|
+
if (!oldColor.startsWith('_')) {
|
|
1847
|
+
themedStyle = themedStyle.replaceAll(oldColor, newColor);
|
|
1584
1848
|
}
|
|
1585
1849
|
}
|
|
1586
|
-
|
|
1850
|
+
const needsTextColor = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'td', 'li', 'blockquote'];
|
|
1851
|
+
if (needsTextColor.includes(tag)) {
|
|
1852
|
+
themedStyle += `;color:${themeOverrides.dark._textColor}`;
|
|
1853
|
+
}
|
|
1854
|
+
} else if (theme === 'light' && themeOverrides.light) {
|
|
1855
|
+
const needsTextColor = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'td', 'li', 'blockquote'];
|
|
1856
|
+
if (needsTextColor.includes(tag)) {
|
|
1857
|
+
themedStyle += `;color:${themeOverrides.light._textColor}`;
|
|
1858
|
+
}
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1587
1861
|
css += `.${prefix}${tag} { ${themedStyle} }\n`;
|
|
1588
1862
|
}
|
|
1589
|
-
|
|
1863
|
+
|
|
1590
1864
|
return css;
|
|
1591
1865
|
};
|
|
1592
1866
|
|
|
1593
1867
|
/**
|
|
1594
|
-
*
|
|
1595
|
-
*
|
|
1596
|
-
* @
|
|
1868
|
+
* Create a pre-configured parser with baked-in options.
|
|
1869
|
+
*
|
|
1870
|
+
* @param {Object} options Options to bake in
|
|
1871
|
+
* @returns {Function} Configured quikdown(markdown) function
|
|
1597
1872
|
*/
|
|
1598
1873
|
quikdown.configure = function(options) {
|
|
1599
1874
|
return function(markdown) {
|
|
@@ -1601,18 +1876,18 @@ quikdown.configure = function(options) {
|
|
|
1601
1876
|
};
|
|
1602
1877
|
};
|
|
1603
1878
|
|
|
1604
|
-
/**
|
|
1605
|
-
* Version information
|
|
1606
|
-
*/
|
|
1879
|
+
/** Semantic version (injected at build time) */
|
|
1607
1880
|
quikdown.version = quikdownVersion;
|
|
1608
1881
|
|
|
1609
|
-
//
|
|
1882
|
+
// ════════════════════════════════════════════════════════════════════
|
|
1883
|
+
// Exports
|
|
1884
|
+
// ════════════════════════════════════════════════════════════════════
|
|
1885
|
+
|
|
1610
1886
|
/* istanbul ignore next */
|
|
1611
1887
|
if (typeof module !== 'undefined' && module.exports) {
|
|
1612
1888
|
module.exports = quikdown;
|
|
1613
1889
|
}
|
|
1614
1890
|
|
|
1615
|
-
// For browser global
|
|
1616
1891
|
/* istanbul ignore next */
|
|
1617
1892
|
if (typeof window !== 'undefined') {
|
|
1618
1893
|
window.quikdown = quikdown;
|