@mintlify/cli 4.0.1812 → 4.1.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.
Files changed (274) hide show
  1. package/dist/assets/export-scripts/README.classic.md +15 -0
  2. package/dist/assets/export-scripts/README.md +14 -0
  3. package/dist/assets/export-scripts/serve.js +114 -0
  4. package/dist/assets/export-scripts/start-docs.bat +10 -0
  5. package/dist/assets/export-scripts/start-docs.sh +9 -0
  6. package/dist/assets/og/Inter-Bold.ttf +0 -0
  7. package/dist/assets/og/Inter-Medium.ttf +0 -0
  8. package/dist/assets/og/Inter-Regular.ttf +0 -0
  9. package/dist/assets/og/Inter-SemiBold.ttf +0 -0
  10. package/dist/assets/og/PaperMono-Regular.ttf +0 -0
  11. package/dist/assets/og/index_bg.wasm +0 -0
  12. package/dist/assets/openChrome.applescript +94 -0
  13. package/dist/assets/socket.io/socket.io.esm.min.js +7 -0
  14. package/dist/assets/socket.io/socket.io.esm.min.js.map +1 -0
  15. package/dist/assets/socket.io/socket.io.js +4907 -0
  16. package/dist/assets/socket.io/socket.io.js.map +1 -0
  17. package/dist/assets/socket.io/socket.io.min.js +7 -0
  18. package/dist/assets/socket.io/socket.io.min.js.map +1 -0
  19. package/dist/assets/socket.io/socket.io.msgpack.min.js +7 -0
  20. package/dist/assets/socket.io/socket.io.msgpack.min.js.map +1 -0
  21. package/dist/assets/xdg-open +1066 -0
  22. package/dist/assets/yargs-locales/be.json +46 -0
  23. package/dist/assets/yargs-locales/cs.json +51 -0
  24. package/dist/assets/yargs-locales/de.json +46 -0
  25. package/dist/assets/yargs-locales/en.json +55 -0
  26. package/dist/assets/yargs-locales/es.json +46 -0
  27. package/dist/assets/yargs-locales/fi.json +49 -0
  28. package/dist/assets/yargs-locales/fr.json +53 -0
  29. package/dist/assets/yargs-locales/hi.json +49 -0
  30. package/dist/assets/yargs-locales/hu.json +46 -0
  31. package/dist/assets/yargs-locales/id.json +50 -0
  32. package/dist/assets/yargs-locales/it.json +46 -0
  33. package/dist/assets/yargs-locales/ja.json +51 -0
  34. package/dist/assets/yargs-locales/ko.json +49 -0
  35. package/dist/assets/yargs-locales/nb.json +44 -0
  36. package/dist/assets/yargs-locales/nl.json +49 -0
  37. package/dist/assets/yargs-locales/nn.json +44 -0
  38. package/dist/assets/yargs-locales/pirate.json +13 -0
  39. package/dist/assets/yargs-locales/pl.json +49 -0
  40. package/dist/assets/yargs-locales/pt.json +45 -0
  41. package/dist/assets/yargs-locales/pt_BR.json +48 -0
  42. package/dist/assets/yargs-locales/ru.json +51 -0
  43. package/dist/assets/yargs-locales/th.json +46 -0
  44. package/dist/assets/yargs-locales/tr.json +48 -0
  45. package/dist/assets/yargs-locales/uk_UA.json +51 -0
  46. package/dist/assets/yargs-locales/uz.json +52 -0
  47. package/dist/assets/yargs-locales/zh_CN.json +48 -0
  48. package/dist/assets/yargs-locales/zh_TW.json +51 -0
  49. package/dist/converter-d8O3xkdU.js +102 -0
  50. package/dist/devtools-CGNZQKFV.js +24 -0
  51. package/dist/index.js +8 -0
  52. package/dist/rolldown-runtime-CEAe2Uac.js +7 -0
  53. package/dist/schemas-CBsmCNpK.js +372 -0
  54. package/dist/start.js +2310 -0
  55. package/package.json +43 -36
  56. package/.oxlintrc.json +0 -5
  57. package/.prettierignore +0 -2
  58. package/.turbo/turbo-build.log +0 -1
  59. package/CONTRIBUTING.md +0 -45
  60. package/__test__/agent-auth.test.ts +0 -114
  61. package/__test__/agent-claim.test.ts +0 -164
  62. package/__test__/agent-credentials.test.ts +0 -149
  63. package/__test__/agent-deployment.test.ts +0 -106
  64. package/__test__/agent-setup-config.test.ts +0 -324
  65. package/__test__/agent-setup.test.ts +0 -207
  66. package/__test__/agent.test.ts +0 -64
  67. package/__test__/analytics/client.test.ts +0 -180
  68. package/__test__/analytics/dates.test.ts +0 -12
  69. package/__test__/analytics/filters.test.ts +0 -116
  70. package/__test__/analytics/format.test.ts +0 -137
  71. package/__test__/authenticatedFetch.test.ts +0 -182
  72. package/__test__/brokenLinks.test.ts +0 -283
  73. package/__test__/checkPort.test.ts +0 -92
  74. package/__test__/deploy-github-actions.test.ts +0 -202
  75. package/__test__/deploy-github-setup.test.ts +0 -240
  76. package/__test__/deploy-instructions.test.ts +0 -155
  77. package/__test__/deploy.test.ts +0 -202
  78. package/__test__/feedback.test.ts +0 -34
  79. package/__test__/foreignSyntax.test.ts +0 -73
  80. package/__test__/format.test.ts +0 -72
  81. package/__test__/helpers.test.ts +0 -52
  82. package/__test__/init.test.ts +0 -119
  83. package/__test__/instructions.test.ts +0 -53
  84. package/__test__/keyring.test.ts +0 -82
  85. package/__test__/mdxAccessibility.test.ts +0 -81
  86. package/__test__/migrate.test.ts +0 -186
  87. package/__test__/openApiCheck.test.ts +0 -185
  88. package/__test__/pathValidation.test.ts +0 -25
  89. package/__test__/resolveFiles.test.ts +0 -70
  90. package/__test__/sessionId.test.ts +0 -46
  91. package/__test__/signup.test.ts +0 -36
  92. package/__test__/update.test.ts +0 -117
  93. package/__test__/utils.ts +0 -25
  94. package/__test__/validateCommand.test.ts +0 -647
  95. package/__test__/validateEngine.test.ts +0 -245
  96. package/__test__/validateRules.test.ts +0 -1275
  97. package/__test__/welcome.test.ts +0 -15
  98. package/__test__/workflows/client.test.ts +0 -126
  99. package/bin/accessibility.js +0 -111
  100. package/bin/accessibilityCheck.js +0 -82
  101. package/bin/addDomain.js +0 -130
  102. package/bin/agent-auth.js +0 -104
  103. package/bin/agent-claim.js +0 -97
  104. package/bin/agent-credentials.js +0 -124
  105. package/bin/agent-deployment.js +0 -96
  106. package/bin/agent-http.js +0 -45
  107. package/bin/agent-setup-config.js +0 -141
  108. package/bin/agent-setup.js +0 -146
  109. package/bin/agent.js +0 -42
  110. package/bin/analytics/client.js +0 -60
  111. package/bin/analytics/dates.js +0 -6
  112. package/bin/analytics/filters.js +0 -27
  113. package/bin/analytics/format.js +0 -13
  114. package/bin/analytics/index.js +0 -496
  115. package/bin/analytics/output.js +0 -75
  116. package/bin/analytics/types.js +0 -1
  117. package/bin/authenticatedFetch.js +0 -46
  118. package/bin/brokenLinks.js +0 -122
  119. package/bin/callbackServer.js +0 -65
  120. package/bin/cli.js +0 -813
  121. package/bin/comingSoon.js +0 -26
  122. package/bin/config.js +0 -78
  123. package/bin/constants.js +0 -16
  124. package/bin/deploy-base.js +0 -20
  125. package/bin/deploy-github-actions.js +0 -102
  126. package/bin/deploy-github-setup.js +0 -165
  127. package/bin/deploy-instructions.js +0 -71
  128. package/bin/deploy.js +0 -123
  129. package/bin/feedback.js +0 -36
  130. package/bin/format.js +0 -121
  131. package/bin/helpers.js +0 -238
  132. package/bin/idToken.js +0 -14
  133. package/bin/index.js +0 -102
  134. package/bin/init.js +0 -182
  135. package/bin/instructions.js +0 -101
  136. package/bin/keyring.js +0 -78
  137. package/bin/login.js +0 -123
  138. package/bin/logout.js +0 -18
  139. package/bin/mdxAccessibility.js +0 -113
  140. package/bin/mdxLinter.js +0 -55
  141. package/bin/middlewares/subdomainMiddleware.js +0 -28
  142. package/bin/middlewares/telemetryMiddleware.js +0 -61
  143. package/bin/migrate.js +0 -54
  144. package/bin/og.js +0 -145
  145. package/bin/openApiCheck.js +0 -99
  146. package/bin/resolveFiles.js +0 -114
  147. package/bin/score/client.js +0 -40
  148. package/bin/score/index.js +0 -211
  149. package/bin/score/types.js +0 -1
  150. package/bin/signup.js +0 -118
  151. package/bin/start.js +0 -36
  152. package/bin/status.js +0 -106
  153. package/bin/telemetry/client.js +0 -53
  154. package/bin/telemetry/distinctId.js +0 -68
  155. package/bin/telemetry/sessionId.js +0 -36
  156. package/bin/telemetry/track.js +0 -149
  157. package/bin/templates.js +0 -127
  158. package/bin/tokenRefresh.js +0 -50
  159. package/bin/tsconfig.build.tsbuildinfo +0 -1
  160. package/bin/update.js +0 -65
  161. package/bin/validate/command.js +0 -127
  162. package/bin/validate/engine.js +0 -154
  163. package/bin/validate/files.js +0 -163
  164. package/bin/validate/mdx.js +0 -238
  165. package/bin/validate/output.js +0 -44
  166. package/bin/validate/report.js +0 -11
  167. package/bin/validate/rules/build.js +0 -102
  168. package/bin/validate/rules/content/duplicateH1.js +0 -57
  169. package/bin/validate/rules/content/foreignSyntax.js +0 -120
  170. package/bin/validate/rules/content/katex.js +0 -34
  171. package/bin/validate/rules/content/missingSnippet.js +0 -34
  172. package/bin/validate/rules/content/undefinedVariable.js +0 -13
  173. package/bin/validate/rules/content/unknownComponent.js +0 -121
  174. package/bin/validate/rules/content/voidTags.js +0 -38
  175. package/bin/validate/rules/index.js +0 -21
  176. package/bin/validate/rules/openapi.js +0 -50
  177. package/bin/validate/rules/parseError.js +0 -7
  178. package/bin/validate/site.js +0 -57
  179. package/bin/validate/types.js +0 -1
  180. package/bin/vendor/converter.js +0 -30882
  181. package/bin/welcome.js +0 -108
  182. package/bin/workflows/client.js +0 -34
  183. package/bin/workflows/index.js +0 -269
  184. package/bin/workflows/types.js +0 -11
  185. package/scripts/bundle-converter.mjs +0 -26
  186. package/scripts/generate-logo.mjs +0 -79
  187. package/src/accessibility.ts +0 -180
  188. package/src/accessibilityCheck.tsx +0 -150
  189. package/src/addDomain.tsx +0 -206
  190. package/src/agent-auth.ts +0 -119
  191. package/src/agent-claim.ts +0 -124
  192. package/src/agent-credentials.ts +0 -104
  193. package/src/agent-deployment.ts +0 -116
  194. package/src/agent-http.ts +0 -34
  195. package/src/agent-setup-config.ts +0 -144
  196. package/src/agent-setup.ts +0 -165
  197. package/src/agent.ts +0 -56
  198. package/src/analytics/client.ts +0 -153
  199. package/src/analytics/dates.ts +0 -6
  200. package/src/analytics/filters.ts +0 -50
  201. package/src/analytics/format.ts +0 -13
  202. package/src/analytics/index.tsx +0 -599
  203. package/src/analytics/output.ts +0 -99
  204. package/src/analytics/types.ts +0 -145
  205. package/src/authenticatedFetch.ts +0 -42
  206. package/src/brokenLinks.tsx +0 -152
  207. package/src/callbackServer.ts +0 -64
  208. package/src/cli.tsx +0 -1105
  209. package/src/comingSoon.tsx +0 -29
  210. package/src/config.ts +0 -75
  211. package/src/constants.ts +0 -28
  212. package/src/deploy-base.ts +0 -18
  213. package/src/deploy-github-actions.ts +0 -139
  214. package/src/deploy-github-setup.ts +0 -189
  215. package/src/deploy-instructions.ts +0 -76
  216. package/src/deploy.ts +0 -165
  217. package/src/feedback.tsx +0 -31
  218. package/src/format.tsx +0 -82
  219. package/src/helpers.tsx +0 -279
  220. package/src/idToken.ts +0 -17
  221. package/src/index.ts +0 -109
  222. package/src/init.tsx +0 -237
  223. package/src/instructions.ts +0 -99
  224. package/src/keyring.ts +0 -65
  225. package/src/login.tsx +0 -166
  226. package/src/logout.tsx +0 -8
  227. package/src/mdxAccessibility.ts +0 -134
  228. package/src/mdxLinter.tsx +0 -88
  229. package/src/middlewares/subdomainMiddleware.ts +0 -20
  230. package/src/middlewares/telemetryMiddleware.ts +0 -55
  231. package/src/migrate.ts +0 -76
  232. package/src/og.tsx +0 -185
  233. package/src/openApiCheck.tsx +0 -125
  234. package/src/resolveFiles.ts +0 -101
  235. package/src/score/client.ts +0 -36
  236. package/src/score/index.tsx +0 -205
  237. package/src/score/types.ts +0 -15
  238. package/src/signup.tsx +0 -172
  239. package/src/start.ts +0 -27
  240. package/src/status.tsx +0 -129
  241. package/src/telemetry/client.ts +0 -46
  242. package/src/telemetry/distinctId.ts +0 -70
  243. package/src/telemetry/sessionId.ts +0 -40
  244. package/src/telemetry/track.ts +0 -163
  245. package/src/templates.tsx +0 -143
  246. package/src/tokenRefresh.ts +0 -53
  247. package/src/update.tsx +0 -89
  248. package/src/validate/command.tsx +0 -135
  249. package/src/validate/engine.ts +0 -210
  250. package/src/validate/files.ts +0 -249
  251. package/src/validate/mdx.ts +0 -290
  252. package/src/validate/output.tsx +0 -76
  253. package/src/validate/report.ts +0 -40
  254. package/src/validate/rules/build.ts +0 -146
  255. package/src/validate/rules/content/duplicateH1.ts +0 -78
  256. package/src/validate/rules/content/foreignSyntax.ts +0 -144
  257. package/src/validate/rules/content/katex.ts +0 -33
  258. package/src/validate/rules/content/missingSnippet.ts +0 -36
  259. package/src/validate/rules/content/undefinedVariable.ts +0 -20
  260. package/src/validate/rules/content/unknownComponent.ts +0 -130
  261. package/src/validate/rules/content/voidTags.ts +0 -43
  262. package/src/validate/rules/index.ts +0 -23
  263. package/src/validate/rules/openapi.ts +0 -59
  264. package/src/validate/rules/parseError.ts +0 -9
  265. package/src/validate/site.ts +0 -53
  266. package/src/validate/types.ts +0 -102
  267. package/src/vendor/converter.ts +0 -1
  268. package/src/welcome.ts +0 -154
  269. package/src/workflows/client.ts +0 -39
  270. package/src/workflows/index.tsx +0 -287
  271. package/src/workflows/types.ts +0 -73
  272. package/tsconfig.build.json +0 -4
  273. package/tsconfig.json +0 -10
  274. package/vitest.config.ts +0 -9
@@ -1,135 +0,0 @@
1
- import { addLog, checkBuild, clearLogs, ErrorLog, SpinnerLog } from '@mintlify/previewing';
2
- import path from 'node:path';
3
- import { format } from 'node:util';
4
-
5
- import { CMD_EXEC_PATH, errorMessage, getVersions, terminate } from '../helpers.js';
6
- import { resolveExplicitFiles } from '../resolveFiles.js';
7
- import { sendPublishPrompt } from '../signup.js';
8
- import { checkSite, runBuildChecks, selectRules } from './engine.js';
9
- import { logReport } from './output.js';
10
- import { toReport, type BuildStatus } from './report.js';
11
-
12
- export type ValidateArgs = {
13
- 'local-schema': boolean;
14
- 'client-version'?: string;
15
- groups?: (string | number)[];
16
- files?: (string | number)[];
17
- build: boolean;
18
- enable?: string[];
19
- disable?: string[];
20
- fix: boolean;
21
- format: 'text' | 'json';
22
- };
23
-
24
- export const EXIT = { passed: 0, failed: 1, crashed: 2 } as const;
25
-
26
- export const splitList = (values: string[]) =>
27
- values.flatMap((value) =>
28
- value
29
- .split(',')
30
- .map((item) => item.trim())
31
- .filter(Boolean)
32
- );
33
-
34
- const write = (text: string) =>
35
- new Promise<void>((resolve) => {
36
- process.stdout.write(text, () => resolve());
37
- });
38
-
39
- /**
40
- * Writes console output straight to stderr, so --format json leaves stdout to the report. Ink
41
- * patches console and redraws on stdout, so redirecting to console.error isn't enough.
42
- */
43
- const consoleToStderr = () => {
44
- const original = {
45
- log: console.log,
46
- info: console.info,
47
- warn: console.warn,
48
- error: console.error,
49
- };
50
- const toStderr = (...args: unknown[]) => {
51
- process.stderr.write(`${format(...args)}\n`);
52
- };
53
- console.log = console.info = console.warn = console.error = toStderr;
54
- return () => Object.assign(console, original);
55
- };
56
-
57
- const resolveSelection = async (baseDir: string, values: (string | number)[] | undefined) => {
58
- const args = (values ?? []).map(String).filter(Boolean);
59
- if (args.length === 0) return undefined;
60
- const files = await resolveExplicitFiles(CMD_EXEC_PATH, args);
61
- return new Set(
62
- files.map((file) =>
63
- path.relative(baseDir, path.resolve(CMD_EXEC_PATH, file)).split(path.sep).join('/')
64
- )
65
- );
66
- };
67
-
68
- /** mint validate: one pass over every selected rule, then the strict build when nothing failed. */
69
- export const validateHandler = async (
70
- argv: ValidateArgs,
71
- packageName: string,
72
- baseDir = CMD_EXEC_PATH
73
- ) => {
74
- const json = argv.format === 'json';
75
- const restoreConsole = json ? consoleToStderr() : undefined;
76
- try {
77
- const rules = selectRules({
78
- enable: argv.enable,
79
- disable: argv.disable ?? [],
80
- });
81
- const selected = await resolveSelection(baseDir, argv.files);
82
- if (!json) addLog(<SpinnerLog message="validating..." />);
83
-
84
- const { site, fixed, ...checked } = await checkSite(baseDir, rules, {
85
- selected,
86
- localSchema: argv['local-schema'],
87
- fix: argv.fix,
88
- });
89
- let { diagnostics } = checked;
90
-
91
- const buildRules = rules.some(({ needsBuild }) => needsBuild);
92
- let build: BuildStatus;
93
- if (!argv.build) {
94
- build = { ran: false, reason: 'disabled' };
95
- } else if (!buildRules) {
96
- build = { ran: false, reason: 'no build rules' };
97
- } else if (diagnostics.some(({ severity }) => severity === 'error')) {
98
- build = { ran: false, reason: 'errors found' };
99
- } else {
100
- if (!json) addLog(<SpinnerLog message="building..." />);
101
- const { cli: cliVersion } = getVersions(packageName);
102
- site.build = await checkBuild({
103
- ...argv,
104
- _: [],
105
- $0: packageName,
106
- packageName,
107
- cliVersion,
108
- disableOpenapi: !rules.some(({ group }) => group === 'openapi'),
109
- });
110
- diagnostics = await runBuildChecks(site, rules, diagnostics);
111
- build = { ran: true, passed: site.build.passed };
112
- }
113
-
114
- const report = toReport(diagnostics, build, argv.fix ? fixed : undefined);
115
- if (json) {
116
- restoreConsole?.();
117
- await write(`${JSON.stringify(report)}\n`);
118
- } else {
119
- clearLogs();
120
- logReport(report);
121
- if (report.passed && build.ran && build.passed) await sendPublishPrompt();
122
- }
123
- await terminate(report.passed ? EXIT.passed : EXIT.failed);
124
- } catch (error) {
125
- // Bad arguments and crashes alike mean validate could not run
126
- if (json) {
127
- restoreConsole?.();
128
- process.stderr.write(`Error: ${errorMessage(error)}\n`);
129
- } else {
130
- clearLogs();
131
- addLog(<ErrorLog message={errorMessage(error)} />);
132
- }
133
- await terminate(EXIT.crashed);
134
- }
135
- };
@@ -1,210 +0,0 @@
1
- import { replaceVariables } from '@mintlify/common';
2
- import fs from 'node:fs/promises';
3
- import path from 'node:path';
4
-
5
- import { checkFiles, parseFile } from './files.js';
6
- import { applyEdits, selectEdits, toPosition } from './mdx.js';
7
- import { buildFindings } from './rules/build.js';
8
- import { RULES } from './rules/index.js';
9
- import { PARSE_ERROR } from './rules/parseError.js';
10
- import { loadSite } from './site.js';
11
- import {
12
- GROUPS,
13
- type Change,
14
- type Diagnostic,
15
- type DiagnosticSource,
16
- type FileEdit,
17
- type FileRule,
18
- type Finding,
19
- type Rule,
20
- type Site,
21
- } from './types.js';
22
-
23
- export class UsageError extends Error {}
24
-
25
- const ruleList = () => RULES.map(({ id }) => id).join(', ');
26
-
27
- const matchToken = (token: string): Rule[] => {
28
- if (token === 'all') return RULES;
29
- const rule = RULES.find(({ id }) => id === token);
30
- if (rule) return [rule];
31
- if ((GROUPS as string[]).includes(token)) {
32
- return RULES.filter(({ group }) => group === token);
33
- }
34
- throw new UsageError(
35
- `unknown rule or group "${token}". Groups: all, ${GROUPS.join(', ')}. Rules: ${ruleList()}`
36
- );
37
- };
38
-
39
- /** Every rule, minus --disable, plus --enable. parse-error is the engine's own, not selectable. */
40
- export const selectRules = ({
41
- enable = [],
42
- disable = [],
43
- }: {
44
- enable?: string[];
45
- disable?: string[];
46
- }): Rule[] => {
47
- if ([...disable, ...enable].includes(PARSE_ERROR.id)) {
48
- throw new UsageError(`${PARSE_ERROR.id} always runs and can't be disabled`);
49
- }
50
- const selected = new Set(RULES);
51
- for (const token of disable) for (const rule of matchToken(token)) selected.delete(rule);
52
- for (const token of enable) for (const rule of matchToken(token)) selected.add(rule);
53
- return RULES.filter((rule) => selected.has(rule));
54
- };
55
-
56
- const toDiagnostic = (source: DiagnosticSource, finding: Finding): Diagnostic => ({
57
- ...finding,
58
- rule: source.id,
59
- group: source.group,
60
- severity: finding.severity ?? source.severity,
61
- });
62
-
63
- const RULE_ORDER = new Map(RULES.map(({ id }, index) => [id, index]));
64
-
65
- const sortDiagnostics = (diagnostics: Diagnostic[]) =>
66
- diagnostics.sort(
67
- (a, b) =>
68
- (a.file < b.file ? -1 : a.file > b.file ? 1 : 0) ||
69
- (a.line ?? 0) - (b.line ?? 0) ||
70
- (a.column ?? 0) - (b.column ?? 0) ||
71
- (RULE_ORDER.get(a.rule) ?? 0) - (RULE_ORDER.get(b.rule) ?? 0)
72
- );
73
-
74
- /** Adds findings for a rule unless a diagnostic of that rule already has the same key. */
75
- const merge = (
76
- diagnostics: Diagnostic[],
77
- source: DiagnosticSource,
78
- extra: Finding[],
79
- key: (item: Pick<Diagnostic, 'file' | 'message'>) => string
80
- ) => {
81
- const reported = new Set(diagnostics.filter((item) => item.rule === source.id).map(key));
82
- for (const finding of extra) {
83
- if (reported.has(key(finding))) continue;
84
- reported.add(key(finding));
85
- diagnostics.push(toDiagnostic(source, finding));
86
- }
87
- };
88
-
89
- const addParseErrors = (site: Site, diagnostics: Diagnostic[], extra: Finding[]) =>
90
- merge(
91
- diagnostics,
92
- PARSE_ERROR,
93
- extra.filter(({ file }) => !site.selected || site.selected.has(file)),
94
- ({ file }) => file
95
- );
96
-
97
- const fileRulesOf = (rules: Rule[]) =>
98
- rules.filter((rule): rule is FileRule => rule.scope === 'file');
99
-
100
- const runSiteRules = async (
101
- site: Site,
102
- rules: Rule[],
103
- needsBuild: boolean,
104
- diagnostics: Diagnostic[]
105
- ) => {
106
- for (const rule of rules) {
107
- if (rule.scope !== 'site' || Boolean(rule.needsBuild) !== needsBuild) continue;
108
- for (const finding of await rule.check(site)) diagnostics.push(toDiagnostic(rule, finding));
109
- }
110
- };
111
-
112
- /** Runs every selected rule that doesn't need the build. */
113
- export const runChecks = async (site: Site, rules: Rule[]) => {
114
- const diagnostics = await checkFiles(site, fileRulesOf(rules));
115
- await runSiteRules(site, rules, false, diagnostics);
116
- return sortDiagnostics(diagnostics);
117
- };
118
-
119
- /** Adds the build's findings to a site whose build has run. */
120
- export const runBuildChecks = async (site: Site, rules: Rule[], diagnostics: Diagnostic[]) => {
121
- await runSiteRules(site, rules, true, diagnostics);
122
- // The file pass already reported files it couldn't parse; the build adds the rest
123
- addParseErrors(site, diagnostics, buildFindings(site, 'parse-error'));
124
- return sortDiagnostics(diagnostics);
125
- };
126
-
127
- const parseLevel = (file: string, content: string, baseDir: string) => {
128
- const parsed = parseFile(file, content, baseDir);
129
- return 'error' in parsed ? 0 : parsed.approximate ? 1 : 2;
130
- };
131
-
132
- /** Applies every fix that doesn't overlap an earlier fix in the same file, writing each Markdown file only when its fixed content parses at least as well as before. */
133
- export const applyFixes = async (site: Site, diagnostics: Diagnostic[]): Promise<Change[]> => {
134
- const read = (file: string) => site.read(file) ?? '';
135
- const chosen = new Map<string, { fix: FileEdit; change: Change }[]>();
136
- const overlaps = ({ file, start, end }: FileEdit) =>
137
- (chosen.get(file) ?? []).some(({ fix }) => start < fix.end && fix.start < end);
138
-
139
- for (const { rule, fix } of diagnostics) {
140
- if (!fix || overlaps(fix)) continue;
141
- const content = read(fix.file);
142
- const change = {
143
- rule,
144
- file: fix.file,
145
- ...toPosition(content, fix.start),
146
- before: content.slice(fix.start, fix.end),
147
- after: fix.text,
148
- };
149
- chosen.set(fix.file, [...(chosen.get(fix.file) ?? []), { fix, change }]);
150
- }
151
-
152
- const snippets = new Set(
153
- site.files.filter(({ kind }) => kind === 'snippet').map(({ file }) => file)
154
- );
155
- const level = (file: string, content: string) =>
156
- parseLevel(
157
- file,
158
- snippets.has(file) ? replaceVariables(content, site.variables) : content,
159
- site.baseDir
160
- );
161
-
162
- const applied: Change[] = [];
163
- for (const [file, picks] of chosen) {
164
- const content = read(file);
165
- const updated = applyEdits(content, selectEdits(picks.map(({ fix }) => fix)));
166
- if (/\.mdx?$/.test(file) && level(file, updated) < level(file, content)) {
167
- continue;
168
- }
169
- await fs.writeFile(path.join(site.baseDir, file), updated);
170
- applied.push(...picks.map(({ change }) => change));
171
- }
172
- return applied;
173
- };
174
-
175
- // Fixes can unlock others (a self-closed <br> lets a page parse), so --fix runs a few passes
176
- const MAX_FIX_PASSES = 5;
177
-
178
- /**
179
- * Loads the site and runs the selected rules. With fix, applies the fixes, rechecks only the
180
- * files that changed for further fixes, then runs every rule once more for the final report.
181
- */
182
- export const checkSite = async (
183
- baseDir: string,
184
- rules: Rule[],
185
- options: { selected?: ReadonlySet<string>; localSchema: boolean; fix: boolean }
186
- ) => {
187
- const fixed: Change[] = [];
188
- let site = await loadSite(baseDir, options);
189
- const { selected } = options;
190
- if (selected && !site.files.some(({ file }) => selected.has(file))) {
191
- throw new UsageError(
192
- `none of the given files are pages or snippets of this site: ${[...selected].join(', ')}`
193
- );
194
- }
195
- const diagnostics = await runChecks(site, rules);
196
- if (!options.fix) return { site, diagnostics, fixed };
197
-
198
- // Only file rules offer fixes, so passes after the first recheck just the files that changed
199
- let pending = diagnostics;
200
- for (let pass = 0; pass < MAX_FIX_PASSES; pass++) {
201
- const changes = await applyFixes(site, pending);
202
- if (changes.length === 0) break;
203
- fixed.push(...changes);
204
- site = await loadSite(baseDir, options);
205
- const changed = new Set(changes.map(({ file }) => file));
206
- pending = sortDiagnostics(await checkFiles({ ...site, selected: changed }, fileRulesOf(rules)));
207
- }
208
- if (fixed.length === 0) return { site, diagnostics, fixed };
209
- return { site, diagnostics: await runChecks(site, rules), fixed };
210
- };
@@ -1,249 +0,0 @@
1
- import { getAST, getFileCategory, parseFrontmatter, replaceVariables } from '@mintlify/common';
2
- import {
3
- categorizeMdxFiles,
4
- getErrorPosition,
5
- getMintIgnore,
6
- listContentFiles,
7
- } from '@mintlify/prebuild';
8
- import type { Root } from 'mdast';
9
-
10
- import {
11
- applyEdits,
12
- collectAnchorLinks,
13
- parseContent,
14
- relocate,
15
- selectEdits,
16
- toOffset,
17
- unmapEdit,
18
- } from './mdx.js';
19
- import { PARSE_ERROR } from './rules/parseError.js';
20
- import type {
21
- Diagnostic,
22
- Edit,
23
- FileContext,
24
- FileFinding,
25
- FileKind,
26
- FileRule,
27
- Site,
28
- SiteFile,
29
- } from './types.js';
30
-
31
- const CHECKED_CATEGORIES = new Set<string | null>(['page', 'snippet', 'snippet-v2']);
32
-
33
- export const readSiteFiles = async (baseDir: string): Promise<SiteFile[]> => {
34
- const { mdxCandidates: candidates } = await listContentFiles(
35
- baseDir,
36
- await getMintIgnore(baseDir)
37
- );
38
- const { snippets, snippetsV2 } = await categorizeMdxFiles(baseDir, candidates);
39
- const snippetFiles = new Set([...snippets, ...snippetsV2]);
40
- return candidates
41
- .filter((name) => /\.mdx?$/.test(name) && CHECKED_CATEGORIES.has(getFileCategory(name)))
42
- .map((name) => ({
43
- file: name.replace(/^\//, ''),
44
- kind: (snippetFiles.has(name) ? 'snippet' : 'page') as FileKind,
45
- }));
46
- };
47
-
48
- const readFrontmatter = (content: string): Record<string, unknown> => {
49
- try {
50
- const { attributes } = parseFrontmatter<unknown>(content);
51
- return attributes && typeof attributes === 'object' ? { ...attributes } : {};
52
- } catch {
53
- return {};
54
- }
55
- };
56
-
57
- type ParseError = { line?: number; column?: number; message: string };
58
-
59
- const REASON_POSITION = /\s*\(\d+:\d+(?:-\d+:\d+)?\)$/;
60
- const TAG_NAME_RULES = new Set(['unexpected-character', 'unexpected-eof']);
61
- const ESCAPE_HINT = "; if this < isn't a tag, escape it as \\<";
62
-
63
- const isStrayLessThan = (
64
- error: unknown,
65
- content: string,
66
- position: { line?: number; column?: number } | null
67
- ) => {
68
- const { source, ruleId } = (error ?? {}) as { source?: unknown; ruleId?: unknown };
69
- return (
70
- source === 'micromark-extension-mdx-jsx' &&
71
- typeof ruleId === 'string' &&
72
- TAG_NAME_RULES.has(ruleId) &&
73
- position?.line != null &&
74
- position.column != null &&
75
- content[toOffset(content, position.line, position.column) - 1] === '<'
76
- );
77
- };
78
-
79
- export const toParseError = (error: unknown, content = ''): ParseError => {
80
- const reason = (error as { reason?: unknown } | null)?.reason;
81
- const fallback = error instanceof Error ? error.message : 'could not parse this file';
82
- const text = typeof reason === 'string' ? reason : fallback;
83
- const position = error && typeof error === 'object' ? getErrorPosition(error) : null;
84
- const hint = isStrayLessThan(error, content, position) ? ESCAPE_HINT : '';
85
- return {
86
- ...(position?.line != null && { line: position.line }),
87
- ...(position?.column != null && { column: position.column }),
88
- message: text.replace(REASON_POSITION, '') + hint,
89
- };
90
- };
91
-
92
- type Extras = Pick<FileContext, 'readFile' | 'anchorLinks' | 'variables'>;
93
-
94
- const parseErrorDiagnostic = (file: string, error: ParseError): Diagnostic => ({
95
- ...error,
96
- rule: PARSE_ERROR.id,
97
- group: PARSE_ERROR.group,
98
- severity: PARSE_ERROR.severity,
99
- file,
100
- message: `${error.message} (other checks were skipped for this file)`,
101
- });
102
-
103
- type Parsed = { context: FileContext; approximate: boolean } | { error: ParseError };
104
-
105
- const tryParseContent = (content: string): Root | undefined => {
106
- try {
107
- return parseContent(content);
108
- } catch {
109
- return undefined;
110
- }
111
- };
112
-
113
- export const parseFile = (
114
- file: string,
115
- content: string,
116
- baseDir: string,
117
- extras: Extras = {}
118
- ): Parsed => {
119
- const context = { file, content, baseDir, frontmatter: readFrontmatter(content), ...extras };
120
- const tree = tryParseContent(content);
121
- if (tree) return { context: { ...context, tree }, approximate: false };
122
- try {
123
- return { context: { ...context, tree: getAST(content, file) }, approximate: true };
124
- } catch (error) {
125
- return { error: toParseError(error, content) };
126
- }
127
- };
128
-
129
- const EMPTY_TREE = { type: 'root' as const, children: [] };
130
-
131
- const SOURCE_STAGE = 1;
132
- const AFTER_RAW_STAGE = 2;
133
-
134
- type Stage = { content: string; edits: Edit[] };
135
-
136
- const VARIABLE = /\{\{\s*([\w.\-]+)\s*\}\}/g;
137
-
138
- const variableEdits = (content: string, variables: Record<string, string> = {}): Edit[] =>
139
- [...content.matchAll(VARIABLE)]
140
- .filter((match) => match[1] !== undefined && variables[match[1]] !== undefined)
141
- .map((match) => ({
142
- start: match.index,
143
- end: match.index + match[0].length,
144
- text: replaceVariables(match[0], variables),
145
- }));
146
-
147
- const toDiagnostic = (
148
- rule: FileRule,
149
- file: string,
150
- { edit, ...finding }: FileFinding,
151
- stages: Stage[],
152
- stage: number
153
- ): Diagnostic => {
154
- const original = edit && unmapEdit(stages, stage, edit);
155
- return {
156
- ...finding,
157
- ...relocate(stages, stage, finding),
158
- rule: rule.id,
159
- group: rule.group,
160
- severity: rule.severity,
161
- file,
162
- ...(original && { fix: { file, ...original } }),
163
- };
164
- };
165
-
166
- const checkFile = (
167
- allRules: FileRule[],
168
- { file, kind }: SiteFile,
169
- content: string,
170
- baseDir: string,
171
- extras: Extras,
172
- variables?: Record<string, string>
173
- ): Diagnostic[] => {
174
- const rules = kind === 'snippet' ? allRules.filter((rule) => !rule.pagesOnly) : allRules;
175
- const raw = rules.filter((rule) => rule.raw);
176
- const treeRules = rules.filter((rule) => !rule.raw);
177
- const varEdits = kind === 'snippet' ? variableEdits(content, variables) : [];
178
- const source = applyEdits(content, varEdits);
179
- const rawContext = {
180
- file,
181
- content: source,
182
- baseDir,
183
- frontmatter: {},
184
- tree: EMPTY_TREE,
185
- variables,
186
- };
187
-
188
- const rawFound = raw.flatMap((rule) =>
189
- rule.check(rawContext).map((finding) => ({ rule, finding }))
190
- );
191
- const rawEdits = selectEdits(
192
- rawFound.flatMap(({ finding }) => (finding.edit ? [finding.edit] : []))
193
- );
194
- const afterRaw = applyEdits(source, rawEdits);
195
- const stages: Stage[] = [
196
- { content, edits: varEdits },
197
- { content: source, edits: rawEdits },
198
- { content: afterRaw, edits: [] },
199
- ];
200
- const diagnostics = rawFound.map(({ rule, finding }) =>
201
- toDiagnostic(rule, file, finding, stages, SOURCE_STAGE)
202
- );
203
-
204
- const parsed = parseFile(file, afterRaw, baseDir, { ...extras, variables });
205
- if ('error' in parsed) {
206
- diagnostics.unshift(
207
- parseErrorDiagnostic(file, {
208
- ...parsed.error,
209
- ...relocate(stages, AFTER_RAW_STAGE, parsed.error),
210
- })
211
- );
212
- return diagnostics;
213
- }
214
- for (const rule of treeRules) {
215
- for (const finding of rule.check(parsed.context)) {
216
- const found = parsed.approximate ? { ...finding, edit: undefined } : finding;
217
- diagnostics.push(toDiagnostic(rule, file, found, stages, AFTER_RAW_STAGE));
218
- }
219
- }
220
- return diagnostics;
221
- };
222
-
223
- const collectSiteAnchorLinks = async (site: Site): Promise<ReadonlySet<string>> => {
224
- const pages = await Promise.all(
225
- site.files.map(({ file }) => collectAnchorLinks(file, site.read(file) ?? ''))
226
- );
227
- return new Set(pages.flat());
228
- };
229
-
230
- export const checkFiles = async (site: Site, rules: FileRule[]): Promise<Diagnostic[]> => {
231
- const anchorRules = new Set(rules.filter((rule) => rule.needsAnchorLinks).map(({ id }) => id));
232
- const targets = site.files.filter(({ file }) => !site.selected || site.selected.has(file));
233
- const checkTarget = (entry: SiteFile, extras: Extras) => {
234
- const content = site.read(entry.file);
235
- if (content === undefined) {
236
- return [parseErrorDiagnostic(entry.file, { message: 'could not read this file' })];
237
- }
238
- return checkFile(rules, entry, content, site.baseDir, extras, site.variables);
239
- };
240
- const needsLinks = (diagnostics: Diagnostic[]) =>
241
- diagnostics.some(({ rule }) => anchorRules.has(rule));
242
- const checked = targets.map((entry) => checkTarget(entry, { readFile: site.read }));
243
- if (!checked.some(needsLinks)) return checked.flat();
244
- const extras = { readFile: site.read, anchorLinks: await collectSiteAnchorLinks(site) };
245
- return targets.flatMap((entry, index) => {
246
- const diagnostics = checked[index] ?? [];
247
- return needsLinks(diagnostics) ? checkTarget(entry, extras) : diagnostics;
248
- });
249
- };