@mintlify/cli 4.0.1605 → 4.0.1606

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/cli.js CHANGED
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
11
11
  import { buildGraph, getBrokenExternalLinks } from '@mintlify/link-rot';
12
12
  import { addLog, dev, exportSite, validateBuild, ErrorLog, SpinnerLog, SuccessLog, Logs, clearLogs, BrokenLinksLog, WarningLog, } from '@mintlify/previewing';
13
+ import { thumbnailOverrideSchema } from '@mintlify/validation';
13
14
  import { render, Text } from 'ink';
14
15
  import fs from 'node:fs/promises';
15
16
  import path from 'path';
@@ -32,6 +33,7 @@ import { logout } from './logout.js';
32
33
  import { mdxLinter } from './mdxLinter.js';
33
34
  import { createTelemetryMiddleware } from './middlewares/telemetryMiddleware.js';
34
35
  import { mintTestHandler } from './mintTest.js';
36
+ import { ogCommand } from './og.js';
35
37
  import { buildOverlayRegistryForCheck, checkOpenApiFile, getOpenApiFilenamesFromDocsConfig, } from './openApiCheck.js';
36
38
  import { resolveExplicitFiles } from './resolveFiles.js';
37
39
  import { scoreHandler } from './score/index.js';
@@ -524,6 +526,37 @@ export const cli = ({ packageName = 'mint' }) => {
524
526
  mdxLinterTerminateCode = yield mdxLinter();
525
527
  }
526
528
  yield terminate(accessibilityCheckTerminateCode || mdxLinterTerminateCode);
529
+ }))
530
+ .command('og [page]', 'Render the social preview (OG) image for a page', (yargs) => yargs
531
+ .positional('page', {
532
+ type: 'string',
533
+ description: 'Page path, such as guides/webhooks. Defaults to index',
534
+ })
535
+ .option('preset', {
536
+ choices: thumbnailOverrideSchema.shape.preset.unwrap().options,
537
+ description: 'Try a preset without editing docs.json',
538
+ })
539
+ .option('surface', {
540
+ choices: thumbnailOverrideSchema.shape.surface.unwrap().options,
541
+ description: 'Try a surface without editing docs.json',
542
+ })
543
+ .option('appearance', {
544
+ choices: thumbnailOverrideSchema.shape.appearance.unwrap().options,
545
+ description: 'Try an appearance without editing docs.json',
546
+ })
547
+ .option('output', {
548
+ type: 'string',
549
+ description: 'Where to write the PNG. Defaults to a temporary file',
550
+ })
551
+ .option('open', {
552
+ type: 'boolean',
553
+ default: true,
554
+ description: 'Open the image after rendering. Use --no-open in CI',
555
+ })
556
+ .example('mint og guides/webhooks', 'Render one page and open it')
557
+ .example('mint og --preset headline --surface brand', 'Try settings on the home page')
558
+ .example('mint og --output og.png --no-open', 'Render to a file in CI'), (argv) => __awaiter(void 0, void 0, void 0, function* () {
559
+ yield terminate(yield ogCommand(argv));
527
560
  }))
528
561
  .command(['version', 'v'], 'Display the current version of the CLI and client', () => undefined, () => __awaiter(void 0, void 0, void 0, function* () {
529
562
  const { cli, client } = getVersions(packageName);
package/bin/og.js ADDED
@@ -0,0 +1,145 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { jsx as _jsx } from "react/jsx-runtime";
11
+ import { parseFrontmatter, slugToTitle } from '@mintlify/common';
12
+ import { renderOgCardPng } from '@mintlify/og';
13
+ import { applyCardThemeOverride, applyThumbnailOverride, buildOgCardInput, checkOgCard, createLocalAssetLoaders, deriveOgThemeParams, getOgPageParams, MAX_OG_ASSET_BYTES, toOgFontAsset, toOgImageAsset, } from '@mintlify/og/config';
14
+ import { getConfigObj } from '@mintlify/prebuild';
15
+ import { addLog, ErrorLog, SuccessLog, WarningLog } from '@mintlify/previewing';
16
+ import { validateDocsConfig } from '@mintlify/validation';
17
+ import { mkdir, mkdtemp, readFile, writeFile } from 'node:fs/promises';
18
+ import os from 'node:os';
19
+ import path from 'node:path';
20
+ import open from 'open';
21
+ import { CMD_EXEC_PATH } from './helpers.js';
22
+ const REMOTE_TIMEOUT_MS = 5000;
23
+ const PAGE_EXTENSIONS = ['.mdx', '.md'];
24
+ const MAX_REPORTED_ISSUES = 5;
25
+ const readLimited = (response) => __awaiter(void 0, void 0, void 0, function* () {
26
+ const declared = Number(response.headers.get('content-length'));
27
+ if (declared > MAX_OG_ASSET_BYTES || !response.body)
28
+ return null;
29
+ const reader = response.body.getReader();
30
+ const chunks = [];
31
+ let size = 0;
32
+ for (let result = yield reader.read(); !result.done; result = yield reader.read()) {
33
+ size += result.value.byteLength;
34
+ if (size > MAX_OG_ASSET_BYTES) {
35
+ yield reader.cancel();
36
+ return null;
37
+ }
38
+ chunks.push(result.value);
39
+ }
40
+ const bytes = new Uint8Array(size);
41
+ let offset = 0;
42
+ for (const chunk of chunks) {
43
+ bytes.set(chunk, offset);
44
+ offset += chunk.byteLength;
45
+ }
46
+ return bytes;
47
+ });
48
+ const fetchAsset = (src) => __awaiter(void 0, void 0, void 0, function* () {
49
+ var _a;
50
+ try {
51
+ const response = yield fetch(src, { signal: AbortSignal.timeout(REMOTE_TIMEOUT_MS) });
52
+ if (!response.ok)
53
+ return null;
54
+ const bytes = yield readLimited(response);
55
+ return bytes && { bytes, contentType: (_a = response.headers.get('content-type')) !== null && _a !== void 0 ? _a : undefined };
56
+ }
57
+ catch (_b) {
58
+ return null;
59
+ }
60
+ });
61
+ const remoteLoaders = {
62
+ image: (src) => __awaiter(void 0, void 0, void 0, function* () {
63
+ const asset = yield fetchAsset(src);
64
+ return asset && toOgImageAsset(asset.bytes, asset.contentType);
65
+ }),
66
+ font: (src) => __awaiter(void 0, void 0, void 0, function* () {
67
+ const asset = yield fetchAsset(src);
68
+ return asset && toOgFontAsset(asset.bytes);
69
+ }),
70
+ };
71
+ const normalizePagePath = (page) => path.posix
72
+ .normalize(`/${(page !== null && page !== void 0 ? page : '').replace(/\\/g, '/')}`)
73
+ .replace(/^\/+|\/+$/g, '')
74
+ .replace(/\.mdx?$/, '') || 'index';
75
+ const pageHref = (pagePath) => pagePath.replace(/(^|\/)index$/, '');
76
+ const pageCandidates = (pagePath) => [pagePath, `${pagePath}/index`].flatMap((base) => PAGE_EXTENSIONS.map((extension) => `${base}${extension}`));
77
+ const readPage = (pagePath) => __awaiter(void 0, void 0, void 0, function* () {
78
+ const root = path.resolve(CMD_EXEC_PATH);
79
+ for (const candidate of pageCandidates(pagePath)) {
80
+ const filePath = path.resolve(root, candidate);
81
+ if (!filePath.startsWith(`${root}${path.sep}`))
82
+ return null;
83
+ try {
84
+ return yield readFile(filePath, 'utf-8');
85
+ }
86
+ catch (_a) {
87
+ continue;
88
+ }
89
+ }
90
+ return null;
91
+ });
92
+ const stringAttribute = (attributes, key) => {
93
+ const value = attributes[key];
94
+ return typeof value === 'string' && value ? value : undefined;
95
+ };
96
+ const defaultOutputPath = (pagePath) => __awaiter(void 0, void 0, void 0, function* () {
97
+ return path.join(yield mkdtemp(path.join(os.tmpdir(), 'mintlify-og-')), `${pagePath.replace(/\//g, '-')}.png`);
98
+ });
99
+ export function ogCommand(options) {
100
+ return __awaiter(this, void 0, void 0, function* () {
101
+ var _a, _b;
102
+ const rawConfig = yield getConfigObj(CMD_EXEC_PATH, 'docs');
103
+ if (!rawConfig) {
104
+ addLog(_jsx(ErrorLog, { message: "No docs.json found. Run mint og from your docs directory." }));
105
+ return 1;
106
+ }
107
+ const validation = validateDocsConfig(rawConfig);
108
+ if (!validation.success) {
109
+ const issues = validation.error.issues
110
+ .slice(0, MAX_REPORTED_ISSUES)
111
+ .map((issue) => `${issue.path.join('.') || 'docs.json'}: ${issue.message}`);
112
+ addLog(_jsx(ErrorLog, { message: `docs.json is invalid:\n${issues.join('\n')}` }));
113
+ return 1;
114
+ }
115
+ const docsConfig = validation.data;
116
+ const pagePath = normalizePagePath(options.page);
117
+ const contents = yield readPage(pagePath);
118
+ if (contents === null) {
119
+ addLog(_jsx(ErrorLog, { message: `Couldn't find a page at ${pageCandidates(pagePath).join(', ')}.` }));
120
+ return 1;
121
+ }
122
+ const { attributes } = parseFrontmatter(contents);
123
+ const pageParams = getOgPageParams({
124
+ path: pagePath,
125
+ title: (_b = (_a = stringAttribute(attributes, 'title')) !== null && _a !== void 0 ? _a : stringAttribute(attributes, 'sidebarTitle')) !== null && _b !== void 0 ? _b : slugToTitle(pageHref(pagePath)),
126
+ description: stringAttribute(attributes, 'description'),
127
+ thumbnail: attributes.thumbnail,
128
+ }, docsConfig);
129
+ const themeParams = applyThumbnailOverride(applyCardThemeOverride(deriveOgThemeParams(docsConfig), pageParams.thumbnail), options);
130
+ const input = yield buildOgCardInput(pageParams, themeParams, createLocalAssetLoaders(CMD_EXEC_PATH, remoteLoaders));
131
+ const png = yield renderOgCardPng(input);
132
+ const outputPath = options.output
133
+ ? path.resolve(options.output)
134
+ : yield defaultOutputPath(pagePath);
135
+ yield mkdir(path.dirname(outputPath), { recursive: true });
136
+ yield writeFile(outputPath, new Uint8Array(png));
137
+ for (const warning of checkOgCard(input, themeParams)) {
138
+ addLog(_jsx(WarningLog, { message: warning }));
139
+ }
140
+ addLog(_jsx(SuccessLog, { message: `Wrote ${outputPath}` }));
141
+ if (options.open && !process.env.CI)
142
+ yield open(outputPath);
143
+ return 0;
144
+ });
145
+ }