@mintlify/cli 4.0.1438 → 4.0.1440

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/bin/format.js CHANGED
@@ -71,7 +71,7 @@ function walk(dir, ignores) {
71
71
  }
72
72
  export const formatHandler = () => __awaiter(void 0, void 0, void 0, function* () {
73
73
  var _a, e_1, _b, _c;
74
- const { mdxToPm, pmToMdx } = yield import('./vendor/converter.js');
74
+ const { mdxToPm, pmToMdx, canonicalPmToMdxOptions } = yield import('./vendor/converter.js');
75
75
  const ignores = [...(yield getGitIgnore()), ...(yield getMintIgnore(CMD_EXEC_PATH))];
76
76
  let total = 0;
77
77
  let changed = 0;
@@ -85,8 +85,20 @@ export const formatHandler = () => __awaiter(void 0, void 0, void 0, function* (
85
85
  const relative = path.relative(CMD_EXEC_PATH, file);
86
86
  try {
87
87
  const raw = yield fs.readFile(file, 'utf-8');
88
- const { doc, frontmatter } = mdxToPm(raw, { singleDollarTextMath: false });
89
- const formatted = `${pmToMdx(doc, { frontmatter })}\n`;
88
+ let formatted = raw;
89
+ let originalFrontmatterKeys = 0;
90
+ for (let pass = 0; pass < 3; pass++) {
91
+ const { doc, frontmatter } = mdxToPm(formatted, { singleDollarTextMath: false });
92
+ if (pass === 0)
93
+ originalFrontmatterKeys = Object.keys(frontmatter).length;
94
+ const next = `${pmToMdx(doc, Object.assign({ frontmatter }, canonicalPmToMdxOptions))}\n`;
95
+ if (next === formatted)
96
+ break;
97
+ formatted = next;
98
+ }
99
+ if (originalFrontmatterKeys > 0 && !formatted.startsWith('---')) {
100
+ throw new Error('formatting would remove frontmatter; file left untouched');
101
+ }
90
102
  if (formatted !== raw) {
91
103
  yield fs.writeFile(file, formatted);
92
104
  changed++;