@nbtca/prompt 1.5.16 → 1.5.18

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.
@@ -8,6 +8,7 @@ import { warning, createSpinner } from '../core/ui.js';
8
8
  import { pickIcon } from '../core/icons.js';
9
9
  import { spawn, execFileSync } from 'child_process';
10
10
  import { URLS } from '../config/data.js';
11
+ import { glyph } from '../core/theme.js';
11
12
  import { t, fmt, getCurrentLanguage } from '../i18n/index.js';
12
13
  import { enterScreen, breadcrumb } from '../core/transitions.js';
13
14
  import { sanitizeTerminalLine, sanitizeTerminalText, truncate } from '../core/text.js';
@@ -59,7 +60,7 @@ const RENDER_CACHE_MAX = 50;
59
60
  const renderCache = new Map();
60
61
  const METADATA_CACHE_TTL_MS = 10 * 60 * 1000;
61
62
  const METADATA_CACHE_MAX = 200;
62
- const METADATA_CONCURRENCY = 4;
63
+ const METADATA_CONCURRENCY = 6;
63
64
  const metadataCache = new Map();
64
65
  let cacheGeneration = 0;
65
66
  function getFreshRender(key) {
@@ -481,13 +482,16 @@ function hasMarkdownTable(content) {
481
482
  function hasMermaidBlock(content) {
482
483
  return /^```mermaid\b/m.test(content);
483
484
  }
484
- function extractInternalLinks(markdown) {
485
+ function isExternalHref(href) {
486
+ return /^https?:\/\//i.test(href);
487
+ }
488
+ function extractLinks(markdown) {
485
489
  const links = [];
486
- const re = /\[([^\]]+)\]\(([^)]+)\)/g;
490
+ const re = /\[([^\]]+)\]\(([^)\s]+)[^)]*\)/g;
487
491
  let m;
488
492
  while ((m = re.exec(markdown))) {
489
493
  const href = m[2] ?? '';
490
- if (isInternalHref(href))
494
+ if (isInternalHref(href) || isExternalHref(href))
491
495
  links.push({ text: m[1] ?? '', href });
492
496
  }
493
497
  return links;
@@ -521,12 +525,15 @@ export async function loadDocForReader(filePath, signal) {
521
525
  const { renderedDoc } = await loadRenderedDoc(filePath, signal);
522
526
  const seen = new Set();
523
527
  const links = [];
524
- for (const raw of extractInternalLinks(renderedDoc.cleaned)) {
525
- const resolved = resolveInternalHref(raw.href, filePath);
526
- if (seen.has(resolved))
528
+ for (const raw of extractLinks(renderedDoc.cleaned)) {
529
+ const external = isExternalHref(raw.href);
530
+ const href = external
531
+ ? sanitizeTerminalLine(raw.href)
532
+ : resolveInternalHref(raw.href, filePath);
533
+ if (seen.has(href))
527
534
  continue;
528
- seen.add(resolved);
529
- links.push({ text: sanitizeTerminalLine(raw.text), href: resolved });
535
+ seen.add(href);
536
+ links.push({ text: sanitizeTerminalLine(raw.text), href, ...(external ? { external } : {}) });
530
537
  }
531
538
  return {
532
539
  path: filePath,
@@ -562,11 +569,14 @@ function listedDoc(item, metadata) {
562
569
  return {
563
570
  ...item,
564
571
  name: sanitizeTerminalLine(item.name),
565
- title: displayDocTitle(item.name, metadata?.title),
572
+ title: metadata ? displayDocTitle(item.name, metadata.title) : '',
566
573
  summary: sanitizeTerminalLine(metadata?.summary ?? ''),
567
574
  };
568
575
  }
569
- export async function fetchDocMetadata(items, signal) {
576
+ export function withFileNameTitle(doc) {
577
+ return doc.title ? doc : { ...doc, title: displayDocTitle(doc.name) };
578
+ }
579
+ export async function fetchDocMetadata(items, signal, onDoc) {
570
580
  const results = items.map((item) => listedDoc(item));
571
581
  let nextIndex = 0;
572
582
  async function worker() {
@@ -589,8 +599,9 @@ export async function fetchDocMetadata(items, signal) {
589
599
  catch (error) {
590
600
  if (signal?.aborted)
591
601
  throw error;
592
- results[index] = listedDoc(item);
602
+ results[index] = withFileNameTitle(listedDoc(item));
593
603
  }
604
+ onDoc?.(index, results[index]);
594
605
  }
595
606
  }
596
607
  const workerCount = Math.min(METADATA_CONCURRENCY, items.length);
@@ -601,31 +612,49 @@ export function peekListedDocs(items) {
601
612
  let complete = true;
602
613
  const docs = items.map((item) => {
603
614
  const metadata = peekDocMetadata(item);
604
- if (metadata)
605
- return listedDoc(item, metadata);
606
- complete = false;
607
- return item;
615
+ if (!metadata)
616
+ complete = false;
617
+ return listedDoc(item, metadata);
608
618
  });
609
619
  return { docs, complete };
610
620
  }
611
- export async function fetchSectionMetadata(section, signal) {
612
- const files = await fetchDocMetadata(section.files, signal);
621
+ export async function fetchSectionMetadata(section, signal, onDoc) {
622
+ const files = await fetchDocMetadata(section.files, signal, onDoc);
613
623
  return { ...section, count: files.length, files };
614
624
  }
625
+ function withoutLeadingTitle(excerpt, title) {
626
+ const text = excerpt.trim();
627
+ return title && text.startsWith(title) ? text.slice(title.length).trimStart() : text;
628
+ }
615
629
  function searchDoc(result) {
630
+ const title = displayDocTitle(result.name, result.title);
616
631
  return {
617
632
  name: sanitizeTerminalLine(result.name),
618
633
  path: result.path,
619
634
  type: 'file',
620
- title: displayDocTitle(result.name, result.title),
635
+ title,
621
636
  summary: sanitizeTerminalLine(result.summary),
622
- excerpt: sanitizeTerminalLine(result.excerpt),
637
+ excerpt: withoutLeadingTitle(sanitizeTerminalLine(result.excerpt), title),
623
638
  route: result.route,
624
639
  score: result.score,
625
640
  section: result.section,
626
641
  };
627
642
  }
628
- export async function searchDocuments(query, signal) {
643
+ export async function searchDocuments(query, signal, onProgress) {
644
+ if (onProgress) {
645
+ const items = await fetchAllDocs(signal);
646
+ let pending = items.filter((item) => !peekDocMetadata(item));
647
+ if (pending.length > METADATA_CONCURRENCY) {
648
+ await runDocsClientOperation(signal, (client) => client.prefetch());
649
+ pending = pending.filter((item) => !peekDocMetadata(item));
650
+ }
651
+ let done = items.length - pending.length;
652
+ onProgress(done, items.length);
653
+ await fetchDocMetadata(pending, signal, () => {
654
+ done += 1;
655
+ onProgress(done, items.length);
656
+ });
657
+ }
629
658
  return (await runDocsClientOperation(signal, (client) => client.search(query, { limit: 20 }))).map(searchDoc);
630
659
  }
631
660
  export function buildSections(all) {
@@ -758,12 +787,17 @@ async function displayWithLess(doc, filePath) {
758
787
  const header = [
759
788
  '',
760
789
  chalk.bold.cyan(` ${title}`),
761
- chalk.dim(` ${filePath}`) + chalk.dim(` · ${readTime}`),
790
+ chalk.dim(` ${filePath}`) + chalk.dim(` ${glyph.sep()} ${readTime}`),
762
791
  rule,
763
792
  ...(tocBlock ? [tocBlock] : []),
764
793
  '',
765
794
  ].join('\n');
766
- const footer = ['', rule, chalk.dim(` ${trans.docs.endOfDocument}`), ''].join('\n');
795
+ const footer = [
796
+ '',
797
+ rule,
798
+ chalk.dim(` ${fmt(trans.docs.endOfDocument, { sep: glyph.sep() })}`),
799
+ '',
800
+ ].join('\n');
767
801
  const plain = chalk.level === 0;
768
802
  const fullContent = plain
769
803
  ? sanitizeTerminalText(header + rendered + footer)
@@ -842,7 +876,7 @@ async function showArchivedSection(files) {
842
876
  spinner.stop();
843
877
  const subDirs = new Set(groupFiles.map((f) => f.path.split('/')[2]).filter(Boolean));
844
878
  const fileSelected = await runMenu({
845
- title: `${trans.docs.categoryArchived} · ${groupKey}`,
879
+ title: `${trans.docs.categoryArchived} ${glyph.sep()} ${groupKey}`,
846
880
  options: [
847
881
  ...groupFiles.map((f) => {
848
882
  const sub = f.path.split('/').slice(2, -1).join('/');