(opts: PreviewOptions)
| 320 | * preview — render HTML and open it. No PDF round trip. |
| 321 | */ |
| 322 | export async function preview(opts: PreviewOptions): Promise<string> { |
| 323 | const progress = new ProgressReporter(opts); |
| 324 | const input = path.resolve(opts.input); |
| 325 | if (!fs.existsSync(input)) { |
| 326 | throw new Error(`input file not found: ${input}`); |
| 327 | } |
| 328 | |
| 329 | progress.begin("Rendering HTML"); |
| 330 | const markdown = fs.readFileSync(input, "utf8"); |
| 331 | // Preview deliberately skips the diagram/image pre-pass (no browse daemon |
| 332 | // round-trip — preview is the fast loop). Be loud about the divergence so |
| 333 | // nobody signs off on a preview that lacks what the PDF will have. |
| 334 | if (!opts.quiet) { |
| 335 | const fenceCount = extractDiagramFences(markdown).fences.length; |
| 336 | const hasLocalImages = /!\[[^\]]*\]\((?!https?:|data:)[^)]+\)/.test(markdown); |
| 337 | if (fenceCount > 0 || hasLocalImages) { |
| 338 | process.stderr.write( |
| 339 | `[make-pdf] preview note: ${fenceCount > 0 ? `${fenceCount} diagram fence(s) shown as code` : ""}` + |
| 340 | `${fenceCount > 0 && hasLocalImages ? "; " : ""}` + |
| 341 | `${hasLocalImages ? "local images may not resolve from the preview location" : ""}` + |
| 342 | ` — \`generate\` renders them fully.\n`, |
| 343 | ); |
| 344 | } |
| 345 | } |
| 346 | const rendered = render({ |
| 347 | markdown, |
| 348 | title: opts.title, |
| 349 | author: opts.author, |
| 350 | date: opts.date, |
| 351 | cover: opts.cover, |
| 352 | toc: opts.toc, |
| 353 | watermark: opts.watermark, |
| 354 | noChapterBreaks: opts.noChapterBreaks, |
| 355 | confidential: opts.confidential, |
| 356 | pageNumbers: opts.pageNumbers, |
| 357 | }); |
| 358 | progress.end("Rendering HTML", `${rendered.meta.wordCount} words`); |
| 359 | |
| 360 | // Write to a stable path under /tmp so the user can reload in the same tab. |
| 361 | const previewPath = path.join(os.tmpdir(), `make-pdf-preview-${deriveSlug(input)}.html`); |
| 362 | fs.writeFileSync(previewPath, rendered.html, "utf8"); |
| 363 | |
| 364 | progress.begin("Opening preview"); |
| 365 | tryOpen(previewPath); |
| 366 | progress.end("Opening preview"); |
| 367 | |
| 368 | progress.done(`Preview at ${previewPath}`); |
| 369 | return previewPath; |
| 370 | } |
| 371 | |
| 372 | // ─── helpers ────────────────────────────────────────────── |
| 373 |
no test coverage detected