@astryxdesign/cli 0.6.3-canary.1d940e0 → 0.6.3-canary.1f7db5b

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.
Files changed (113) hide show
  1. package/README.md +2 -1
  2. package/api/docs/_adapter.d.mts +10 -0
  3. package/api/docs/_adapter.mjs +67 -11
  4. package/api/docs/detail/detail.d.mts +15 -0
  5. package/api/docs/detail/detail.mjs +23 -8
  6. package/api/docs/detail/section/section.d.mts +1 -1
  7. package/api/docs/detail/section/section.mjs +37 -17
  8. package/api/docs/detail/section/section.test.mjs +40 -0
  9. package/api/docs/docs.d.mts +7 -2
  10. package/api/docs/docs.doc.mjs +27 -10
  11. package/api/docs/docs.mjs +16 -9
  12. package/api/docs/docs.test.mjs +6 -0
  13. package/api/docs/docs.type.d.mts +37 -0
  14. package/api/docs/docs.type.mjs +28 -0
  15. package/api/docs/index/index.d.mts +18 -0
  16. package/api/docs/index/index.mjs +31 -0
  17. package/api/docs/index/index.test.mjs +62 -0
  18. package/api/docs/integrationDocs.test.mjs +106 -0
  19. package/api/doctor/doctor.d.mts +48 -0
  20. package/api/doctor/doctor.mjs +236 -0
  21. package/api/doctor/doctor.test.mjs +196 -0
  22. package/api/hook/list/list.d.mts +1 -1
  23. package/api/integration/integration-authoring.type.d.mts +1 -1
  24. package/api/search/search.d.mts +1 -1
  25. package/api/search/search.type.d.mts +1 -1
  26. package/api/template/template.d.mts +1 -1
  27. package/api/template/template.type.d.mts +1 -1
  28. package/api/upgrade/_adapter.mjs +71 -5
  29. package/api/upgrade/project-context.test.mjs +272 -0
  30. package/assets/docs/README.md +9 -0
  31. package/assets/docs/authoring.doc.mjs +14 -0
  32. package/assets/docs/cli-integrations.doc.mjs +5 -1
  33. package/authoring/debug/parse.d.mts +1 -1
  34. package/authoring/doctypes/_schema.d.mts +676 -21
  35. package/authoring/doctypes/_schema.mjs +360 -38
  36. package/authoring/doctypes/base/graph-fields.doc.d.mts +9 -0
  37. package/authoring/doctypes/base/graph-fields.doc.mjs +55 -0
  38. package/authoring/doctypes/base/type.ts +36 -0
  39. package/authoring/doctypes/command/type.ts +2 -1
  40. package/authoring/doctypes/component/component.doc.mjs +1 -1
  41. package/authoring/doctypes/component/type.ts +3 -2
  42. package/authoring/doctypes/enum/type.ts +3 -1
  43. package/authoring/doctypes/function/type.ts +6 -2
  44. package/authoring/doctypes/hook/type.ts +2 -1
  45. package/authoring/doctypes/legacy.d.mts +4 -2
  46. package/authoring/doctypes/legacy.mjs +3 -2
  47. package/authoring/doctypes/namespace/namespace.doc.d.mts +9 -0
  48. package/authoring/doctypes/namespace/namespace.doc.mjs +128 -0
  49. package/authoring/doctypes/namespace/parse.d.mts +12 -0
  50. package/authoring/doctypes/namespace/parse.mjs +25 -0
  51. package/authoring/doctypes/namespace/parse.test.mjs +165 -0
  52. package/authoring/doctypes/namespace/type.ts +71 -0
  53. package/authoring/doctypes/parse.d.mts +4 -2
  54. package/authoring/doctypes/parse.mjs +8 -2
  55. package/authoring/doctypes/parse.test.mjs +77 -3
  56. package/authoring/doctypes/reference/parse.mjs +7 -4
  57. package/authoring/doctypes/reference/reference.doc.mjs +13 -4
  58. package/authoring/doctypes/reference/type.ts +51 -5
  59. package/authoring/doctypes/schema/type.ts +2 -1
  60. package/authoring/doctypes/template/parse.mjs +3 -1
  61. package/authoring/doctypes/template/parse.test.mjs +8 -2
  62. package/authoring/doctypes/template/type.ts +2 -2
  63. package/authoring/doctypes/types.ts +1 -0
  64. package/authoring/gap-report/parse.d.mts +1 -1
  65. package/authoring/identity/identity.doc.d.mts +9 -0
  66. package/authoring/identity/identity.doc.mjs +60 -0
  67. package/authoring/identity/type.ts +132 -0
  68. package/authoring/index.d.mts +1 -0
  69. package/authoring/index.d.ts +32 -0
  70. package/authoring/index.mjs +1 -0
  71. package/authoring/integration/integration.doc.mjs +13 -6
  72. package/authoring/integration/parse.test.mjs +10 -1
  73. package/authoring/integration/schema.d.mts +2 -0
  74. package/authoring/integration/schema.mjs +6 -0
  75. package/authoring/integration/type.ts +7 -3
  76. package/authoring/shadcn/receipt.d.mts +6 -6
  77. package/clients/cli/commands/docs.doc.mjs +13 -3
  78. package/clients/cli/commands/docs.mjs +121 -21
  79. package/clients/cli/commands/docs.test.mjs +88 -0
  80. package/clients/cli/formatters/index.mjs +162 -1
  81. package/clients/cli/formatters/index.test.mjs +91 -0
  82. package/clients/cli/lib/manifest.mjs +7 -2
  83. package/foundation/config/project.mjs +21 -6
  84. package/foundation/discovery/authoring-self-docs.d.mts +69 -0
  85. package/foundation/discovery/authoring-self-docs.mjs +214 -0
  86. package/foundation/discovery/authoring-self-docs.test.mjs +95 -0
  87. package/foundation/discovery/component-discovery.d.mts +1 -1
  88. package/foundation/discovery/component-discovery.mjs +2 -1
  89. package/foundation/discovery/docs-discovery.d.mts +7 -4
  90. package/foundation/discovery/docs-discovery.mjs +194 -88
  91. package/foundation/discovery/docs-discovery.test.mjs +243 -13
  92. package/foundation/discovery/docs-output-budget.d.mts +28 -0
  93. package/foundation/discovery/docs-output-budget.mjs +50 -0
  94. package/foundation/discovery/docs-section-key.d.mts +98 -0
  95. package/foundation/discovery/docs-section-key.mjs +221 -0
  96. package/foundation/discovery/docs-section-key.test.mjs +224 -0
  97. package/foundation/discovery/template-adapter.mjs +2 -1
  98. package/foundation/identity/provider-identity.d.mts +90 -0
  99. package/foundation/identity/provider-identity.mjs +320 -0
  100. package/foundation/identity/provider-identity.test.mjs +254 -0
  101. package/foundation/identity/providers.d.mts +7 -0
  102. package/foundation/identity/providers.mjs +16 -0
  103. package/foundation/integrations/autolink.mjs +12 -5
  104. package/foundation/integrations/integration-warnings.mjs +6 -0
  105. package/foundation/integrations/integrations.d.mts +46 -2
  106. package/foundation/integrations/integrations.mjs +167 -8
  107. package/foundation/integrations/integrations.test.mjs +384 -1
  108. package/foundation/integrations/provider-conflicts.test.mjs +125 -0
  109. package/foundation/integrations/validate-contributions.d.mts +2 -0
  110. package/foundation/integrations/validate-contributions.mjs +10 -0
  111. package/foundation/response/json-contract.test.mjs +46 -17
  112. package/foundation/response/response-types.doc.mjs +6 -1
  113. package/package.json +9 -9
package/README.md CHANGED
@@ -403,7 +403,8 @@ Every response has a `type` discriminant. The full set is below (generated from
403
403
  | `component.detail.blocks` | One component's example blocks, as {component, showcase, examples, related} of BlockEntry. |
404
404
  | `docs.list` | All reference-doc topics as DocsListEntry[] ({topic, description}), in discovery order. |
405
405
  | `docs.detail` | One topic's full ReferenceDoc, with token-ref blocks inlined. |
406
- | `docs.detail.section` | A single ReferenceSection of a topic: the first whose title contains the section query. |
406
+ | `docs.index` | One topic's section index (--index): each section's key, title, and one-line summary. |
407
+ | `docs.detail.section` | One ReferenceSection of a topic, found by key or title, with token-ref blocks inlined. |
407
408
  | `blog.list` | The feed URL plus every post parsed from the RSS feed, each with slug, title, description, date, type, authors, link, and plaintext URL. |
408
409
  | `blog.detail` | One post's metadata plus the feed URL and the post's full plaintext body. |
409
410
  | `discover.list` | The configured external packages (name, category, components, version, description); when empty it carries meta.configured to tell "nothing configured" from "nothing discovered". |
@@ -39,6 +39,12 @@ export function loadReferenceDocs(docPath: string, { lang }?: {
39
39
  export function loadTopicDoc(entry: import("../../foundation/discovery/docs-discovery.mjs").DocsTopicEntry, { lang }?: {
40
40
  lang?: string | null;
41
41
  }): Promise<import("./docs.type.mjs").DocsDetailResponse["data"]>;
42
+ /**
43
+ * The overlay languages a topic ships for its own file or any extension.
44
+ * @param {import('../../foundation/discovery/docs-discovery.mjs').DocsTopicEntry} entry
45
+ * @returns {string[]}
46
+ */
47
+ export function overlayLanguages(entry: import("../../foundation/discovery/docs-discovery.mjs").DocsTopicEntry): string[];
42
48
  /**
43
49
  * Resolve `topic` against the project's catalog (throwing `ERR_UNKNOWN_TOPIC`
44
50
  * when unmatched), and load it with any --dense/--zh overlay and any
@@ -54,6 +60,7 @@ export function loadTopicDoc(entry: import("../../foundation/discovery/docs-disc
54
60
  * @returns {Promise<{
55
61
  * catalog: DocsCatalog,
56
62
  * docsData: import('./docs.type.mjs').DocsDetailResponse['data'],
63
+ * lang: string | null,
57
64
  * }>}
58
65
  */
59
66
  export function resolveTopicDocs(topic: string, options?: {
@@ -64,5 +71,8 @@ export function resolveTopicDocs(topic: string, options?: {
64
71
  }): Promise<{
65
72
  catalog: DocsCatalog;
66
73
  docsData: import("./docs.type.mjs").DocsDetailResponse["data"];
74
+ lang: string | null;
67
75
  }>;
76
+ /** The localized overlays a docs read can apply. */
77
+ export const OVERLAY_LANGUAGES: string[];
68
78
  import { DocsCatalog } from '../../foundation/discovery/docs-discovery.mjs';
@@ -23,9 +23,16 @@ import {Project} from '../../foundation/config/project.mjs';
23
23
  import {
24
24
  DocsCatalog,
25
25
  mergeTopic,
26
+ problemsInTopic,
27
+ withSourceTitle,
26
28
  } from '../../foundation/discovery/docs-discovery.mjs';
29
+ import {
30
+ sectionKeyProblems,
31
+ withSectionKeys,
32
+ } from '../../foundation/discovery/docs-section-key.mjs';
27
33
  import {AstryxError} from '../error.mjs';
28
34
  import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
35
+ import {parseDoc} from '../../authoring/doctypes/parse.mjs';
29
36
 
30
37
  /**
31
38
  * The project's topics: the built-in ones plus whatever the configured
@@ -56,13 +63,20 @@ export async function loadDocsCatalog(cwd = process.cwd()) {
56
63
  */
57
64
  export async function loadReferenceDocs(docPath, {lang} = {}) {
58
65
  const mod = await import(pathToFileURL(docPath).href);
59
- const docs = mod.docs ?? mod.default;
66
+ const parsed = parseDoc(mod.docs ?? mod.default, path.basename(docPath));
67
+ if (!('sections' in parsed)) {
68
+ throw new Error(`${path.basename(docPath)} is not a reference document.`);
69
+ }
70
+ const problems = problemsInTopic(parsed);
71
+ if (problems.length > 0) {
72
+ throw new Error(
73
+ `${path.basename(docPath)} is invalid: ${problems.join('; ')}`,
74
+ );
75
+ }
76
+ const docs = parsed;
60
77
  if (!lang || lang === 'en') return docs;
61
78
 
62
- const dir = path.dirname(docPath);
63
- const base = path.basename(docPath, '.doc.mjs');
64
- const locale = lang === 'dense' ? 'dense' : lang;
65
- const translationPath = path.join(dir, `${base}.doc.${locale}.mjs`);
79
+ const translationPath = overlayPath(docPath, lang);
66
80
  if (!fs.existsSync(translationPath)) return docs;
67
81
 
68
82
  const translationMod = await import(pathToFileURL(translationPath).href);
@@ -86,10 +100,12 @@ export async function loadReferenceDocs(docPath, {lang} = {}) {
86
100
  ...docs,
87
101
  description: translation.description || docs.description,
88
102
  sections: docs.sections.map(
89
- (/** @type {import('@astryxdesign/cli/authoring').ReferenceSection} */ section) => {
103
+ (
104
+ /** @type {import('@astryxdesign/cli/authoring').ReferenceSection} */ section,
105
+ ) => {
90
106
  const ts = bySection.get(section.title);
91
107
  if (!ts) return section;
92
- return {
108
+ const localized = {
93
109
  ...section,
94
110
  title: ts.title || section.title,
95
111
  content: section.content.map(
@@ -99,12 +115,15 @@ export async function loadReferenceDocs(docPath, {lang} = {}) {
99
115
  ) => {
100
116
  const tb = ts.content?.[bi];
101
117
  if (!tb) return block;
102
- if (tb.type === 'prose' && block.type === 'prose') return {...block, text: tb.text};
103
- if (tb.type === 'list' && block.type === 'list') return {...block, items: tb.items};
118
+ if (tb.type === 'prose' && block.type === 'prose')
119
+ return {...block, text: tb.text};
120
+ if (tb.type === 'list' && block.type === 'list')
121
+ return {...block, items: tb.items};
104
122
  return block;
105
123
  },
106
124
  ),
107
125
  };
126
+ return withSourceTitle(localized, section.title);
108
127
  },
109
128
  ),
110
129
  };
@@ -127,8 +146,44 @@ export async function loadTopicDoc(entry, {lang} = {}) {
127
146
  let doc = await loadReferenceDocs(entry.path, {lang});
128
147
  for (const extension of entry.extensions) {
129
148
  doc = mergeTopic(doc, await loadReferenceDocs(extension.path, {lang}));
149
+ // Merging matches on keys, so this holds unless merge itself regresses.
150
+ const problems = sectionKeyProblems(doc.sections);
151
+ if (problems.length > 0) {
152
+ throw new Error(
153
+ `${path.basename(extension.path)}, extending ${entry.name}, leaves two sections with one key: ${problems.join('; ')}`,
154
+ );
155
+ }
130
156
  }
131
- return doc;
157
+ // Derived keys are stamped only now, so they never take part in merging.
158
+ return withSectionKeys(doc);
159
+ }
160
+
161
+ /** The localized overlays a docs read can apply. */
162
+ export const OVERLAY_LANGUAGES = ['zh', 'dense'];
163
+
164
+ /**
165
+ * Where the `lang` overlay of a doc file lives: `{topic}.doc.{lang}.mjs`.
166
+ * @param {string} docPath
167
+ * @param {string} lang
168
+ * @returns {string}
169
+ */
170
+ function overlayPath(docPath, lang) {
171
+ return path.join(
172
+ path.dirname(docPath),
173
+ `${path.basename(docPath, '.doc.mjs')}.doc.${lang}.mjs`,
174
+ );
175
+ }
176
+
177
+ /**
178
+ * The overlay languages a topic ships for its own file or any extension.
179
+ * @param {import('../../foundation/discovery/docs-discovery.mjs').DocsTopicEntry} entry
180
+ * @returns {string[]}
181
+ */
182
+ export function overlayLanguages(entry) {
183
+ const files = [entry.path, ...entry.extensions.map(ext => ext.path)];
184
+ return OVERLAY_LANGUAGES.filter(lang =>
185
+ files.some(file => fs.existsSync(overlayPath(file, lang))),
186
+ );
132
187
  }
133
188
 
134
189
  /**
@@ -146,6 +201,7 @@ export async function loadTopicDoc(entry, {lang} = {}) {
146
201
  * @returns {Promise<{
147
202
  * catalog: DocsCatalog,
148
203
  * docsData: import('./docs.type.mjs').DocsDetailResponse['data'],
204
+ * lang: string | null,
149
205
  * }>}
150
206
  */
151
207
  export async function resolveTopicDocs(topic, options = {}) {
@@ -166,5 +222,5 @@ export async function resolveTopicDocs(topic, options = {}) {
166
222
  }
167
223
 
168
224
  const docsData = await loadTopicDoc(entry, {lang: effectiveLang});
169
- return {catalog, docsData};
225
+ return {catalog, docsData, lang: effectiveLang};
170
226
  }
@@ -1,6 +1,21 @@
1
1
  // @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
2
2
  // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
3
 
4
+ /**
5
+ * Resolve token-ref blocks by inlining the referenced section's table.
6
+ * This allows section docs to reference token tables without duplicating data.
7
+ *
8
+ * The reference is resolved through the catalog, so a topic may point at one
9
+ * an integration contributed (or replaced) rather than only at a built-in. It
10
+ * names the section by key or authored title, so it resolves in every language.
11
+ * @param {import('../docs.type.mjs').DocsDetailResponse['data']} docsData
12
+ * @param {import('../../../foundation/discovery/docs-discovery.mjs').DocsCatalog} catalog
13
+ * @param {{lang?: string | null}} [options]
14
+ * @returns {Promise<import('../docs.type.mjs').DocsDetailResponse['data']>}
15
+ */
16
+ export function resolveTokenRefs(docsData: import("../docs.type.mjs").DocsDetailResponse["data"], catalog: import("../../../foundation/discovery/docs-discovery.mjs").DocsCatalog, { lang }?: {
17
+ lang?: string | null;
18
+ }): Promise<import("../docs.type.mjs").DocsDetailResponse["data"]>;
4
19
  /**
5
20
  * @param {string} topic
6
21
  * @param {object} [options]
@@ -12,18 +12,25 @@
12
12
  */
13
13
 
14
14
  import {loadTopicDoc, resolveTopicDocs} from '../_adapter.mjs';
15
+ import {
16
+ sectionKey,
17
+ sourceTitle,
18
+ withSourceTitle,
19
+ } from '../../../foundation/discovery/docs-section-key.mjs';
15
20
 
16
21
  /**
17
22
  * Resolve token-ref blocks by inlining the referenced section's table.
18
23
  * This allows section docs to reference token tables without duplicating data.
19
24
  *
20
25
  * The reference is resolved through the catalog, so a topic may point at one
21
- * an integration contributed (or replaced) rather than only at a built-in.
26
+ * an integration contributed (or replaced) rather than only at a built-in. It
27
+ * names the section by key or authored title, so it resolves in every language.
22
28
  * @param {import('../docs.type.mjs').DocsDetailResponse['data']} docsData
23
29
  * @param {import('../../../foundation/discovery/docs-discovery.mjs').DocsCatalog} catalog
30
+ * @param {{lang?: string | null}} [options]
24
31
  * @returns {Promise<import('../docs.type.mjs').DocsDetailResponse['data']>}
25
32
  */
26
- async function resolveTokenRefs(docsData, catalog) {
33
+ export async function resolveTokenRefs(docsData, catalog, {lang = null} = {}) {
27
34
  const resolved = {...docsData, sections: [...docsData.sections]};
28
35
  for (let si = 0; si < resolved.sections.length; si++) {
29
36
  const section = resolved.sections[si];
@@ -36,10 +43,12 @@ async function resolveTokenRefs(docsData, catalog) {
36
43
  newContent.push({type: 'prose', text: `[token-ref: unknown topic "${block.topic}"]`});
37
44
  continue;
38
45
  }
39
- const refDocs = await loadTopicDoc(refEntry);
46
+ const refDocs = await loadTopicDoc(refEntry, {lang});
47
+ const wanted = block.section.toLowerCase();
40
48
  const refSection = refDocs.sections.find(
41
49
  (/** @type {import('@astryxdesign/cli/authoring').ReferenceSection} */ s) =>
42
- s.title.toLowerCase() === block.section.toLowerCase(),
50
+ sectionKey(s) === block.section ||
51
+ sourceTitle(s).toLowerCase() === wanted,
43
52
  );
44
53
  if (!refSection) {
45
54
  newContent.push({type: 'prose', text: `[token-ref: section "${block.section}" not found in "${block.topic}"]`});
@@ -52,14 +61,20 @@ async function resolveTokenRefs(docsData, catalog) {
52
61
  }
53
62
  // If the referenced section has a previewType, attach it to our section
54
63
  if (refSection.previewType && !section.previewType) {
55
- resolved.sections[si] = {...section, previewType: refSection.previewType, content: newContent};
64
+ resolved.sections[si] = withSourceTitle(
65
+ {...section, previewType: refSection.previewType, content: newContent},
66
+ sourceTitle(section),
67
+ );
56
68
  }
57
69
  } else {
58
70
  newContent.push(block);
59
71
  }
60
72
  }
61
73
  if (resolved.sections[si] === section) {
62
- resolved.sections[si] = {...section, content: newContent};
74
+ resolved.sections[si] = withSourceTitle(
75
+ {...section, content: newContent},
76
+ sourceTitle(section),
77
+ );
63
78
  } else {
64
79
  resolved.sections[si].content = newContent;
65
80
  }
@@ -77,7 +92,7 @@ async function resolveTokenRefs(docsData, catalog) {
77
92
  * @returns {Promise<import('../docs.type.mjs').DocsDetailResponse>}
78
93
  */
79
94
  export async function detail(topic, options = {}) {
80
- const {catalog, docsData} = await resolveTopicDocs(topic, options);
81
- const resolved = await resolveTokenRefs(docsData, catalog);
95
+ const {catalog, docsData, lang} = await resolveTopicDocs(topic, options);
96
+ const resolved = await resolveTokenRefs(docsData, catalog, {lang});
82
97
  return {type: 'docs.detail', data: resolved};
83
98
  }
@@ -3,7 +3,7 @@
3
3
 
4
4
  /**
5
5
  * @param {string} topic
6
- * @param {string} sectionName
6
+ * @param {string} sectionName a section key, or a title (or part of one)
7
7
  * @param {object} [options]
8
8
  * @param {string} [options.lang]
9
9
  * @param {boolean} [options.zh]
@@ -1,25 +1,31 @@
1
1
  // Copyright (c) Meta Platforms, Inc. and affiliates.
2
2
 
3
3
  /**
4
- * @file docs.detail.section leaf — load a single named section of a topic.
4
+ * @file docs.detail.section leaf — load a single section of a topic.
5
5
  *
6
6
  * @input A topic name, a section query, and optional {lang, zh, dense}. Resolves
7
- * and loads the topic via the shared adapter, then finds the first section
8
- * whose title contains the (case-insensitive) query.
9
- * @output { type: 'docs.detail.section', data: ReferenceSection } — matching
10
- * `xds --json docs <topic> <section>`. Throws ERR_UNKNOWN_SECTION when no
11
- * section title matches.
7
+ * and loads the topic via the shared adapter, then finds the section by its
8
+ * stable key, then by exact title, then by a title that contains the query.
9
+ * @output { type: 'docs.detail.section', data: ReferenceSection } with any
10
+ * token-ref blocks inlined — matching `astryx --json docs <topic> <section>`.
11
+ * Throws ERR_UNKNOWN_SECTION when nothing matches, or when the query matches
12
+ * more than one section (the candidates come back as suggestions).
12
13
  * @position Leaf nested under api/docs/detail. Shares discovery/loading/
13
14
  * topic-resolution with the detail leaf via _adapter.mjs.
14
15
  */
15
16
 
16
17
  import {AstryxError} from '../../../error.mjs';
17
18
  import {ERROR_CODES} from '../../../../foundation/response/error-codes.mjs';
19
+ import {
20
+ findDocSection,
21
+ sectionKey,
22
+ } from '../../../../foundation/discovery/docs-section-key.mjs';
18
23
  import {resolveTopicDocs} from '../../_adapter.mjs';
24
+ import {resolveTokenRefs} from '../detail.mjs';
19
25
 
20
26
  /**
21
27
  * @param {string} topic
22
- * @param {string} sectionName
28
+ * @param {string} sectionName a section key, or a title (or part of one)
23
29
  * @param {object} [options]
24
30
  * @param {string} [options.lang]
25
31
  * @param {boolean} [options.zh]
@@ -28,10 +34,9 @@ import {resolveTopicDocs} from '../../_adapter.mjs';
28
34
  * @returns {Promise<import('../../docs.type.mjs').DocsDetailSectionResponse>}
29
35
  */
30
36
  export async function section(topic, sectionName, options = {}) {
31
- // An empty section name must error, not resolve to the first section via
32
- // `.includes('')`. The docs() dispatcher routes a falsy section to detail, but
33
- // the leaf must be safe on its own. A non-string would also throw a raw
34
- // TypeError below (`.toLowerCase()`) → downgrades to ERR_UNKNOWN.
37
+ // An empty section name must error, not resolve to the first section. The
38
+ // docs() dispatcher routes a falsy section to the topic, but the leaf must be
39
+ // safe on its own; a non-string would otherwise throw a raw TypeError.
35
40
  if (typeof sectionName !== 'string' || !sectionName.trim()) {
36
41
  throw new AstryxError(
37
42
  'A section name is required',
@@ -39,16 +44,31 @@ export async function section(topic, sectionName, options = {}) {
39
44
  ERROR_CODES.ERR_UNKNOWN_SECTION,
40
45
  );
41
46
  }
42
- const {docsData} = await resolveTopicDocs(topic, options);
47
+ const {catalog, docsData, lang} = await resolveTopicDocs(topic, options);
43
48
 
44
- const normalizedSection = sectionName.toLowerCase();
45
- const match = docsData.sections.find(s => s.title.toLowerCase().includes(normalizedSection));
49
+ const {section: match, candidates} = findDocSection(
50
+ docsData.sections,
51
+ sectionName,
52
+ );
46
53
  if (!match) {
54
+ const ambiguous = candidates.length > 1;
47
55
  throw new AstryxError(
48
- `Section "${sectionName}" not found in "${topic}"`,
49
- docsData.sections.map(s => ({name: s.title, reason: 'available section'})),
56
+ ambiguous
57
+ ? `Section "${sectionName}" matches ${candidates.length} sections in "${topic}". Read one by its key.`
58
+ : `Section "${sectionName}" not found in "${topic}"`,
59
+ (ambiguous ? candidates : docsData.sections).map(s => ({
60
+ name: sectionKey(s),
61
+ reason: s.title,
62
+ })),
50
63
  ERROR_CODES.ERR_UNKNOWN_SECTION,
51
64
  );
52
65
  }
53
- return {type: 'docs.detail.section', data: match};
66
+ // A section read on its own inlines its token refs, as the whole topic does;
67
+ // otherwise a section that is only a token-ref prints blank.
68
+ const {sections} = await resolveTokenRefs(
69
+ {...docsData, sections: [match]},
70
+ catalog,
71
+ {lang},
72
+ );
73
+ return {type: 'docs.detail.section', data: sections[0]};
54
74
  }
@@ -10,6 +10,7 @@
10
10
  import {describe, it, expect} from 'vitest';
11
11
  import {section} from './section.mjs';
12
12
  import {AstryxError} from '../../../error.mjs';
13
+ import {loadDocsCatalog, loadTopicDoc} from '../../_adapter.mjs';
13
14
 
14
15
  const SLOW = 30_000;
15
16
 
@@ -39,4 +40,43 @@ describe('docs.detail.section leaf', () => {
39
40
  code: 'ERR_UNKNOWN_SECTION',
40
41
  });
41
42
  }, SLOW);
43
+
44
+ it('reads a section by its stable key', async () => {
45
+ const doc = await loadTopicDoc((await loadDocsCatalog()).resolve('theme'));
46
+ const target = doc.sections[doc.sections.length - 1];
47
+ const res = await section('theme', target.id);
48
+ expect(res.data.title).toBe(target.title);
49
+ expect(res.data.id).toBe(target.id);
50
+ }, SLOW);
51
+
52
+ it('refuses a query that matches more than one section', async () => {
53
+ const err = await section('theme', 'e').catch(e => e);
54
+ expect(err).toBeInstanceOf(AstryxError);
55
+ expect(err.code).toBe('ERR_UNKNOWN_SECTION');
56
+ expect(err.message).toMatch(/matches \d+ sections/);
57
+ expect(err.suggestions.length).toBeGreaterThan(1);
58
+ }, SLOW);
59
+
60
+ it.each([null, 'zh', 'dense'])(
61
+ 'inlines token refs when a section is read on its own (lang %s)',
62
+ async lang => {
63
+ const catalog = await loadDocsCatalog();
64
+ let checked = 0;
65
+ for (const entry of catalog.entries()) {
66
+ const doc = await loadTopicDoc(entry);
67
+ for (const own of doc.sections) {
68
+ if (!own.content.some(block => block.type === 'token-ref')) continue;
69
+ const res = await section(entry.name, own.id, lang ? {lang} : {});
70
+ expect(res.data.content.length).toBeGreaterThan(0);
71
+ expect(res.data.content.some(block => block.type === 'token-ref')).toBe(
72
+ false,
73
+ );
74
+ expect(JSON.stringify(res.data.content)).not.toContain('[token-ref:');
75
+ checked += 1;
76
+ }
77
+ }
78
+ expect(checked).toBeGreaterThan(0);
79
+ },
80
+ SLOW,
81
+ );
42
82
  });
@@ -8,9 +8,12 @@
8
8
  * @param {string} [options.lang]
9
9
  * @param {boolean} [options.zh]
10
10
  * @param {boolean} [options.dense]
11
+ * @param {boolean} [options.index] return the topic's section index instead of
12
+ * the whole doc
11
13
  * @param {string} [options.cwd]
12
14
  * @returns {Promise<
13
15
  * import('./docs.type.mjs').DocsListResponse |
16
+ * import('./docs.type.mjs').DocsIndexResponse |
14
17
  * import('./docs.type.mjs').DocsDetailResponse |
15
18
  * import('./docs.type.mjs').DocsDetailSectionResponse
16
19
  * >}
@@ -19,9 +22,11 @@ export function docs(topic?: string, section?: string, options?: {
19
22
  lang?: string | undefined;
20
23
  zh?: boolean | undefined;
21
24
  dense?: boolean | undefined;
25
+ index?: boolean | undefined;
22
26
  cwd?: string | undefined;
23
- }): Promise<import("./docs.type.mjs").DocsListResponse | import("./docs.type.mjs").DocsDetailResponse | import("./docs.type.mjs").DocsDetailSectionResponse>;
27
+ }): Promise<import("./docs.type.mjs").DocsListResponse | import("./docs.type.mjs").DocsIndexResponse | import("./docs.type.mjs").DocsDetailResponse | import("./docs.type.mjs").DocsDetailSectionResponse>;
24
28
  import { list } from './list/list.mjs';
29
+ import { index } from './index/index.mjs';
25
30
  import { detail } from './detail/detail.mjs';
26
31
  import { section as sectionLeaf } from './detail/section/section.mjs';
27
- export { list, detail, sectionLeaf as section };
32
+ export { list, index, detail, sectionLeaf as section };
@@ -13,18 +13,20 @@ export const doc = {
13
13
  name: 'docs',
14
14
  displayName: 'docs()',
15
15
  summary:
16
- 'Read the reference docs: list every topic, one topic, or a single section of a topic.',
16
+ 'Read the reference docs: list every topic, one topic\'s sections, one section, or a whole topic.',
17
17
  description:
18
- 'Routes on its arguments: no topic lists every reference-doc topic; a topic ' +
19
- 'returns that full ReferenceDoc (with token-ref blocks inlined); a topic ' +
20
- 'plus a section returns the first section whose title contains the ' +
21
- '(case-insensitive) query. The topic set is the CLI\'s own docs plus the ' +
18
+ 'No topic lists every reference-doc topic; a topic returns that full ' +
19
+ 'ReferenceDoc; `index: true` returns the topic\'s section index instead ' +
20
+ '(each section\'s key, title, and summary); a topic plus a section returns ' +
21
+ 'that one section, found by its key, then its exact title, then a unique ' +
22
+ 'part of its title (an ambiguous query is refused). Token-ref blocks are ' +
23
+ 'inlined in every read. The topic set is the CLI\'s own docs plus the ' +
22
24
  'ones the project\'s configured integrations contribute, including any ' +
23
25
  'topic an integration replaces or extends, so it depends on the cwd. ' +
24
26
  'Overlay options select localized or dense variants.',
25
27
  importPath: '@astryxdesign/cli/api',
26
28
  signature:
27
- 'docs(topic?: string, section?: string, options?: DocsOptions): Promise<DocsListResponse | DocsDetailResponse | DocsDetailSectionResponse>',
29
+ 'docs(topic?: string, section?: string, options?: DocsOptions): Promise<DocsListResponse | DocsIndexResponse | DocsDetailResponse | DocsDetailSectionResponse>',
28
30
  keywords: [
29
31
  'docs',
30
32
  'documentation',
@@ -46,7 +48,7 @@ export const doc = {
46
48
  name: 'section',
47
49
  type: 'string',
48
50
  description:
49
- 'Section within the topic to return; matches the first section title that contains this (case-insensitive).',
51
+ "Section to return: its key (from the topic's index), its title, or a unique part of its title (case-insensitive).",
50
52
  },
51
53
  {
52
54
  name: 'options.lang',
@@ -63,6 +65,12 @@ export const doc = {
63
65
  type: 'boolean',
64
66
  description: 'Return the token-efficient dense doc variant.',
65
67
  },
68
+ {
69
+ name: 'options.index',
70
+ type: 'boolean',
71
+ description:
72
+ "Return the topic's section index (each section's key, title, and summary) instead of the whole doc.",
73
+ },
66
74
  {
67
75
  name: 'options.cwd',
68
76
  type: 'string',
@@ -81,10 +89,15 @@ export const doc = {
81
89
  description:
82
90
  "One topic's full ReferenceDoc, with token-ref blocks inlined.",
83
91
  },
92
+ {
93
+ type: 'docs.index',
94
+ description:
95
+ "One topic's section index (index: true): {name, title, description, sections: [{id, title, summary}]}.",
96
+ },
84
97
  {
85
98
  type: 'docs.detail.section',
86
99
  description:
87
- 'A single ReferenceSection of the topic: the first whose title contains the section query.',
100
+ 'One ReferenceSection of the topic, found by key or title, with token-ref blocks inlined.',
88
101
  },
89
102
  ],
90
103
  throws: [
@@ -94,13 +107,17 @@ export const doc = {
94
107
  },
95
108
  {
96
109
  code: 'ERR_UNKNOWN_SECTION',
97
- when: 'a section is requested but is empty or matches no section title in the topic',
110
+ when: 'a section is requested but is empty, matches no section, or matches more than one',
98
111
  },
99
112
  ],
100
113
  examples: [
101
114
  {label: 'List topics', code: 'const r = await docs();'},
102
115
  {label: 'Load a topic', code: "await docs('principles');"},
103
- {label: 'One section', code: "await docs('tokens', 'spacing');"},
116
+ {
117
+ label: "A topic's sections",
118
+ code: "await docs('principles', undefined, {index: true});",
119
+ },
120
+ {label: 'One section by key', code: "await docs('tokens', 'spacing');"},
104
121
  ],
105
122
  command: 'docs',
106
123
  related: ['search', 'component', 'hook', 'template'],
package/api/docs/docs.mjs CHANGED
@@ -3,24 +3,27 @@
3
3
  /**
4
4
  * @file Programmatic API for the docs command.
5
5
  *
6
- * Dispatcher + barrel. `docs()` routes by argument shape into one of three
6
+ * Dispatcher + barrel. `docs()` routes by argument shape into one of four
7
7
  * leaves, each projecting into a single { type, data } envelope:
8
8
  *
9
- * docs() -> list -> docs.list
10
- * docs(topic) -> detail -> docs.detail
11
- * docs(topic, section) -> section -> docs.detail.section
9
+ * docs() -> list -> docs.list
10
+ * docs(topic) -> detail -> docs.detail
11
+ * docs(topic, undefined, {index: true}) -> index -> docs.index
12
+ * docs(topic, section) -> section -> docs.detail.section
12
13
  *
13
- * The leaves live in list/, detail/, and detail/section/; the discovery,
14
- * overlay loading, and topic resolution they share sit in _adapter.mjs. This
15
- * module keeps the same `docs` export (and re-exports the leaves) so
16
- * api/index.mjs and the CLI consumer import from here unchanged.
14
+ * A topic read returns the whole doc, as it always has. The index is how a
15
+ * reader works progressively instead: list the topic's sections, then read one
16
+ * by its key. The leaves live in list/, index/, detail/, and detail/section/;
17
+ * the discovery, overlay loading, and topic resolution they share sit in
18
+ * _adapter.mjs.
17
19
  */
18
20
 
19
21
  import {list} from './list/list.mjs';
22
+ import {index} from './index/index.mjs';
20
23
  import {detail} from './detail/detail.mjs';
21
24
  import {section as sectionLeaf} from './detail/section/section.mjs';
22
25
 
23
- export {list, detail, sectionLeaf as section};
26
+ export {list, index, detail, sectionLeaf as section};
24
27
 
25
28
  /**
26
29
  * @param {string} [topic]
@@ -29,9 +32,12 @@ export {list, detail, sectionLeaf as section};
29
32
  * @param {string} [options.lang]
30
33
  * @param {boolean} [options.zh]
31
34
  * @param {boolean} [options.dense]
35
+ * @param {boolean} [options.index] return the topic's section index instead of
36
+ * the whole doc
32
37
  * @param {string} [options.cwd]
33
38
  * @returns {Promise<
34
39
  * import('./docs.type.mjs').DocsListResponse |
40
+ * import('./docs.type.mjs').DocsIndexResponse |
35
41
  * import('./docs.type.mjs').DocsDetailResponse |
36
42
  * import('./docs.type.mjs').DocsDetailSectionResponse
37
43
  * >}
@@ -39,5 +45,6 @@ export {list, detail, sectionLeaf as section};
39
45
  export async function docs(topic, section, options = {}) {
40
46
  if (!topic) return list(options);
41
47
  if (section) return sectionLeaf(topic, section, options);
48
+ if (options.index) return index(topic, options);
42
49
  return detail(topic, options);
43
50
  }
@@ -33,6 +33,12 @@ describe('docs() dispatcher routing', () => {
33
33
  expect(r.type).toBe('docs.detail');
34
34
  }, SLOW);
35
35
 
36
+ it('topic + index -> docs.index', async () => {
37
+ const {data} = await docs();
38
+ const r = await docs(data[0].topic, undefined, {index: true});
39
+ expect(r.type).toBe('docs.index');
40
+ }, SLOW);
41
+
36
42
  it('topic + section -> docs.detail.section', async () => {
37
43
  const {data} = await docs();
38
44
  let routed = null;