UNPKG

@mintlify/link-rot

Version:

Static checking for broken internal links

48 lines (47 loc) 2.41 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { resolveFileRefs } from '@mintlify/prebuild'; import { validateDocsConfig, validateMintConfig } from '@mintlify/validation'; import fs from 'fs-extra'; import path from 'path'; /** * Read docs.json (preferred) or mint.json from `baseDir` and resolve any * `$ref` pointers inside it to sibling JSON files. Returns the fully * expanded object, matching what production sees after `getConfigObj`. * * Returns `null` when neither config exists or when parsing/resolution fails. */ export const readResolvedConfigJson = (baseDir) => __awaiter(void 0, void 0, void 0, function* () { const order = ['docs.json', 'mint.json']; for (const configFile of order) { const configPath = path.join(baseDir, configFile); if (!fs.existsSync(configPath)) continue; try { const parsed = yield fs.readJSON(configPath); const { resolved } = yield resolveFileRefs(parsed, baseDir); if (resolved && typeof resolved === 'object' && !Array.isArray(resolved)) { return { configFile, json: resolved }; } // Resolved to a non-object (malformed config) — try the next candidate. } catch (err) { console.warn(`Warning: Failed to read or resolve ${configFile}: ${err}`); } } return null; }); export const readResolvedConfigJsonAndValidate = (baseDir) => __awaiter(void 0, void 0, void 0, function* () { const resolvedConfigJson = yield readResolvedConfigJson(baseDir); if (!resolvedConfigJson) return null; const validate = resolvedConfigJson.configFile === 'docs.json' ? validateDocsConfig : validateMintConfig; return validate(resolvedConfigJson.json); });