inkbone 0.0.0-stage → 2.0.0

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.
Files changed (139) hide show
  1. package/LICENSE +21 -0
  2. package/README.en.md +318 -0
  3. package/README.md +333 -2
  4. package/bin/inkbone.mjs +941 -0
  5. package/configs/genres.json +570 -0
  6. package/configs/messages.json +302 -0
  7. package/configs/platforms.json +62 -0
  8. package/configs/presets.json +265 -0
  9. package/configs/severity.json +52 -0
  10. package/configs/zh-linguistics.json +986 -0
  11. package/configs/zh-traditional.json +97 -0
  12. package/examples/claude-code-hooks.json +15 -0
  13. package/examples/mcp-config.json +12 -0
  14. package/package.json +61 -4
  15. package/prompts/00-style-architect.md +42 -0
  16. package/prompts/01-beat-planner.md +54 -0
  17. package/prompts/02-paratactic-renderer.md +60 -0
  18. package/prompts/03-targeted-rewriter.md +39 -0
  19. package/prompts/04-evidence-reviewer.md +39 -0
  20. package/prompts/05-reader-simulator.md +54 -0
  21. package/prompts/06-ledger-importer.md +41 -0
  22. package/prompts/07-chapter-settler.md +66 -0
  23. package/resources/bad_vs_good_cases.md +98 -0
  24. package/resources/eval/blind-read-instructions.md +18 -0
  25. package/resources/genre_playbooks/classical-xianxia.md +22 -0
  26. package/resources/genre_playbooks/general-clean.md +19 -0
  27. package/resources/genre_playbooks/historical-court.md +21 -0
  28. package/resources/genre_playbooks/light-fantasy.md +21 -0
  29. package/resources/genre_playbooks/mystery-thriller.md +22 -0
  30. package/resources/genre_playbooks/romance-emotional.md +20 -0
  31. package/resources/genre_playbooks/scifi-wasteland.md +21 -0
  32. package/resources/genre_playbooks/urban-realism.md +22 -0
  33. package/resources/genre_playbooks/wuxia-jianghu.md +21 -0
  34. package/resources/micro_techniques.md +77 -0
  35. package/skills/inkbone/LICENSE +21 -0
  36. package/skills/inkbone/README.md +334 -0
  37. package/skills/inkbone/SKILL.md +155 -0
  38. package/skills/inkbone/benchmarks/eval-set/README.md +49 -0
  39. package/skills/inkbone/benchmarks/eval-set/public-pairs.json +42 -0
  40. package/skills/inkbone/benchmarks/eval-set/seeds.json +42 -0
  41. package/skills/inkbone/bin/inkbone.mjs +941 -0
  42. package/skills/inkbone/configs/genres.json +570 -0
  43. package/skills/inkbone/configs/messages.json +302 -0
  44. package/skills/inkbone/configs/platforms.json +62 -0
  45. package/skills/inkbone/configs/presets.json +265 -0
  46. package/skills/inkbone/configs/severity.json +52 -0
  47. package/skills/inkbone/configs/zh-linguistics.json +986 -0
  48. package/skills/inkbone/configs/zh-traditional.json +97 -0
  49. package/skills/inkbone/package.json +63 -0
  50. package/skills/inkbone/prompts/00-style-architect.md +42 -0
  51. package/skills/inkbone/prompts/01-beat-planner.md +54 -0
  52. package/skills/inkbone/prompts/02-paratactic-renderer.md +60 -0
  53. package/skills/inkbone/prompts/03-targeted-rewriter.md +39 -0
  54. package/skills/inkbone/prompts/04-evidence-reviewer.md +39 -0
  55. package/skills/inkbone/prompts/05-reader-simulator.md +54 -0
  56. package/skills/inkbone/prompts/06-ledger-importer.md +41 -0
  57. package/skills/inkbone/prompts/07-chapter-settler.md +66 -0
  58. package/skills/inkbone/resources/bad_vs_good_cases.md +98 -0
  59. package/skills/inkbone/resources/eval/blind-read-instructions.md +18 -0
  60. package/skills/inkbone/resources/genre_playbooks/classical-xianxia.md +22 -0
  61. package/skills/inkbone/resources/genre_playbooks/general-clean.md +19 -0
  62. package/skills/inkbone/resources/genre_playbooks/historical-court.md +21 -0
  63. package/skills/inkbone/resources/genre_playbooks/light-fantasy.md +21 -0
  64. package/skills/inkbone/resources/genre_playbooks/mystery-thriller.md +22 -0
  65. package/skills/inkbone/resources/genre_playbooks/romance-emotional.md +20 -0
  66. package/skills/inkbone/resources/genre_playbooks/scifi-wasteland.md +21 -0
  67. package/skills/inkbone/resources/genre_playbooks/urban-realism.md +22 -0
  68. package/skills/inkbone/resources/genre_playbooks/wuxia-jianghu.md +21 -0
  69. package/skills/inkbone/resources/micro_techniques.md +77 -0
  70. package/skills/inkbone/src/auditor.mjs +24 -0
  71. package/skills/inkbone/src/benchmark-runner.mjs +71 -0
  72. package/skills/inkbone/src/boundary-compiler.mjs +54 -0
  73. package/skills/inkbone/src/browser-doctor.mjs +30 -0
  74. package/skills/inkbone/src/calibrator.mjs +175 -0
  75. package/skills/inkbone/src/config-loader.mjs +233 -0
  76. package/skills/inkbone/src/continuity.mjs +5 -0
  77. package/skills/inkbone/src/core/auditor.mjs +914 -0
  78. package/skills/inkbone/src/core/boundary-compiler.mjs +423 -0
  79. package/skills/inkbone/src/core/browser-doctor.mjs +318 -0
  80. package/skills/inkbone/src/core/config-compiler.mjs +434 -0
  81. package/skills/inkbone/src/core/continuity.mjs +158 -0
  82. package/skills/inkbone/src/core/evidence-validator.mjs +77 -0
  83. package/skills/inkbone/src/core/exporter.mjs +128 -0
  84. package/skills/inkbone/src/core/formatter.mjs +241 -0
  85. package/skills/inkbone/src/core/ledger-manager.mjs +576 -0
  86. package/skills/inkbone/src/core/normalizer.mjs +517 -0
  87. package/skills/inkbone/src/core/profiler.mjs +686 -0
  88. package/skills/inkbone/src/core/rule-engine.mjs +1536 -0
  89. package/skills/inkbone/src/core/style-profiler.mjs +229 -0
  90. package/skills/inkbone/src/detector.mjs +270 -0
  91. package/skills/inkbone/src/doctor.mjs +428 -0
  92. package/skills/inkbone/src/evaluation.mjs +311 -0
  93. package/skills/inkbone/src/evidence-validator.mjs +15 -0
  94. package/skills/inkbone/src/exporter.mjs +106 -0
  95. package/skills/inkbone/src/formatter.mjs +34 -0
  96. package/skills/inkbone/src/importer.mjs +230 -0
  97. package/skills/inkbone/src/index.mjs +116 -0
  98. package/skills/inkbone/src/ledger-manager.mjs +15 -0
  99. package/skills/inkbone/src/mcp-server.mjs +861 -0
  100. package/skills/inkbone/src/normalizer.mjs +73 -0
  101. package/skills/inkbone/src/profiler.mjs +17 -0
  102. package/skills/inkbone/src/rule-engine.mjs +73 -0
  103. package/skills/inkbone/src/rule-stats.mjs +142 -0
  104. package/skills/inkbone/src/style-profiler.mjs +25 -0
  105. package/src/auditor.mjs +24 -0
  106. package/src/benchmark-runner.mjs +71 -0
  107. package/src/boundary-compiler.mjs +54 -0
  108. package/src/browser-doctor.mjs +30 -0
  109. package/src/calibrator.mjs +175 -0
  110. package/src/config-loader.mjs +233 -0
  111. package/src/continuity.mjs +5 -0
  112. package/src/core/auditor.mjs +914 -0
  113. package/src/core/boundary-compiler.mjs +423 -0
  114. package/src/core/browser-doctor.mjs +318 -0
  115. package/src/core/config-compiler.mjs +434 -0
  116. package/src/core/continuity.mjs +158 -0
  117. package/src/core/evidence-validator.mjs +77 -0
  118. package/src/core/exporter.mjs +128 -0
  119. package/src/core/formatter.mjs +241 -0
  120. package/src/core/ledger-manager.mjs +576 -0
  121. package/src/core/normalizer.mjs +517 -0
  122. package/src/core/profiler.mjs +686 -0
  123. package/src/core/rule-engine.mjs +1536 -0
  124. package/src/core/style-profiler.mjs +229 -0
  125. package/src/detector.mjs +270 -0
  126. package/src/doctor.mjs +428 -0
  127. package/src/evaluation.mjs +311 -0
  128. package/src/evidence-validator.mjs +15 -0
  129. package/src/exporter.mjs +106 -0
  130. package/src/formatter.mjs +34 -0
  131. package/src/importer.mjs +230 -0
  132. package/src/index.mjs +116 -0
  133. package/src/ledger-manager.mjs +15 -0
  134. package/src/mcp-server.mjs +861 -0
  135. package/src/normalizer.mjs +73 -0
  136. package/src/profiler.mjs +17 -0
  137. package/src/rule-engine.mjs +73 -0
  138. package/src/rule-stats.mjs +142 -0
  139. package/src/style-profiler.mjs +25 -0
@@ -0,0 +1,517 @@
1
+ // 零 Token 确定性排版与标点清洗器(严格零汉字代码,规则词条来自 JSON 配置)
2
+
3
+ // 按行拆解文本并保留每一行的原始换行符(\r\n 或 \n),避免全局改写无关行尾
4
+ function splitLinesKeepingEndings(input) {
5
+ const lines = [];
6
+ const endings = [];
7
+ let cursor = 0;
8
+
9
+ while (cursor < input.length) {
10
+ const nlIndex = input.indexOf("\n", cursor);
11
+ if (nlIndex === -1) {
12
+ lines.push(input.slice(cursor));
13
+ endings.push("");
14
+ break;
15
+ }
16
+ const isCrlf = nlIndex > cursor && input[nlIndex - 1] === "\r";
17
+ lines.push(input.slice(cursor, isCrlf ? nlIndex - 1 : nlIndex));
18
+ endings.push(isCrlf ? "\r\n" : "\n");
19
+ cursor = nlIndex + 1;
20
+ }
21
+
22
+ return { lines, endings };
23
+ }
24
+
25
+ function hasYamlFrontMatter(lines) {
26
+ if (!lines[0] || lines[0].trim() !== "---") return false;
27
+ let sawField = false;
28
+ for (let i = 1; i < Math.min(lines.length, 40); i += 1) {
29
+ const trimmed = lines[i].trim();
30
+ if (trimmed === "---") return sawField;
31
+ if (/^[A-Za-z0-9_-]+:\s*/.test(trimmed)) sawField = true;
32
+ }
33
+ return false;
34
+ }
35
+
36
+ function parseOpeningFence(line) {
37
+ const m = line.match(/^ {0,3}(`{3,}|~{3,})(.*)$/);
38
+ if (!m) return null;
39
+ if (m[1][0] === "`" && m[2].includes("`")) return null;
40
+ return { marker: m[1][0], minLen: m[1].length };
41
+ }
42
+
43
+ function isClosingFence(line, fence) {
44
+ const esc = fence.marker === "`" ? "`" : "~";
45
+ const m = line.match(new RegExp(`^ {0,3}(${esc}{3,})[\\t ]*$`));
46
+ return Boolean(m && m[1].length >= fence.minLen);
47
+ }
48
+
49
+ // 计算单行内 HTML 注释区间(支持跨行状态透传)
50
+ function getHtmlCommentSpans(line, openBefore) {
51
+ const spans = [];
52
+ let open = openBefore;
53
+ let cursor = 0;
54
+
55
+ while (cursor < line.length) {
56
+ if (open) {
57
+ const close = line.indexOf("-->", cursor);
58
+ if (close === -1) {
59
+ spans.push([cursor, line.length]);
60
+ return { spans, open: true };
61
+ }
62
+ spans.push([cursor, close + 3]);
63
+ cursor = close + 3;
64
+ open = false;
65
+ continue;
66
+ }
67
+ const start = line.indexOf("<!--", cursor);
68
+ if (start === -1) break;
69
+ cursor = start;
70
+ open = true;
71
+ }
72
+
73
+ return { spans, open };
74
+ }
75
+
76
+ // 计算单行内反引号代码与书级字面量白名单的受保护区间
77
+ function getProtectedSpans(line, commentSpans, whitelistLiterals) {
78
+ const spans = [...commentSpans];
79
+
80
+ // 保护行内反引号片段
81
+ let btIdx = 0;
82
+ while (btIdx < line.length) {
83
+ const open = line.indexOf("`", btIdx);
84
+ if (open === -1) break;
85
+ const close = line.indexOf("`", open + 1);
86
+ if (close === -1) break;
87
+ spans.push([open, close + 1]);
88
+ btIdx = close + 1;
89
+ }
90
+
91
+ // 保护书级白名单字面量
92
+ for (const literal of whitelistLiterals || []) {
93
+ if (!literal) continue;
94
+ let idx = line.indexOf(literal);
95
+ while (idx !== -1) {
96
+ spans.push([idx, idx + literal.length]);
97
+ idx = line.indexOf(literal, idx + literal.length);
98
+ }
99
+ }
100
+
101
+ return spans;
102
+ }
103
+
104
+ // 提取单行对话引号区间与方括号面板区间(含行末未收口多段对白)
105
+ function getDialogueAndPanelSpans(line) {
106
+ const spans = [];
107
+ const stack = [];
108
+ const openMap = new Map([
109
+ ["\"", "\""],
110
+ ["\u201c", "\u201d"],
111
+ ["\u300c", "\u300d"],
112
+ ["\u300e", "\u300f"],
113
+ ["\u3010", "\u3011"],
114
+ ]);
115
+ const closeSet = new Set(["\u201d", "\u300d", "\u300f", "\u3011"]);
116
+
117
+ for (let i = 0; i < line.length; i += 1) {
118
+ const ch = line[i];
119
+ const top = stack.length > 0 ? stack[stack.length - 1] : null;
120
+ if (ch === "\"" && top && top.ch === "\"") {
121
+ const open = stack.pop();
122
+ if (stack.length === 0) spans.push([open.at + 1, i]);
123
+ } else if (openMap.has(ch)) {
124
+ stack.push({ ch, expected: openMap.get(ch), at: i });
125
+ } else if (closeSet.has(ch) && top && top.expected === ch) {
126
+ const open = stack.pop();
127
+ if (stack.length === 0) spans.push([open.at + 1, i]);
128
+ }
129
+ }
130
+ if (stack.length > 0) {
131
+ spans.push([stack[0].at + 1, line.length]);
132
+ }
133
+ return spans;
134
+ }
135
+
136
+ function insideSpans(start, end, spans) {
137
+ return spans.some(([s, e]) => start < e && end > s);
138
+ }
139
+
140
+ // 在未受保护区间剥离成对的 Markdown 加粗(**...**)或斜体(*...*)星号
141
+ function stripEmphasisInLine(line, lineNo, commentBefore, whitelistLiterals) {
142
+ let current = line;
143
+ const findings = [];
144
+ const boldRe = /\*\*([^*\r\n]+)\*\*/g;
145
+ const italicRe = /(?<!\*)\*([^*\r\n]+)\*(?!\*)/g;
146
+
147
+ const stripOnePattern = (text, re, protectedList, recordFindings) => {
148
+ re.lastIndex = 0;
149
+ let output = "";
150
+ let last = 0;
151
+ let m;
152
+ while ((m = re.exec(text)) !== null) {
153
+ if (insideSpans(m.index, m.index + m[0].length, protectedList)) {
154
+ continue;
155
+ }
156
+ output += text.slice(last, m.index) + m[1];
157
+ last = m.index + m[0].length;
158
+ if (recordFindings) {
159
+ findings.push({
160
+ line: lineNo,
161
+ column: m.index + 1,
162
+ type: "markdown_emphasis",
163
+ });
164
+ }
165
+ }
166
+ output += text.slice(last);
167
+ return output;
168
+ };
169
+
170
+ let firstPass = true;
171
+ for (let iter = 0; iter < 5; iter += 1) {
172
+ const boldComments = getHtmlCommentSpans(current, commentBefore);
173
+ const boldProtected = getProtectedSpans(
174
+ current,
175
+ boldComments.spans,
176
+ whitelistLiterals
177
+ );
178
+ let next = stripOnePattern(current, boldRe, boldProtected, firstPass);
179
+ const italicComments = getHtmlCommentSpans(next, commentBefore);
180
+ const italicProtected = getProtectedSpans(
181
+ next,
182
+ italicComments.spans,
183
+ whitelistLiterals
184
+ );
185
+ next = stripOnePattern(next, italicRe, italicProtected, firstPass);
186
+ firstPass = false;
187
+ if (next === current) break;
188
+ current = next;
189
+ }
190
+
191
+ return { line: current, findings };
192
+ }
193
+
194
+ function prevNonSpace(text, idx) {
195
+ for (let i = idx; i >= 0; i -= 1) {
196
+ if (!/\s/.test(text[i])) return text[i];
197
+ }
198
+ return "";
199
+ }
200
+
201
+ function nextNonSpace(text, idx) {
202
+ for (let i = idx; i < text.length; i += 1) {
203
+ if (!/\s/.test(text[i])) return text[i];
204
+ }
205
+ return "";
206
+ }
207
+
208
+ const OPENING_DELIMS = new Set(["\"", "\u201c", "\u300c", "\u300e", "(", "\uff08", "\u3010", "["]);
209
+ const CLOSING_QUOTES = new Set(["\"", "\u201d", "\u300d", "\u300f", ")", "\uff09", "\u3011", "]"]);
210
+ const SENTENCE_ENDS = new Set(["\u3002", "\uff01", "\uff1f", "!", "?", "\u2026"]);
211
+ const ALL_PUNCT = new Set([
212
+ "\u3002",
213
+ "\uff01",
214
+ "\uff1f",
215
+ "\uff0c",
216
+ "\u3001",
217
+ "\uff1b",
218
+ "\uff1a",
219
+ "\u2026",
220
+ "!",
221
+ "?",
222
+ ",",
223
+ ";",
224
+ ":",
225
+ ".",
226
+ ]);
227
+
228
+ function chooseNarrationDashReplacement(text, start, length, normCfg) {
229
+ const before = prevNonSpace(text, start - 1);
230
+ const after = nextNonSpace(text, start + length);
231
+ const rest = text.slice(start + length).trimStart();
232
+ const left = text.slice(0, start).trim();
233
+
234
+ if (before === "" || OPENING_DELIMS.has(before)) return "";
235
+ if (/\d/.test(before) && /\d/.test(after)) {
236
+ return normCfg.rangeConnector || "-";
237
+ }
238
+ if (CLOSING_QUOTES.has(after)) {
239
+ return SENTENCE_ENDS.has(before) ? "" : "\u3002";
240
+ }
241
+ if (!after) {
242
+ return SENTENCE_ENDS.has(before) ? "" : "\u3002";
243
+ }
244
+ if (SENTENCE_ENDS.has(before) || ALL_PUNCT.has(after)) return "";
245
+ if (normCfg.colonLeadRe && normCfg.colonLeadRe.test(rest)) return "\uff1a";
246
+ if (normCfg.colonTailRe && normCfg.colonTailRe.test(left)) return "\uff1a";
247
+ return "\uff0c";
248
+ }
249
+
250
+ function normalizePunctuationPass(
251
+ line,
252
+ lineNo,
253
+ protectedSpans,
254
+ options,
255
+ normCfg
256
+ ) {
257
+ const findings = [];
258
+ const stdEllipsis = "\u2026\u2026";
259
+ const stdDash = "\u2014\u2014";
260
+ const dashUnit = "(?:\u2014+[-\u2014]*|-+\u2014+[-\u2014]*|--+)";
261
+ const ellipsisUnit = "(?:\\.+\\u2026+[.\\u2026]*|\\u2026+\\.+[.\\u2026]*|\\u2026+|\\.{3,}|\\u3002{3,})";
262
+ const pattern = new RegExp(
263
+ `${dashUnit}${ellipsisUnit}|${ellipsisUnit}${dashUnit}|${ellipsisUnit}|${dashUnit}`,
264
+ "g"
265
+ );
266
+
267
+ const quoteOrPanelSpans = getDialogueAndPanelSpans(line);
268
+ let output = "";
269
+ let lastIndex = 0;
270
+ let m;
271
+
272
+ while ((m = pattern.exec(line)) !== null) {
273
+ const token = m[0];
274
+ if (insideSpans(m.index, m.index + token.length, protectedSpans)) {
275
+ continue;
276
+ }
277
+
278
+ const inDialogueOrPanel = insideSpans(
279
+ m.index,
280
+ m.index + token.length,
281
+ quoteOrPanelSpans
282
+ );
283
+ const hasEllipsis = /[\u2026\u3002]|\.{3,}/.test(token);
284
+ const hasDash = /[\u2014-]/.test(token);
285
+
286
+ // 1. 破折号与省略号混粘或非标省略号统一收束为标准双省略号 ……
287
+ if (hasEllipsis) {
288
+ const merged = lastIndex === m.index && output.endsWith(stdEllipsis);
289
+ const rep = merged ? "" : stdEllipsis;
290
+ output += line.slice(lastIndex, m.index) + rep;
291
+ lastIndex = m.index + token.length;
292
+ if (hasDash || rep !== token) {
293
+ findings.push({
294
+ line: lineNo,
295
+ column: m.index + 1,
296
+ type: "nonstandard_ellipsis",
297
+ });
298
+ }
299
+ continue;
300
+ }
301
+
302
+ if (!hasDash) continue;
303
+
304
+ // 2. 对话引号或【…】面板内:统一规范为双破折号 ——
305
+ if (inDialogueOrPanel) {
306
+ const merged = lastIndex === m.index && output.endsWith(stdDash);
307
+ const rep = merged ? "" : stdDash;
308
+ output += line.slice(lastIndex, m.index) + rep;
309
+ lastIndex = m.index + token.length;
310
+ if (rep !== token) {
311
+ findings.push({
312
+ line: lineNo,
313
+ column: m.index + 1,
314
+ type: "dialogue_dash_normalized",
315
+ });
316
+ }
317
+ continue;
318
+ }
319
+
320
+ // 3. 引号外旁白破折号:仅在开启 fixNarrationEmDash 时替换中文破折号,默认仍清理 ASCII --
321
+ if (!options.fixNarrationEmDash) {
322
+ if (token.includes("-")) {
323
+ const merged = lastIndex === m.index && output.endsWith(stdDash);
324
+ const rep = merged ? "" : stdDash;
325
+ output += line.slice(lastIndex, m.index) + rep;
326
+ lastIndex = m.index + token.length;
327
+ findings.push({
328
+ line: lineNo,
329
+ column: m.index + 1,
330
+ type: "double_hyphen",
331
+ });
332
+ }
333
+ continue;
334
+ }
335
+
336
+ const rep = chooseNarrationDashReplacement(
337
+ line,
338
+ m.index,
339
+ token.length,
340
+ normCfg
341
+ );
342
+ output += line.slice(lastIndex, m.index) + rep;
343
+ lastIndex = m.index + token.length;
344
+ findings.push({
345
+ line: lineNo,
346
+ column: m.index + 1,
347
+ type: "narration_em_dash",
348
+ });
349
+ }
350
+
351
+ output += line.slice(lastIndex);
352
+ return { line: output, findings };
353
+ }
354
+
355
+ // 零 Token 确定性清洗正文中的 Markdown 星号、分隔线与非标省略号/破折号
356
+ export function normalizeProseText(rawContent, options = {}) {
357
+ const lingConfig = options.lingConfig || {};
358
+ const whitelistLiterals = options.whitelistLiterals || [];
359
+ const rawNorm = lingConfig.normalizer || {};
360
+ const normCfg = {
361
+ rangeConnector: rawNorm.rangeConnector || "-",
362
+ colonLeadRe: rawNorm.colonLeadRegex
363
+ ? new RegExp(rawNorm.colonLeadRegex)
364
+ : null,
365
+ colonTailRe: rawNorm.colonTailRegex
366
+ ? new RegExp(rawNorm.colonTailRegex)
367
+ : null,
368
+ };
369
+
370
+ const input = String(rawContent || "");
371
+ const { lines, endings } = splitLinesKeepingEndings(input);
372
+ const findings = [];
373
+ const outputLines = [];
374
+
375
+ let fence = null;
376
+ let inFrontMatter = hasYamlFrontMatter(lines);
377
+ let commentOpen = false;
378
+ let commentStart = null;
379
+ const commentCloseAhead = new Array(lines.length + 1).fill(false);
380
+ for (let i = lines.length - 1; i >= 0; i -= 1) {
381
+ commentCloseAhead[i] =
382
+ lines[i].includes("-->") || commentCloseAhead[i + 1];
383
+ }
384
+
385
+ for (let i = 0; i < lines.length; i += 1) {
386
+ const lineNo = i + 1;
387
+ const ending = endings[i];
388
+ let line = lines[i];
389
+ const trimmed = line.trim();
390
+
391
+ if (commentOpen && !commentCloseAhead[i]) {
392
+ findings.push({
393
+ line: commentStart?.line || lineNo,
394
+ column: commentStart?.column || 1,
395
+ type: "html_comment_unclosed",
396
+ });
397
+ commentOpen = false;
398
+ commentStart = null;
399
+ }
400
+
401
+ if (inFrontMatter) {
402
+ outputLines.push(line + ending);
403
+ if (i > 0 && trimmed === "---") inFrontMatter = false;
404
+ continue;
405
+ }
406
+
407
+ if (fence) {
408
+ outputLines.push(line + ending);
409
+ if (isClosingFence(line, fence)) fence = null;
410
+ continue;
411
+ }
412
+
413
+ const openingFence = parseOpeningFence(line);
414
+ if (openingFence) {
415
+ fence = openingFence;
416
+ outputLines.push(line + ending);
417
+ continue;
418
+ }
419
+
420
+ // 独立分隔线 --- 或 *** 直接移除(仅剥离星号模式可保留分隔线)
421
+ if (
422
+ !options.preserveDividers &&
423
+ !commentOpen &&
424
+ (trimmed === "---" || /^\s*\*{3,}\s*$/.test(line))
425
+ ) {
426
+ findings.push({
427
+ line: lineNo,
428
+ column: Math.max(1, line.search(/\S/) + 1),
429
+ type: "markdown_divider",
430
+ });
431
+ continue;
432
+ }
433
+
434
+ // Markdown 标题行原样保留
435
+ if (!commentOpen && /^\s*#{1,6}\s/.test(line)) {
436
+ outputLines.push(line + ending);
437
+ continue;
438
+ }
439
+
440
+ const commentBefore = commentOpen;
441
+ const initialComments = getHtmlCommentSpans(line, commentBefore);
442
+ commentOpen = initialComments.open;
443
+ if (!commentBefore && commentOpen) {
444
+ commentStart = {
445
+ line: lineNo,
446
+ column: Math.max(1, line.lastIndexOf("<!--") + 1),
447
+ };
448
+ } else if (!commentOpen) {
449
+ commentStart = null;
450
+ }
451
+
452
+ if (options.stripMarkdownEmphasis) {
453
+ const empRes = stripEmphasisInLine(
454
+ line,
455
+ lineNo,
456
+ commentBefore,
457
+ whitelistLiterals
458
+ );
459
+ line = empRes.line;
460
+ findings.push(...empRes.findings);
461
+ }
462
+
463
+ if (!options.skipPunctuation) {
464
+ let firstPass = true;
465
+ for (let iter = 0; iter < 5; iter += 1) {
466
+ const cState = getHtmlCommentSpans(line, commentBefore);
467
+ const prot = getProtectedSpans(line, cState.spans, whitelistLiterals);
468
+ const pRes = normalizePunctuationPass(
469
+ line,
470
+ lineNo,
471
+ prot,
472
+ options,
473
+ normCfg
474
+ );
475
+ if (firstPass) {
476
+ findings.push(...pRes.findings);
477
+ firstPass = false;
478
+ }
479
+ if (pRes.line === line) break;
480
+ line = pRes.line;
481
+ }
482
+ }
483
+
484
+ outputLines.push(line + ending);
485
+ }
486
+
487
+ if (commentOpen) {
488
+ findings.push({
489
+ line: commentStart?.line || lines.length,
490
+ column: commentStart?.column || 1,
491
+ type: "html_comment_unclosed",
492
+ });
493
+ }
494
+
495
+ const output = outputLines.join("");
496
+ return {
497
+ output,
498
+ changed: output !== input,
499
+ findingCount: findings.length,
500
+ findings,
501
+ };
502
+ }
503
+
504
+ // 仅剥离受保护区间之外的 Markdown 加粗与斜体星号,用于边界与锚点匹配
505
+ export function stripMarkdownEmphasis(
506
+ rawContent,
507
+ whitelistLiterals = [],
508
+ lingConfig = null
509
+ ) {
510
+ return normalizeProseText(rawContent, {
511
+ lingConfig,
512
+ whitelistLiterals,
513
+ stripMarkdownEmphasis: true,
514
+ skipPunctuation: true,
515
+ preserveDividers: true,
516
+ }).output;
517
+ }