@mintlify/cli 4.0.1315 → 4.0.1316
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/__test__/deslop/client.test.ts +167 -0
- package/__test__/deslop/resolveFiles.test.ts +183 -0
- package/__test__/deslop/whitespace.test.ts +94 -0
- package/bin/cli.js +38 -0
- package/bin/deslop/client.js +74 -0
- package/bin/deslop/index.js +242 -0
- package/bin/deslop/resolveFiles.js +186 -0
- package/bin/deslop/types.js +1 -0
- package/bin/deslop/whitespace.js +144 -0
- package/bin/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/cli.tsx +50 -0
- package/src/deslop/client.ts +77 -0
- package/src/deslop/index.tsx +286 -0
- package/src/deslop/resolveFiles.ts +176 -0
- package/src/deslop/types.ts +39 -0
- package/src/deslop/whitespace.ts +155 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
export interface WhitespaceReport {
|
|
2
|
+
fixed: string;
|
|
3
|
+
changed: boolean;
|
|
4
|
+
counts: {
|
|
5
|
+
trailing: number;
|
|
6
|
+
blankRuns: number;
|
|
7
|
+
unicodeSpaces: number;
|
|
8
|
+
zeroWidth: number;
|
|
9
|
+
spaces: number;
|
|
10
|
+
finalNewline: boolean;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Unusual space characters that should read as a normal space.
|
|
15
|
+
const UNICODE_SPACES = /[\u00A0\u2000-\u200A\u202F\u205F\u3000]/g;
|
|
16
|
+
// Zero-width and BOM characters that should be removed outright.
|
|
17
|
+
const ZERO_WIDTH = /[\u200B-\u200D\u2060\uFEFF]/g;
|
|
18
|
+
|
|
19
|
+
const FENCE = /^(\s*)(`{3,}|~{3,})/;
|
|
20
|
+
// Splits a line so odd indices are inline-code spans (left untouched).
|
|
21
|
+
const INLINE_CODE = /(`+[^`]*`+)/;
|
|
22
|
+
|
|
23
|
+
function isFrontmatterFence(line: string, index: number): boolean {
|
|
24
|
+
return index === 0 && line.trim() === '---';
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Normalize prose whitespace in an MDX document without touching the bytes of
|
|
29
|
+
* fenced code blocks or YAML frontmatter, where spacing is significant.
|
|
30
|
+
*/
|
|
31
|
+
export function normalizeWhitespace(content: string): WhitespaceReport {
|
|
32
|
+
const counts = {
|
|
33
|
+
trailing: 0,
|
|
34
|
+
blankRuns: 0,
|
|
35
|
+
unicodeSpaces: 0,
|
|
36
|
+
zeroWidth: 0,
|
|
37
|
+
spaces: 0,
|
|
38
|
+
finalNewline: false,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const lines = content.split('\n');
|
|
42
|
+
const out: string[] = [];
|
|
43
|
+
const isProtected: boolean[] = [];
|
|
44
|
+
let inFence = false;
|
|
45
|
+
let fenceMarker = '';
|
|
46
|
+
let inFrontmatter = false;
|
|
47
|
+
|
|
48
|
+
const pushProtected = (line: string) => {
|
|
49
|
+
out.push(line);
|
|
50
|
+
isProtected.push(true);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
for (const [index, rawLine] of lines.entries()) {
|
|
54
|
+
if (isFrontmatterFence(rawLine, index)) {
|
|
55
|
+
inFrontmatter = true;
|
|
56
|
+
pushProtected(rawLine);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (inFrontmatter) {
|
|
60
|
+
pushProtected(rawLine);
|
|
61
|
+
if (rawLine.trim() === '---') inFrontmatter = false;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const fence = rawLine.match(FENCE);
|
|
66
|
+
if (fence) {
|
|
67
|
+
const marker = fence[2] ?? '';
|
|
68
|
+
if (!inFence) {
|
|
69
|
+
inFence = true;
|
|
70
|
+
fenceMarker = marker;
|
|
71
|
+
} else if (marker[0] === fenceMarker[0] && marker.length >= fenceMarker.length) {
|
|
72
|
+
inFence = false;
|
|
73
|
+
}
|
|
74
|
+
pushProtected(rawLine);
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (inFence) {
|
|
78
|
+
pushProtected(rawLine);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let line = rawLine;
|
|
83
|
+
// Separate a trailing CR so CRLF files still get their trailing whitespace
|
|
84
|
+
// stripped (and keep the CR so line endings are preserved).
|
|
85
|
+
const cr = line.endsWith('\r') ? '\r' : '';
|
|
86
|
+
if (cr) line = line.slice(0, -1);
|
|
87
|
+
|
|
88
|
+
// Apply substitutions only outside inline-code spans (odd split indices),
|
|
89
|
+
// so backticked code keeps its exact spacing.
|
|
90
|
+
line = line
|
|
91
|
+
.split(INLINE_CODE)
|
|
92
|
+
.map((seg, i) => {
|
|
93
|
+
if (i % 2 === 1) return seg;
|
|
94
|
+
const uni = seg.match(UNICODE_SPACES);
|
|
95
|
+
if (uni) counts.unicodeSpaces += uni.length;
|
|
96
|
+
seg = seg.replace(UNICODE_SPACES, ' ');
|
|
97
|
+
const zw = seg.match(ZERO_WIDTH);
|
|
98
|
+
if (zw) counts.zeroWidth += zw.length;
|
|
99
|
+
return seg.replace(ZERO_WIDTH, '');
|
|
100
|
+
})
|
|
101
|
+
.join('');
|
|
102
|
+
|
|
103
|
+
const trimmed = line.replace(/[ \t]+$/, '');
|
|
104
|
+
if (trimmed !== line) counts.trailing += 1;
|
|
105
|
+
line = trimmed;
|
|
106
|
+
|
|
107
|
+
// Collapse runs of internal spaces (outside inline code), preserving leading indentation.
|
|
108
|
+
const leading = line.match(/^[ \t]*/)?.[0] ?? '';
|
|
109
|
+
const body = line.slice(leading.length);
|
|
110
|
+
const collapsed = body
|
|
111
|
+
.split(INLINE_CODE)
|
|
112
|
+
.map((seg, i) => (i % 2 === 1 ? seg : seg.replace(/ {2,}/g, ' ')))
|
|
113
|
+
.join('');
|
|
114
|
+
if (collapsed !== body) counts.spaces += 1;
|
|
115
|
+
line = leading + collapsed + cr;
|
|
116
|
+
|
|
117
|
+
out.push(line);
|
|
118
|
+
isProtected.push(false);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Collapse runs of 2 or more consecutive blank lines down to a single blank
|
|
122
|
+
// line, but never inside protected regions (code fences, frontmatter).
|
|
123
|
+
const collapsed: string[] = [];
|
|
124
|
+
let blankRun = 0;
|
|
125
|
+
for (const [index, line] of out.entries()) {
|
|
126
|
+
if (!isProtected[index] && line.trim() === '') {
|
|
127
|
+
blankRun += 1;
|
|
128
|
+
if (blankRun <= 1) collapsed.push(line);
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (blankRun > 1) counts.blankRuns += 1;
|
|
132
|
+
blankRun = 0;
|
|
133
|
+
collapsed.push(line);
|
|
134
|
+
}
|
|
135
|
+
if (blankRun > 1) counts.blankRuns += 1;
|
|
136
|
+
|
|
137
|
+
let fixed = collapsed.join('\n').replace(/\n+$/, '');
|
|
138
|
+
if (content.length > 0) {
|
|
139
|
+
counts.finalNewline = !content.endsWith('\n') || content.endsWith('\n\n');
|
|
140
|
+
fixed += '\n';
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return { fixed, changed: fixed !== content, counts };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function summarizeWhitespace(counts: WhitespaceReport['counts']): string | null {
|
|
147
|
+
const parts: string[] = [];
|
|
148
|
+
if (counts.trailing) parts.push(`${counts.trailing} trailing`);
|
|
149
|
+
if (counts.blankRuns) parts.push(`${counts.blankRuns} blank-line runs`);
|
|
150
|
+
if (counts.unicodeSpaces) parts.push(`${counts.unicodeSpaces} unicode spaces`);
|
|
151
|
+
if (counts.zeroWidth) parts.push(`${counts.zeroWidth} zero-width chars`);
|
|
152
|
+
if (counts.spaces) parts.push(`${counts.spaces} collapsed spaces`);
|
|
153
|
+
if (counts.finalNewline) parts.push('final newline');
|
|
154
|
+
return parts.length ? parts.join(', ') : null;
|
|
155
|
+
}
|