@driftsys/markspec 0.11.0 → 0.12.1
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 +9 -0
- package/esm/ast/normalize.d.ts +4 -5
- package/esm/ast/normalize.d.ts.map +1 -1
- package/esm/ast/normalize.js +6 -37
- package/esm/collect/mod.d.ts +29 -9
- package/esm/collect/mod.d.ts.map +1 -1
- package/esm/collect/mod.js +36 -10
- package/esm/compiler/mod.d.ts.map +1 -1
- package/esm/compiler/mod.js +2 -6
- package/esm/config/markspec.d.ts +5 -3
- package/esm/config/markspec.d.ts.map +1 -1
- package/esm/config/mod.d.ts.map +1 -1
- package/esm/config/mod.js +47 -5
- package/esm/discovery/mod.d.ts +24 -8
- package/esm/discovery/mod.d.ts.map +1 -1
- package/esm/discovery/mod.js +57 -21
- package/esm/discovery/projects.d.ts +111 -0
- package/esm/discovery/projects.d.ts.map +1 -0
- package/esm/discovery/projects.js +242 -0
- package/esm/formatter/entry_body.d.ts +84 -0
- package/esm/formatter/entry_body.d.ts.map +1 -0
- package/esm/formatter/entry_body.js +291 -0
- package/esm/formatter/md_equiv.d.ts +6 -5
- package/esm/formatter/md_equiv.d.ts.map +1 -1
- package/esm/formatter/md_equiv.js +6 -5
- package/esm/formatter/mod.d.ts +16 -9
- package/esm/formatter/mod.d.ts.map +1 -1
- package/esm/formatter/mod.js +327 -53
- package/esm/formatter/tab_markers.d.ts +15 -0
- package/esm/formatter/tab_markers.d.ts.map +1 -0
- package/esm/formatter/tab_markers.js +133 -0
- package/esm/gates/mod.d.ts.map +1 -1
- package/esm/gates/mod.js +6 -5
- package/esm/lint/runner.d.ts.map +1 -1
- package/esm/lint/runner.js +4 -4
- package/esm/lock/check.d.ts.map +1 -1
- package/esm/lock/check.js +2 -1
- package/esm/lock/edge_drift.d.ts +2 -0
- package/esm/lock/edge_drift.d.ts.map +1 -1
- package/esm/lock/edge_drift.js +4 -0
- package/esm/lock/mod.d.ts +1 -1
- package/esm/lock/mod.d.ts.map +1 -1
- package/esm/lock/mod.js +1 -1
- package/esm/lock/upstream_deps.js +1 -1
- package/esm/mod.d.ts +11 -6
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +9 -6
- package/esm/model/mod.d.ts +22 -1
- package/esm/model/mod.d.ts.map +1 -1
- package/esm/model/mod.js +10 -0
- package/esm/parser/code_regions.d.ts +29 -0
- package/esm/parser/code_regions.d.ts.map +1 -0
- package/esm/parser/code_regions.js +38 -0
- package/esm/parser/markdown.d.ts.map +1 -1
- package/esm/parser/markdown.js +103 -40
- package/esm/parser/mod.d.ts.map +1 -1
- package/esm/parser/mod.js +7 -1
- package/esm/parser/translate.d.ts.map +1 -1
- package/esm/parser/translate.js +5 -0
- package/esm/refs/mod.d.ts +6 -2
- package/esm/refs/mod.d.ts.map +1 -1
- package/esm/refs/mod.js +15 -21
- package/esm/reporter/mod.d.ts.map +1 -1
- package/esm/reporter/mod.js +2 -2
- package/esm/sync/mapping.d.ts +4 -3
- package/esm/sync/mapping.d.ts.map +1 -1
- package/esm/sync/mapping.js +52 -9
- package/esm/util/display_id.d.ts +20 -0
- package/esm/util/display_id.d.ts.map +1 -0
- package/esm/util/display_id.js +19 -0
- package/esm/util/fence.d.ts +20 -29
- package/esm/util/fence.d.ts.map +1 -1
- package/esm/util/fence.js +42 -50
- package/esm/util/modals.d.ts +6 -0
- package/esm/util/modals.d.ts.map +1 -1
- package/esm/util/modals.js +37 -0
- package/esm/validator/body_blocks.d.ts +2 -1
- package/esm/validator/body_blocks.d.ts.map +1 -1
- package/esm/validator/body_blocks.js +80 -2
- package/esm/validator/corpus.d.ts +6 -0
- package/esm/validator/corpus.d.ts.map +1 -1
- package/esm/validator/corpus.js +16 -4
- package/esm/validator/mod.d.ts +1 -1
- package/esm/validator/mod.d.ts.map +1 -1
- package/esm/validator/mod.js +1 -1
- package/esm/validator/pipeline.d.ts +6 -4
- package/esm/validator/pipeline.d.ts.map +1 -1
- package/esm/validator/pipeline.js +24 -19
- package/package.json +1 -1
- package/script/ast/normalize.d.ts +4 -5
- package/script/ast/normalize.d.ts.map +1 -1
- package/script/ast/normalize.js +5 -36
- package/script/collect/mod.d.ts +29 -9
- package/script/collect/mod.d.ts.map +1 -1
- package/script/collect/mod.js +38 -10
- package/script/compiler/mod.d.ts.map +1 -1
- package/script/compiler/mod.js +1 -5
- package/script/config/markspec.d.ts +5 -3
- package/script/config/markspec.d.ts.map +1 -1
- package/script/config/mod.d.ts.map +1 -1
- package/script/config/mod.js +47 -5
- package/script/discovery/mod.d.ts +24 -8
- package/script/discovery/mod.d.ts.map +1 -1
- package/script/discovery/mod.js +56 -20
- package/script/discovery/projects.d.ts +111 -0
- package/script/discovery/projects.d.ts.map +1 -0
- package/script/discovery/projects.js +250 -0
- package/script/formatter/entry_body.d.ts +84 -0
- package/script/formatter/entry_body.d.ts.map +1 -0
- package/script/formatter/entry_body.js +296 -0
- package/script/formatter/md_equiv.d.ts +6 -5
- package/script/formatter/md_equiv.d.ts.map +1 -1
- package/script/formatter/md_equiv.js +6 -5
- package/script/formatter/mod.d.ts +16 -9
- package/script/formatter/mod.d.ts.map +1 -1
- package/script/formatter/mod.js +324 -50
- package/script/formatter/tab_markers.d.ts +15 -0
- package/script/formatter/tab_markers.d.ts.map +1 -0
- package/script/formatter/tab_markers.js +141 -0
- package/script/gates/mod.d.ts.map +1 -1
- package/script/gates/mod.js +15 -14
- package/script/lint/runner.d.ts.map +1 -1
- package/script/lint/runner.js +3 -3
- package/script/lock/check.d.ts.map +1 -1
- package/script/lock/check.js +2 -1
- package/script/lock/edge_drift.d.ts +2 -0
- package/script/lock/edge_drift.d.ts.map +1 -1
- package/script/lock/edge_drift.js +5 -0
- package/script/lock/mod.d.ts +1 -1
- package/script/lock/mod.d.ts.map +1 -1
- package/script/lock/mod.js +2 -1
- package/script/lock/upstream_deps.js +1 -1
- package/script/mod.d.ts +11 -6
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +23 -5
- package/script/model/mod.d.ts +22 -1
- package/script/model/mod.d.ts.map +1 -1
- package/script/model/mod.js +11 -0
- package/script/parser/code_regions.d.ts +29 -0
- package/script/parser/code_regions.d.ts.map +1 -0
- package/script/parser/code_regions.js +41 -0
- package/script/parser/markdown.d.ts.map +1 -1
- package/script/parser/markdown.js +102 -39
- package/script/parser/mod.d.ts.map +1 -1
- package/script/parser/mod.js +7 -1
- package/script/parser/translate.d.ts.map +1 -1
- package/script/parser/translate.js +5 -0
- package/script/refs/mod.d.ts +6 -2
- package/script/refs/mod.d.ts.map +1 -1
- package/script/refs/mod.js +14 -20
- package/script/reporter/mod.d.ts.map +1 -1
- package/script/reporter/mod.js +1 -1
- package/script/sync/mapping.d.ts +4 -3
- package/script/sync/mapping.d.ts.map +1 -1
- package/script/sync/mapping.js +52 -9
- package/script/util/display_id.d.ts +20 -0
- package/script/util/display_id.d.ts.map +1 -0
- package/script/util/display_id.js +22 -0
- package/script/util/fence.d.ts +20 -29
- package/script/util/fence.d.ts.map +1 -1
- package/script/util/fence.js +47 -54
- package/script/util/modals.d.ts +6 -0
- package/script/util/modals.d.ts.map +1 -1
- package/script/util/modals.js +38 -0
- package/script/validator/body_blocks.d.ts +2 -1
- package/script/validator/body_blocks.d.ts.map +1 -1
- package/script/validator/body_blocks.js +80 -2
- package/script/validator/corpus.d.ts +6 -0
- package/script/validator/corpus.d.ts.map +1 -1
- package/script/validator/corpus.js +16 -3
- package/script/validator/mod.d.ts +1 -1
- package/script/validator/mod.d.ts.map +1 -1
- package/script/validator/mod.js +2 -1
- package/script/validator/pipeline.d.ts +6 -4
- package/script/validator/pipeline.d.ts.map +1 -1
- package/script/validator/pipeline.js +24 -19
package/esm/formatter/mod.js
CHANGED
|
@@ -10,18 +10,20 @@ import { ulid as defaultUlid } from "@std/ulid";
|
|
|
10
10
|
import { stringify as stringifyYaml } from "@std/yaml";
|
|
11
11
|
import { attributeSpec, CSV_SPLITTABLE_TYPES, IDENTITY_KEY, } from "../model/mod.js";
|
|
12
12
|
import { MARKDOWN_EXTENSIONS } from "../discovery/mod.js";
|
|
13
|
-
import { ATTR_LINE_RE } from "../parser/attributes.js";
|
|
13
|
+
import { ATTR_LINE_RE, parseAttributes } from "../parser/attributes.js";
|
|
14
14
|
import { extractFrontMatter } from "../parser/frontmatter.js";
|
|
15
15
|
import { parseMarkdown } from "../parser/markdown.js";
|
|
16
|
-
import {
|
|
16
|
+
import { fencedCodeLines } from "../util/fence.js";
|
|
17
17
|
import { applyLineEnding, detectLineEnding, normalizeLineEndings, } from "../util/line_endings.js";
|
|
18
|
-
import {
|
|
18
|
+
import { isSentenceInitial as _isSentenceInitial, normalizeProseModals, } from "../util/modals.js";
|
|
19
19
|
import { synthesizedUlid } from "./synth_ulid.js";
|
|
20
20
|
import { buildBodyAst } from "../ast/build.js";
|
|
21
21
|
import { render as renderBodyAst } from "../ast/render.js";
|
|
22
22
|
import { normalizeBodyAst } from "../ast/normalize.js";
|
|
23
23
|
import { astEquivalent } from "../ast/equivalence.js";
|
|
24
24
|
import { formatProseSegments } from "./prose.js";
|
|
25
|
+
import { contentColumn } from "./entry_body.js";
|
|
26
|
+
import { entryListItems, protectedTabRanges, tabMarkers, } from "./tab_markers.js";
|
|
25
27
|
import { MARKSPEC_MARKDOWN_GLOBAL_CONFIG, } from "./dprint.js";
|
|
26
28
|
import { markdownSemanticallyEquivalent } from "./md_equiv.js";
|
|
27
29
|
/**
|
|
@@ -43,7 +45,7 @@ export function isSentenceInitial(line, offset) {
|
|
|
43
45
|
*
|
|
44
46
|
* The pass skips:
|
|
45
47
|
*
|
|
46
|
-
* -
|
|
48
|
+
* - CommonMark fenced code blocks and inline-code spans — code is
|
|
47
49
|
* verbatim per round-trip invariants (spec §5.1).
|
|
48
50
|
* - Lines indented by four or more spaces (or a tab) — conservatively
|
|
49
51
|
* captures indented code blocks and attribute trailers, both of which
|
|
@@ -52,21 +54,12 @@ export function isSentenceInitial(line, offset) {
|
|
|
52
54
|
* @remarks Not called by `format()` as of SP3 Task 5 — body modal
|
|
53
55
|
* normalization moved to `normalizeBodyAst` in the AST pass (spec-correct
|
|
54
56
|
* verbatim-boundary handling). Retained as an exported symbol because
|
|
55
|
-
* `mod_test.ts` tests it directly
|
|
56
|
-
*
|
|
57
|
+
* `mod_test.ts` tests it directly. Both entry points delegate to the same
|
|
58
|
+
* AST-backed prose normalizer. Do NOT
|
|
57
59
|
* delete — external callers may depend on this export.
|
|
58
60
|
*/
|
|
59
61
|
export function normalizeModalKeywords(markdown) {
|
|
60
|
-
|
|
61
|
-
walkProseLines(markdown, (line, i) => {
|
|
62
|
-
// Indented-code / attribute-trailer lines aren't prose either.
|
|
63
|
-
if (/^( {4}|\t)/.test(line))
|
|
64
|
-
return;
|
|
65
|
-
let normalized = line.replace(RFC2119_MODAL_RE, (m) => m.toLowerCase());
|
|
66
|
-
normalized = normalized.replace(EARS_KEYWORD_RE, (m, _g1, offset) => isSentenceInitial(normalized, offset) ? m : m.toLowerCase());
|
|
67
|
-
lines[i] = normalized;
|
|
68
|
-
});
|
|
69
|
-
return lines.join("\n");
|
|
62
|
+
return normalizeProseModals(markdown);
|
|
70
63
|
}
|
|
71
64
|
/**
|
|
72
65
|
* Canonical front-matter key order per ADR-007. Core keys first, then
|
|
@@ -217,18 +210,71 @@ function emitBodyViaAst(lines, file, diagnostics, cachedEntries, proseFormat) {
|
|
|
217
210
|
// Process bottom-to-top so splices don't shift earlier entry positions.
|
|
218
211
|
const sorted = [...entries].sort((a, b) => b.location.line - a.location.line);
|
|
219
212
|
for (const entry of sorted) {
|
|
213
|
+
const protectedRanges = protectedTabRanges(lines);
|
|
214
|
+
const start = entry.location.line - 1;
|
|
215
|
+
if (protectedRanges.some((range) => start >= range.start && start < range.end))
|
|
216
|
+
continue;
|
|
217
|
+
const children = protectedRanges.filter((range, index) => range.start > start && range.start < (entry.endLine ?? start) &&
|
|
218
|
+
!protectedRanges.some((outer, other) => other !== index &&
|
|
219
|
+
outer.start < range.start && outer.end >= range.end));
|
|
220
|
+
if (children.length > 0) {
|
|
221
|
+
// Format the parent's prose with each refused child represented by a
|
|
222
|
+
// verbatim block, then restore the original child lines.
|
|
223
|
+
const end = entry.endLine;
|
|
224
|
+
const fragment = lines.slice(start, end);
|
|
225
|
+
const indent = contentColumn(lines[start], entry.location.column - 1);
|
|
226
|
+
const replacements = [];
|
|
227
|
+
for (const child of children.toReversed()) {
|
|
228
|
+
const childEnd = Math.min(child.end, end);
|
|
229
|
+
let token = "<!-- markspec-preserved-child -->";
|
|
230
|
+
while (lines.some((line) => line.includes(token)) ||
|
|
231
|
+
replacements.some((r) => r.token === token))
|
|
232
|
+
token = token.replace(" -->", "- -->");
|
|
233
|
+
replacements.push({
|
|
234
|
+
token,
|
|
235
|
+
original: lines.slice(child.start, childEnd),
|
|
236
|
+
});
|
|
237
|
+
fragment.splice(child.start - start, childEnd - child.start, " ".repeat(indent) + token, "");
|
|
238
|
+
}
|
|
239
|
+
const nestedDiagnostics = [];
|
|
240
|
+
emitBodyViaAst(fragment, file, nestedDiagnostics, undefined, proseFormat);
|
|
241
|
+
for (const { token, original } of replacements) {
|
|
242
|
+
const at = fragment.findIndex((line) => line.trim() === token);
|
|
243
|
+
if (at < 0)
|
|
244
|
+
throw new Error("Preserved child placeholder was lost");
|
|
245
|
+
let after = at + 1;
|
|
246
|
+
while (after < fragment.length && fragment[after].trim() === "") {
|
|
247
|
+
after++;
|
|
248
|
+
}
|
|
249
|
+
fragment.splice(at, after - at, ...original);
|
|
250
|
+
}
|
|
251
|
+
diagnostics.push(...nestedDiagnostics.map((d) => ({
|
|
252
|
+
...d,
|
|
253
|
+
location: d.location
|
|
254
|
+
? { ...d.location, line: d.location.line + start }
|
|
255
|
+
: undefined,
|
|
256
|
+
})));
|
|
257
|
+
if (fragment.join("\n") !== lines.slice(start, end).join("\n")) {
|
|
258
|
+
lines.splice(start, end - start, ...fragment);
|
|
259
|
+
bodyChanged = true;
|
|
260
|
+
}
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
220
263
|
if (!entry.body.trim())
|
|
221
264
|
continue; // empty or whitespace-only body
|
|
222
|
-
|
|
265
|
+
// List-item continuation indent: the item's content column, which
|
|
266
|
+
// counts every space and tab after the marker (#865).
|
|
267
|
+
const indent = contentColumn(lines[entry.location.line - 1] ?? "", entry.location.column - 1);
|
|
223
268
|
const indentStr = " ".repeat(indent);
|
|
224
269
|
const titleLineIdx = entry.location.line - 1; // 0-based
|
|
225
270
|
// Locate the body segment in `lines` — between the title line and
|
|
226
271
|
// the attr block start (or item end when there is no attr block).
|
|
227
|
-
const
|
|
272
|
+
const itemEndFloor = entry.endLine ?? 0;
|
|
273
|
+
const range = findAttributeBlockRange(lines, entry.location.line, indent, itemEndFloor);
|
|
228
274
|
const bodyStart = titleLineIdx + 1;
|
|
229
275
|
const bodyEnd = range
|
|
230
276
|
? range.start
|
|
231
|
-
: findItemEnd(lines, titleLineIdx, indent);
|
|
277
|
+
: findItemEnd(lines, titleLineIdx, indent, itemEndFloor);
|
|
232
278
|
if (bodyEnd <= bodyStart)
|
|
233
279
|
continue; // no body lines to replace
|
|
234
280
|
// Route the body through the canonical AST: build → normalize
|
|
@@ -359,6 +405,97 @@ function stampDisciplineFrozen(value, today) {
|
|
|
359
405
|
}
|
|
360
406
|
return value;
|
|
361
407
|
}
|
|
408
|
+
/**
|
|
409
|
+
* Normalize tab markers before any pass can stamp or splice their entries.
|
|
410
|
+
* Compare whole documents so a neighbouring list cannot become a child.
|
|
411
|
+
*/
|
|
412
|
+
function normalizeTabMarkers(markdown, options, diagnostics) {
|
|
413
|
+
const lines = markdown.split("\n");
|
|
414
|
+
const original = [...lines];
|
|
415
|
+
const markers = tabMarkers(lines).sort((a, b) => b.start - a.start);
|
|
416
|
+
if (markers.length === 0)
|
|
417
|
+
return markdown;
|
|
418
|
+
const accepted = new Set();
|
|
419
|
+
const refused = new Set();
|
|
420
|
+
for (const marker of markers) {
|
|
421
|
+
const before = lines.join("\n");
|
|
422
|
+
const candidate = [...lines];
|
|
423
|
+
const title = lines[marker.start];
|
|
424
|
+
const indent = contentColumn(title, marker.column);
|
|
425
|
+
const delta = indent - (marker.column + 2);
|
|
426
|
+
candidate[marker.start] = title.slice(0, marker.column) + "- " +
|
|
427
|
+
title.slice(marker.column + 1 + marker.whitespace.length);
|
|
428
|
+
for (let i = marker.start + 1; i < marker.end; i++) {
|
|
429
|
+
if (candidate[i].startsWith(" ".repeat(indent))) {
|
|
430
|
+
candidate[i] = candidate[i].slice(delta);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
const after = candidate.join("\n");
|
|
434
|
+
const beforeEntries = parseMarkdown(before).entries;
|
|
435
|
+
const afterEntries = parseMarkdown(after).entries;
|
|
436
|
+
const identitiesKept = beforeEntries.length === afterEntries.length &&
|
|
437
|
+
beforeEntries.every((entry, i) => entry.displayId === afterEntries[i].displayId &&
|
|
438
|
+
entry.id === afterEntries[i].id);
|
|
439
|
+
const containsRefused = [...refused].some((start) => start > marker.start && start < marker.end);
|
|
440
|
+
if (containsRefused || !markdownSemanticallyEquivalent(before, after) ||
|
|
441
|
+
!identitiesKept) {
|
|
442
|
+
// Refusing an enclosing entry must also undo admitted changes to its
|
|
443
|
+
// descendants. No identities have been stamped at this point.
|
|
444
|
+
lines.splice(marker.start, marker.end - marker.start, ...original.slice(marker.start, marker.end));
|
|
445
|
+
for (const start of accepted) {
|
|
446
|
+
if (start >= marker.start && start < marker.end)
|
|
447
|
+
accepted.delete(start);
|
|
448
|
+
}
|
|
449
|
+
refused.add(marker.start);
|
|
450
|
+
diagnostics.push({
|
|
451
|
+
code: "MSL-F013",
|
|
452
|
+
severity: "warning",
|
|
453
|
+
message: "Tab-separated entry marker cannot be normalized without " +
|
|
454
|
+
"changing CommonMark structure; kept the entry unchanged",
|
|
455
|
+
location: {
|
|
456
|
+
file: options?.file ?? "<unknown>",
|
|
457
|
+
line: marker.start + 1,
|
|
458
|
+
column: marker.column + 1,
|
|
459
|
+
},
|
|
460
|
+
});
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
lines.splice(0, lines.length, ...candidate);
|
|
464
|
+
accepted.add(marker.start);
|
|
465
|
+
}
|
|
466
|
+
const indexedStarts = new Set(parseMarkdown(lines.join("\n")).entries.map((entry) => entry.location.line - 1));
|
|
467
|
+
for (const start of [...accepted].sort((a, b) => b - a)) {
|
|
468
|
+
// Re-read extents after each nested splice. Admission itself never
|
|
469
|
+
// changes line counts, so earlier title positions remain valid.
|
|
470
|
+
const marker = entryListItems(lines).find((item) => item.start === start);
|
|
471
|
+
// Nested entries are outside the parser's top-level entry index. Format
|
|
472
|
+
// their normalized item separately, then restore the enclosing indent.
|
|
473
|
+
if (marker.container.start !== marker.start &&
|
|
474
|
+
!indexedStarts.has(marker.container.start)) {
|
|
475
|
+
const prefix = " ".repeat(marker.column);
|
|
476
|
+
const fragment = lines.slice(marker.start, marker.end)
|
|
477
|
+
.map((line) => line.startsWith(prefix) ? line.slice(prefix.length) : line)
|
|
478
|
+
.join("\n");
|
|
479
|
+
const nested = formatEntries(fragment, options);
|
|
480
|
+
const replacement = nested.output.split("\n")
|
|
481
|
+
.map((line) => line === "" ? "" : prefix + line);
|
|
482
|
+
lines.splice(marker.start, marker.end - marker.start, ...replacement);
|
|
483
|
+
diagnostics.push(...nested.diagnostics.map((diagnostic) => ({
|
|
484
|
+
...diagnostic,
|
|
485
|
+
...(diagnostic.location
|
|
486
|
+
? {
|
|
487
|
+
location: {
|
|
488
|
+
...diagnostic.location,
|
|
489
|
+
line: diagnostic.location.line + marker.start,
|
|
490
|
+
column: diagnostic.location.column + marker.column,
|
|
491
|
+
},
|
|
492
|
+
}
|
|
493
|
+
: {}),
|
|
494
|
+
})));
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
return lines.join("\n");
|
|
498
|
+
}
|
|
362
499
|
/**
|
|
363
500
|
* Format a Markdown string — normalize attribute blocks,
|
|
364
501
|
* fix indentation, enforce canonical ordering.
|
|
@@ -368,6 +505,20 @@ function stampDisciplineFrozen(value, today) {
|
|
|
368
505
|
* @returns Format result with output text and diagnostics
|
|
369
506
|
*/
|
|
370
507
|
export function format(markdown, options) {
|
|
508
|
+
const diagnostics = [];
|
|
509
|
+
const ending = detectLineEnding(markdown);
|
|
510
|
+
const prepared = normalizeTabMarkers(normalizeLineEndings(markdown), options, diagnostics);
|
|
511
|
+
const result = formatEntries(prepared, options);
|
|
512
|
+
const output = applyLineEnding(result.output, ending);
|
|
513
|
+
return {
|
|
514
|
+
output,
|
|
515
|
+
diagnostics: [...diagnostics, ...result.diagnostics],
|
|
516
|
+
changed: output !== markdown,
|
|
517
|
+
preservedRanges: protectedTabRanges(normalizeLineEndings(output).split("\n")),
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
/** Run the entry and prose passes after tab-marker admission. */
|
|
521
|
+
function formatEntries(markdown, options) {
|
|
371
522
|
const file = options?.file ?? "<unknown>";
|
|
372
523
|
// Detect the source file's line-ending convention up front so we can
|
|
373
524
|
// restore it on write-back. The rest of the formatter operates on a
|
|
@@ -386,13 +537,16 @@ export function format(markdown, options) {
|
|
|
386
537
|
// `normalizeBodyAst` (more spec-correct: respects §2.5 verbatim
|
|
387
538
|
// boundaries and per-node sentence-initial context). `normalizeModalKeywords`
|
|
388
539
|
// stays exported/defined for other callers/tests but is not invoked here.
|
|
389
|
-
const body = rawBody;
|
|
390
|
-
const { entries } = parseMarkdown(body, { file });
|
|
391
540
|
const diagnostics = [...fm.diagnostics];
|
|
541
|
+
// Drop empty-valued trailer lines (`Id:`, `Satisfies:`) before the main
|
|
542
|
+
// pass; left in place they demote the whole trailer block to body code.
|
|
543
|
+
const repaired = removeEmptyTrailerLines(rawBody, parseMarkdown(rawBody, { file }).entries, diagnostics);
|
|
544
|
+
const body = repaired ?? rawBody;
|
|
545
|
+
const { entries } = parseMarkdown(body, { file });
|
|
392
546
|
// ADR-029: the whole-document Markdown pass is Markdown-only. Gate on the
|
|
393
547
|
// file extension HERE so the invariant has one home — a caller that wires
|
|
394
|
-
// formatMarkdownProse without its own guard (e.g. a future write tool
|
|
395
|
-
//
|
|
548
|
+
// formatMarkdownProse without its own guard (e.g. a future write tool
|
|
549
|
+
// forwarding options with a source-file label) cannot silently
|
|
396
550
|
// reflow a source file's doc comment as Markdown. The CommonMark-semantic
|
|
397
551
|
// gate cannot catch that class, so it must be prevented, not detected. The
|
|
398
552
|
// CLI/LSP call-site guards remain as defense-in-depth.
|
|
@@ -405,13 +559,31 @@ export function format(markdown, options) {
|
|
|
405
559
|
return { output: markdown, diagnostics, changed: false };
|
|
406
560
|
}
|
|
407
561
|
const lines = body.split("\n");
|
|
408
|
-
let changed =
|
|
562
|
+
let changed = repaired !== undefined;
|
|
409
563
|
// Process bottom-to-top so line splicing doesn't shift earlier entries.
|
|
410
564
|
const sorted = [...entries].sort((a, b) => b.location.line - a.location.line);
|
|
411
565
|
const genUlid = options?.generateUlid ?? defaultUlid;
|
|
566
|
+
const protectedRanges = protectedTabRanges(lines);
|
|
412
567
|
for (const entry of sorted) {
|
|
413
|
-
|
|
414
|
-
|
|
568
|
+
if (protectedRanges.some((range) => entry.location.line - 1 >= range.start &&
|
|
569
|
+
entry.location.line - 1 < range.end))
|
|
570
|
+
continue;
|
|
571
|
+
const indent = contentColumn(lines[entry.location.line - 1] ?? "", entry.location.column - 1);
|
|
572
|
+
// `entry.endLine` comes from the parse of the unedited buffer. Entries
|
|
573
|
+
// are processed bottom to top, so only this entry's own edits move its
|
|
574
|
+
// end; the change in line count since here measures them.
|
|
575
|
+
const lengthAtStart = lines.length;
|
|
576
|
+
const itemEndFloor = () => entry.endLine === undefined
|
|
577
|
+
? 0
|
|
578
|
+
: entry.endLine + (lines.length - lengthAtStart);
|
|
579
|
+
const existingTrailer = protectedRanges.length > 0
|
|
580
|
+
? findAttributeBlockRange(lines, entry.location.line, indent, itemEndFloor(), protectedRanges)
|
|
581
|
+
: undefined;
|
|
582
|
+
let attrs = protectedRanges.length > 0
|
|
583
|
+
? existingTrailer
|
|
584
|
+
? parseAttributes(lines.slice(existingTrailer.start, existingTrailer.end))
|
|
585
|
+
: []
|
|
586
|
+
: [...entry.rawAttributes];
|
|
415
587
|
const titleLineIdx = entry.location.line - 1;
|
|
416
588
|
// #686: a title that soft-wraps onto extra physical lines is a single
|
|
417
589
|
// Markdown paragraph, but the line-based passes here and in
|
|
@@ -425,7 +597,7 @@ export function format(markdown, options) {
|
|
|
425
597
|
// attribute block so a malformed entry with no blank line before its
|
|
426
598
|
// trailer cannot swallow it.
|
|
427
599
|
if (titleLineIdx >= 0 && titleLineIdx < lines.length) {
|
|
428
|
-
const attrRange = findAttributeBlockRange(lines, entry.location.line, indent);
|
|
600
|
+
const attrRange = findAttributeBlockRange(lines, entry.location.line, indent, itemEndFloor());
|
|
429
601
|
const limit = attrRange ? attrRange.start : lines.length;
|
|
430
602
|
let titleEnd = titleLineIdx;
|
|
431
603
|
while (titleEnd + 1 < limit && lines[titleEnd + 1].trim() !== "") {
|
|
@@ -507,7 +679,7 @@ export function format(markdown, options) {
|
|
|
507
679
|
if (attrs.length === 0)
|
|
508
680
|
continue;
|
|
509
681
|
const normalized = sortAttributes(expandCsvValues(attrs));
|
|
510
|
-
const range = findAttributeBlockRange(lines, entry.location.line, indent);
|
|
682
|
+
const range = findAttributeBlockRange(lines, entry.location.line, indent, itemEndFloor(), protectedRanges);
|
|
511
683
|
if (range) {
|
|
512
684
|
// Replace existing attribute block.
|
|
513
685
|
const newBlock = renderAttributeBlock(normalized, indent);
|
|
@@ -519,7 +691,7 @@ export function format(markdown, options) {
|
|
|
519
691
|
}
|
|
520
692
|
else {
|
|
521
693
|
// No attribute block — insert one after the entry body.
|
|
522
|
-
const insertLine = findEntryBodyEnd(lines, entry, indent);
|
|
694
|
+
const insertLine = findEntryBodyEnd(lines, entry, indent, itemEndFloor());
|
|
523
695
|
const newBlock = renderAttributeBlock(normalized, indent);
|
|
524
696
|
lines.splice(insertLine, 0, "", ...newBlock.split("\n"));
|
|
525
697
|
changed = true;
|
|
@@ -560,10 +732,32 @@ export function format(markdown, options) {
|
|
|
560
732
|
: entries;
|
|
561
733
|
const extents = proseEntries.map((e) => {
|
|
562
734
|
const start = e.location.line - 1;
|
|
563
|
-
const entryIndent = (e.location.column - 1)
|
|
564
|
-
return {
|
|
735
|
+
const entryIndent = contentColumn(collapsedLines[start] ?? "", e.location.column - 1);
|
|
736
|
+
return {
|
|
737
|
+
start,
|
|
738
|
+
end: findItemEnd(collapsedLines, start, entryIndent, e.endLine ?? 0),
|
|
739
|
+
};
|
|
565
740
|
});
|
|
566
|
-
|
|
741
|
+
if (tabMarkers(collapsedLines).length > 0) {
|
|
742
|
+
// Title-only entry items are outside the entry index. Keep their list
|
|
743
|
+
// context too: isolated prose formatting can move adjacent entries.
|
|
744
|
+
const indexedStarts = new Set(proseEntries.map((entry) => entry.location.line - 1));
|
|
745
|
+
extents.push(...entryListItems(collapsedLines)
|
|
746
|
+
.filter((item) => !indexedStarts.has(item.start))
|
|
747
|
+
.map((item) => item.listContext));
|
|
748
|
+
extents.push(...protectedTabRanges(collapsedLines, true));
|
|
749
|
+
}
|
|
750
|
+
extents.sort((a, b) => a.start - b.start);
|
|
751
|
+
const merged = [];
|
|
752
|
+
for (const extent of extents) {
|
|
753
|
+
const previous = merged.at(-1);
|
|
754
|
+
if (previous && extent.start <= previous.end) {
|
|
755
|
+
previous.end = Math.max(previous.end, extent.end);
|
|
756
|
+
}
|
|
757
|
+
else
|
|
758
|
+
merged.push({ ...extent });
|
|
759
|
+
}
|
|
760
|
+
const prose = formatProseSegments(collapsedLines, merged, proseFormat);
|
|
567
761
|
if (prose.changed)
|
|
568
762
|
changed = true;
|
|
569
763
|
for (const fallback of prose.fallbacks) {
|
|
@@ -626,16 +820,12 @@ function renderFrontMatter(attrs) {
|
|
|
626
820
|
*/
|
|
627
821
|
function collapseBlankLines(lines) {
|
|
628
822
|
const out = [];
|
|
629
|
-
|
|
823
|
+
const fencedLines = fencedCodeLines(lines.join("\n"));
|
|
824
|
+
const protectedRanges = protectedTabRanges(lines, true);
|
|
630
825
|
let prevBlank = false;
|
|
631
|
-
for (const line of lines) {
|
|
632
|
-
if (
|
|
633
|
-
|
|
634
|
-
inFence = !inFence;
|
|
635
|
-
prevBlank = false;
|
|
636
|
-
continue;
|
|
637
|
-
}
|
|
638
|
-
if (inFence) {
|
|
826
|
+
for (const [i, line] of lines.entries()) {
|
|
827
|
+
if (fencedLines.has(i) ||
|
|
828
|
+
protectedRanges.some((range) => i >= range.start && i < range.end)) {
|
|
639
829
|
out.push(line);
|
|
640
830
|
prevBlank = false;
|
|
641
831
|
continue;
|
|
@@ -732,8 +922,8 @@ export function sortAttributes(attributes) {
|
|
|
732
922
|
* no trailing line-continuation characters.
|
|
733
923
|
*
|
|
734
924
|
* @param attributes - The attributes to render, in canonical order.
|
|
735
|
-
* @param indent -
|
|
736
|
-
*
|
|
925
|
+
* @param indent - The 0-based list item content column, or 0 inside
|
|
926
|
+
* source-file doc comments.
|
|
737
927
|
*/
|
|
738
928
|
export function renderAttributeBlock(attributes, indent) {
|
|
739
929
|
const prefix = " ".repeat(indent + 4);
|
|
@@ -744,18 +934,26 @@ export function renderAttributeBlock(attributes, indent) {
|
|
|
744
934
|
/**
|
|
745
935
|
* Find the 0-based line index where a list item's content ends.
|
|
746
936
|
* Exported for the ADR-029 prose pass (entry-extent computation).
|
|
747
|
-
* Scans forward from the
|
|
748
|
-
*
|
|
937
|
+
* Scans forward from the later of the line after the entry start and
|
|
938
|
+
* `itemEndFloor` until a line has less indent than the content column, a
|
|
939
|
+
* conventional sibling marker is found, or EOF. Pass the parser's
|
|
940
|
+
* `Entry.endLine` as `itemEndFloor` so that a lazy continuation line
|
|
941
|
+
* inside the item does not end the scan early; the default `0` keeps
|
|
942
|
+
* the indentation-only scan.
|
|
749
943
|
*/
|
|
750
|
-
export function findItemEnd(lines, startIdx, indent) {
|
|
944
|
+
export function findItemEnd(lines, startIdx, indent, itemEndFloor = 0) {
|
|
751
945
|
const indentStr = " ".repeat(indent);
|
|
752
|
-
//
|
|
946
|
+
// Recognize sibling bullets with one space after the marker.
|
|
753
947
|
const markerPrefix = " ".repeat(Math.max(0, indent - 2)) + "- ";
|
|
754
|
-
|
|
948
|
+
// `itemEndFloor` is the 0-based index just after the item's last content
|
|
949
|
+
// line, from the parser's `Entry.endLine`. The scan starts there, because
|
|
950
|
+
// an indentation scan ends the item early at a lazy continuation line
|
|
951
|
+
// (a paragraph line indented less than the item content).
|
|
952
|
+
for (let i = Math.max(startIdx + 1, itemEndFloor); i < lines.length; i++) {
|
|
755
953
|
const line = lines[i];
|
|
756
954
|
if (line.trim() === "")
|
|
757
955
|
continue;
|
|
758
|
-
// Sibling
|
|
956
|
+
// Sibling with a one-space marker; wider markers use the indent check.
|
|
759
957
|
if (line.startsWith(markerPrefix))
|
|
760
958
|
return i;
|
|
761
959
|
// Line with less indent than continuation
|
|
@@ -764,6 +962,79 @@ export function findItemEnd(lines, startIdx, indent) {
|
|
|
764
962
|
}
|
|
765
963
|
return lines.length;
|
|
766
964
|
}
|
|
965
|
+
/** A trailer-shaped line whose value may be empty: `Key:` or `Key: value`. */
|
|
966
|
+
const TRAILER_SHAPED_RE = /^[A-Z][A-Za-z-]*:(?:\s.*)?$/;
|
|
967
|
+
/** A trailer-shaped line with no value: `Key:` (spec §2 makes it MSL-P021). */
|
|
968
|
+
const EMPTY_TRAILER_RE = /^([A-Z][A-Za-z-]*):\s*$/;
|
|
969
|
+
/**
|
|
970
|
+
* Remove empty-valued `Key:` lines from each entry's trailing trailer block.
|
|
971
|
+
*
|
|
972
|
+
* An empty-valued line is not a valid trailer (spec §2, MSL-P021), so the
|
|
973
|
+
* parser treats the whole trailing block as body code. Left alone, the
|
|
974
|
+
* main pass would fence that block and append a fresh `Id:`, silently
|
|
975
|
+
* turning real trace links into inert body text. An empty value carries no
|
|
976
|
+
* information, so dropping the line is lossless: an `Id:` placeholder then
|
|
977
|
+
* gets stamped in place and the remaining trailers stay trailers.
|
|
978
|
+
*
|
|
979
|
+
* Only touches a block of contiguous lines at the very end of an entry,
|
|
980
|
+
* indented at least four columns past the entry body, in which every line
|
|
981
|
+
* is trailer-shaped. Emits one MSL-F001 info diagnostic per removed line.
|
|
982
|
+
*
|
|
983
|
+
* @returns The repaired text, or `undefined` when nothing was removed.
|
|
984
|
+
*/
|
|
985
|
+
function removeEmptyTrailerLines(body, entries, diagnostics) {
|
|
986
|
+
const lines = body.split("\n");
|
|
987
|
+
let removedAny = false;
|
|
988
|
+
const sorted = [...entries].sort((a, b) => b.location.line - a.location.line);
|
|
989
|
+
const protectedRanges = protectedTabRanges(lines);
|
|
990
|
+
for (const entry of sorted) {
|
|
991
|
+
if (protectedRanges.some((range) => entry.location.line - 1 >= range.start &&
|
|
992
|
+
entry.location.line - 1 < range.end))
|
|
993
|
+
continue;
|
|
994
|
+
const indent = contentColumn(lines[entry.location.line - 1] ?? "", entry.location.column - 1);
|
|
995
|
+
const startIdx = entry.location.line - 1;
|
|
996
|
+
let end = findItemEnd(lines, startIdx, indent, entry.endLine ?? 0);
|
|
997
|
+
while (end > startIdx && lines[end - 1].trim() === "")
|
|
998
|
+
end--;
|
|
999
|
+
let start = end;
|
|
1000
|
+
while (start - 1 > startIdx && lines[start - 1].trim() !== "" &&
|
|
1001
|
+
!protectedRanges.some((range) => start - 1 >= range.start && start - 1 < range.end))
|
|
1002
|
+
start--;
|
|
1003
|
+
if (start >= end || start - 1 <= startIdx)
|
|
1004
|
+
continue;
|
|
1005
|
+
const trailerIndent = " ".repeat(indent + 4);
|
|
1006
|
+
const block = lines.slice(start, end);
|
|
1007
|
+
if (protectedRanges.some((range) => range.start > startIdx && start < range.end && end > range.start))
|
|
1008
|
+
continue;
|
|
1009
|
+
const isTrailerBlock = block.every((l) => l.startsWith(trailerIndent) && TRAILER_SHAPED_RE.test(l.trim()));
|
|
1010
|
+
if (!isTrailerBlock)
|
|
1011
|
+
continue;
|
|
1012
|
+
const kept = block.filter((l) => !EMPTY_TRAILER_RE.test(l.trim()));
|
|
1013
|
+
if (kept.length === block.length)
|
|
1014
|
+
continue;
|
|
1015
|
+
for (const l of block) {
|
|
1016
|
+
const key = EMPTY_TRAILER_RE.exec(l.trim())?.[1];
|
|
1017
|
+
if (key === undefined)
|
|
1018
|
+
continue;
|
|
1019
|
+
diagnostics.push({
|
|
1020
|
+
code: "MSL-F001",
|
|
1021
|
+
severity: "info",
|
|
1022
|
+
message: `removed empty '${key}:' trailer line from ${entry.displayId}`,
|
|
1023
|
+
location: entry.location,
|
|
1024
|
+
});
|
|
1025
|
+
}
|
|
1026
|
+
if (kept.length > 0) {
|
|
1027
|
+
lines.splice(start, end - start, ...kept);
|
|
1028
|
+
}
|
|
1029
|
+
else {
|
|
1030
|
+
// Whole block was placeholders: drop it and its leading blank line.
|
|
1031
|
+
const from = lines[start - 1].trim() === "" ? start - 1 : start;
|
|
1032
|
+
lines.splice(from, end - from);
|
|
1033
|
+
}
|
|
1034
|
+
removedAny = true;
|
|
1035
|
+
}
|
|
1036
|
+
return removedAny ? lines.join("\n") : undefined;
|
|
1037
|
+
}
|
|
767
1038
|
/**
|
|
768
1039
|
* Find the 0-based line range [start, end) of the attribute block
|
|
769
1040
|
* for an entry starting at the given line.
|
|
@@ -771,9 +1042,9 @@ export function findItemEnd(lines, startIdx, indent) {
|
|
|
771
1042
|
* Scans forward from the entry start to find the list item boundary,
|
|
772
1043
|
* then walks backwards to find the contiguous trailing attribute block.
|
|
773
1044
|
*/
|
|
774
|
-
export function findAttributeBlockRange(lines, entryStartLine, indent) {
|
|
1045
|
+
export function findAttributeBlockRange(lines, entryStartLine, indent, itemEndFloor = 0, preservedRanges = []) {
|
|
775
1046
|
const startIdx = entryStartLine - 1;
|
|
776
|
-
const itemEnd = findItemEnd(lines, startIdx, indent);
|
|
1047
|
+
const itemEnd = findItemEnd(lines, startIdx, indent, itemEndFloor);
|
|
777
1048
|
// Walk backwards from itemEnd, skip trailing blank lines.
|
|
778
1049
|
let scanEnd = itemEnd;
|
|
779
1050
|
while (scanEnd > startIdx && lines[scanEnd - 1].trim() === "") {
|
|
@@ -784,6 +1055,9 @@ export function findAttributeBlockRange(lines, entryStartLine, indent) {
|
|
|
784
1055
|
// Walk backwards collecting attribute lines.
|
|
785
1056
|
let attrStart = scanEnd;
|
|
786
1057
|
for (let i = scanEnd - 1; i > startIdx; i--) {
|
|
1058
|
+
if (preservedRanges.some((range) => i >= range.start && i < range.end)) {
|
|
1059
|
+
break;
|
|
1060
|
+
}
|
|
787
1061
|
const trimmed = lines[i].trim();
|
|
788
1062
|
if (trimmed === "")
|
|
789
1063
|
break;
|
|
@@ -802,9 +1076,9 @@ export function findAttributeBlockRange(lines, entryStartLine, indent) {
|
|
|
802
1076
|
* Find the 0-based line index where a new attribute block should be inserted
|
|
803
1077
|
* (after the last non-blank body line of the entry).
|
|
804
1078
|
*/
|
|
805
|
-
function findEntryBodyEnd(lines, entry, indent) {
|
|
1079
|
+
function findEntryBodyEnd(lines, entry, indent, itemEndFloor = 0) {
|
|
806
1080
|
const startIdx = entry.location.line - 1;
|
|
807
|
-
const itemEnd = findItemEnd(lines, startIdx, indent);
|
|
1081
|
+
const itemEnd = findItemEnd(lines, startIdx, indent, itemEndFloor);
|
|
808
1082
|
let insertAt = itemEnd;
|
|
809
1083
|
while (insertAt > startIdx && lines[insertAt - 1].trim() === "") {
|
|
810
1084
|
insertAt--;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { EntryExtent } from "./prose.js";
|
|
2
|
+
/** An entry title and the list context that controls its indentation. */
|
|
3
|
+
export interface EntryListItem extends EntryExtent {
|
|
4
|
+
readonly column: number;
|
|
5
|
+
readonly whitespace: string;
|
|
6
|
+
readonly container: EntryExtent;
|
|
7
|
+
readonly listContext: EntryExtent;
|
|
8
|
+
}
|
|
9
|
+
/** Find actual unordered list items, excluding code, HTML, and task lists. */
|
|
10
|
+
export declare function entryListItems(lines: readonly string[]): EntryListItem[];
|
|
11
|
+
/** Find entry list items with a tab in the whitespace after the marker. */
|
|
12
|
+
export declare function tabMarkers(lines: readonly string[]): EntryListItem[];
|
|
13
|
+
/** Ranges kept out of every formatter pass after a tab rewrite is refused. */
|
|
14
|
+
export declare function protectedTabRanges(lines: readonly string[], preserveListContext?: boolean): EntryExtent[];
|
|
15
|
+
//# sourceMappingURL=tab_markers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tab_markers.d.ts","sourceRoot":"","sources":["../../src/formatter/tab_markers.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAI9C,yEAAyE;AACzE,MAAM,WAAW,aAAc,SAAQ,WAAW;IAChD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,SAAS,EAAE,WAAW,CAAC;IAChC,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC;CACnC;AAED,8EAA8E;AAC9E,wBAAgB,cAAc,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,aAAa,EAAE,CAoFxE;AAED,2EAA2E;AAC3E,wBAAgB,UAAU,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,aAAa,EAAE,CAKpE;AAED,8EAA8E;AAC9E,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,SAAS,MAAM,EAAE,EACxB,mBAAmB,UAAQ,GAC1B,WAAW,EAAE,CA4Cf"}
|