@docs-sdk-dev/reader 0.0.0-stage → 0.4.0-next.0
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/LICENSE +21 -0
- package/README.md +97 -2
- package/dist/annotation-projection.d.ts +35 -0
- package/dist/annotation-projection.js +78 -0
- package/dist/annotation-projection.js.map +1 -0
- package/dist/artifacts.d.ts +19 -0
- package/dist/artifacts.js +33 -0
- package/dist/artifacts.js.map +1 -0
- package/dist/ask.d.ts +41 -0
- package/dist/ask.js +89 -0
- package/dist/ask.js.map +1 -0
- package/dist/collection-view.d.ts +190 -0
- package/dist/collection-view.js +170 -0
- package/dist/collection-view.js.map +1 -0
- package/dist/composition.d.ts +199 -0
- package/dist/composition.js +473 -0
- package/dist/composition.js.map +1 -0
- package/dist/condition.d.ts +17 -0
- package/dist/condition.js +61 -0
- package/dist/condition.js.map +1 -0
- package/dist/content-schema.d.ts +182 -0
- package/dist/content-schema.js +288 -0
- package/dist/content-schema.js.map +1 -0
- package/dist/context-search.d.ts +168 -0
- package/dist/context-search.js +73 -0
- package/dist/context-search.js.map +1 -0
- package/dist/context-store.d.ts +93 -0
- package/dist/context-store.js +438 -0
- package/dist/context-store.js.map +1 -0
- package/dist/dimension-runtime.d.ts +203 -0
- package/dist/dimension-runtime.js +737 -0
- package/dist/dimension-runtime.js.map +1 -0
- package/dist/dimensions.d.ts +84 -0
- package/dist/dimensions.js +136 -0
- package/dist/dimensions.js.map +1 -0
- package/dist/directory-navigation.d.ts +60 -0
- package/dist/directory-navigation.js +41 -0
- package/dist/directory-navigation.js.map +1 -0
- package/dist/directory-sections.d.ts +9 -0
- package/dist/directory-sections.js +76 -0
- package/dist/directory-sections.js.map +1 -0
- package/dist/document-reader.d.ts +1017 -0
- package/dist/document-reader.js +396 -0
- package/dist/document-reader.js.map +1 -0
- package/dist/document-selection.d.ts +412 -0
- package/dist/document-selection.js +45 -0
- package/dist/document-selection.js.map +1 -0
- package/dist/evidence.d.ts +102 -0
- package/dist/evidence.js +104 -0
- package/dist/evidence.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +23 -0
- package/dist/model.d.ts +89 -0
- package/dist/model.js +31 -0
- package/dist/model.js.map +1 -0
- package/dist/module.d.ts +657 -0
- package/dist/module.js +199 -0
- package/dist/module.js.map +1 -0
- package/dist/navigation-view.d.ts +33 -0
- package/dist/navigation-view.js +68 -0
- package/dist/navigation-view.js.map +1 -0
- package/dist/node.d.ts +2004 -0
- package/dist/node.js +210 -0
- package/dist/node.js.map +1 -0
- package/dist/open-docs.d.ts +1396 -0
- package/dist/open-docs.js +198 -0
- package/dist/open-docs.js.map +1 -0
- package/dist/outline.d.ts +97 -0
- package/dist/outline.js +171 -0
- package/dist/outline.js.map +1 -0
- package/dist/persistence.d.ts +82 -0
- package/dist/persistence.js +377 -0
- package/dist/persistence.js.map +1 -0
- package/dist/projection-catalog.d.ts +23 -0
- package/dist/projection-catalog.js +45 -0
- package/dist/projection-catalog.js.map +1 -0
- package/dist/publication.d.ts +59 -0
- package/dist/publication.js +11 -0
- package/dist/publication.js.map +1 -0
- package/dist/reader.d.ts +236 -0
- package/dist/reader.js +400 -0
- package/dist/reader.js.map +1 -0
- package/dist/references.d.ts +54 -0
- package/dist/references.js +70 -0
- package/dist/references.js.map +1 -0
- package/dist/retrieval.d.ts +82 -0
- package/dist/retrieval.js +135 -0
- package/dist/retrieval.js.map +1 -0
- package/dist/search-view.d.ts +11 -0
- package/dist/search-view.js +30 -0
- package/dist/search-view.js.map +1 -0
- package/dist/search.d.ts +95 -0
- package/dist/search.js +234 -0
- package/dist/search.js.map +1 -0
- package/dist/semantic.d.ts +177 -0
- package/dist/semantic.js +104 -0
- package/dist/semantic.js.map +1 -0
- package/dist/skills.d.ts +21 -0
- package/dist/skills.js +42 -0
- package/dist/skills.js.map +1 -0
- package/docs/index.md +10 -0
- package/docs/sdk/composition.md +72 -0
- package/docs/sdk/reader-storage.md +70 -0
- package/docs/sdk/reader.md +99 -0
- package/docs/zh-CN/sdk/composition.md +72 -0
- package/docs/zh-CN/sdk/reader-storage.md +70 -0
- package/docs/zh-CN/sdk/reader.md +99 -0
- package/package.json +69 -4
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { factContext, openPinnedReader } from "./document-reader.js";
|
|
2
|
+
import { createDocumentSelector } from "./document-selection.js";
|
|
3
|
+
import { contextualSearch } from "./context-search.js";
|
|
4
|
+
import { DocsError } from "./model.js";
|
|
5
|
+
import { doc } from "./references.js";
|
|
6
|
+
import { resolveContext } from "./semantic.js";
|
|
7
|
+
|
|
8
|
+
// src/open-docs.ts
|
|
9
|
+
async function openDocs({ revision, host, packages = [], publicSearch }) {
|
|
10
|
+
const modules = new Map(packages.map((module) => [module.software.name, module]));
|
|
11
|
+
if (modules.size !== packages.length) throw new DocsError("DOCS_REFERENCE_CONFLICT", "Select one publication per imported software package.");
|
|
12
|
+
const pinned = await openPinnedReader(revision, host);
|
|
13
|
+
const selectDocument = createDocumentSelector(pinned.reader, pinned.manifest);
|
|
14
|
+
let retrieval;
|
|
15
|
+
function searchEngine() {
|
|
16
|
+
retrieval ??= Promise.all([import("./retrieval.js"), import("./search-view.js")]).then(async ([{ createRetrieval }, { readPublishedSearchIndex }]) => {
|
|
17
|
+
const index = await readPublishedSearchIndex(pinned);
|
|
18
|
+
const publication = {
|
|
19
|
+
revision: pinned.buildRevision,
|
|
20
|
+
inventoryRevision: revision,
|
|
21
|
+
index
|
|
22
|
+
};
|
|
23
|
+
return createRetrieval(publicSearch ? {
|
|
24
|
+
...publication,
|
|
25
|
+
audience: "public",
|
|
26
|
+
search: publicSearch
|
|
27
|
+
} : publication);
|
|
28
|
+
}).catch((error) => {
|
|
29
|
+
retrieval = void 0;
|
|
30
|
+
throw error;
|
|
31
|
+
});
|
|
32
|
+
return retrieval;
|
|
33
|
+
}
|
|
34
|
+
let facts;
|
|
35
|
+
async function semantic() {
|
|
36
|
+
const pointer = pinned.manifest.semantic;
|
|
37
|
+
if (!pointer) return void 0;
|
|
38
|
+
facts ??= import("./reader.js").then(({ createSemanticLoader }) => createSemanticLoader(pointer, pinned.readText)).catch((error) => {
|
|
39
|
+
facts = void 0;
|
|
40
|
+
throw error;
|
|
41
|
+
});
|
|
42
|
+
return facts;
|
|
43
|
+
}
|
|
44
|
+
function scoped(context) {
|
|
45
|
+
async function read(ref, options) {
|
|
46
|
+
const target = typeof ref === "string" ? doc(ref) : ref;
|
|
47
|
+
const section = options?.section ?? target.section;
|
|
48
|
+
const result = selectDocument({
|
|
49
|
+
...target,
|
|
50
|
+
dimensions: context
|
|
51
|
+
}, { fallback: options?.fallback });
|
|
52
|
+
if (result.status === "ambiguous") return result;
|
|
53
|
+
if (result.status === "unsupported-context") return {
|
|
54
|
+
status: "unavailable",
|
|
55
|
+
available: result.available
|
|
56
|
+
};
|
|
57
|
+
if (result.status === "not-found") return { status: "missing" };
|
|
58
|
+
const value = await pinned.reader.read(result.value.identity, section, void 0, result.effective);
|
|
59
|
+
if (!value) return { status: "missing" };
|
|
60
|
+
return {
|
|
61
|
+
status: result.status,
|
|
62
|
+
value,
|
|
63
|
+
identity: result.value.identity,
|
|
64
|
+
revision,
|
|
65
|
+
...result.resolution && { resolution: result.resolution }
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
async function readPackage(ref) {
|
|
69
|
+
const module = modules.get(ref.package);
|
|
70
|
+
if (!module) throw new DocsError("DOCS_REFERENCE_DENIED", "The host has not supplied a pinned publication for " + ref.package + ".");
|
|
71
|
+
const result = await module.readExport(ref.export, context);
|
|
72
|
+
if (result.status === "exact" && result.kind !== "markdown") throw new DocsError("DOCS_REFERENCE_INVALID", "Document composition requires a page or section export: " + ref.export);
|
|
73
|
+
if (result.status === "not-found") return {
|
|
74
|
+
status: "missing",
|
|
75
|
+
ref
|
|
76
|
+
};
|
|
77
|
+
if (result.status === "unsupported-context") return {
|
|
78
|
+
status: "unavailable",
|
|
79
|
+
available: result.available,
|
|
80
|
+
ref
|
|
81
|
+
};
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
function compose(refs) {
|
|
85
|
+
const targets = "content" in refs ? refs.content.map((item) => item.include) : refs;
|
|
86
|
+
return Promise.all(targets.map((ref) => typeof ref === "object" && "package" in ref ? readPackage(ref) : read(ref)));
|
|
87
|
+
}
|
|
88
|
+
async function retrieve(query, options = {}) {
|
|
89
|
+
options.signal?.throwIfAborted();
|
|
90
|
+
const requested = {
|
|
91
|
+
...context,
|
|
92
|
+
...options.context
|
|
93
|
+
};
|
|
94
|
+
const retrieval = await searchEngine();
|
|
95
|
+
if (!pinned.manifest.dimensionDeclarations?.length) return retrieval.query(query, {
|
|
96
|
+
...options,
|
|
97
|
+
context: requested
|
|
98
|
+
});
|
|
99
|
+
const edition = contextualSearch(pinned.manifest, pinned.reader, requested).options;
|
|
100
|
+
return retrieval.query(query, {
|
|
101
|
+
...options,
|
|
102
|
+
locale: options.locale ?? edition.locale,
|
|
103
|
+
context: void 0,
|
|
104
|
+
acceptDocument: (variant) => edition.acceptDocument(variant) && (!options.acceptDocument || options.acceptDocument(variant))
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
revision,
|
|
109
|
+
read,
|
|
110
|
+
readPackage,
|
|
111
|
+
compose,
|
|
112
|
+
manifest: pinned.manifest,
|
|
113
|
+
readText: pinned.readText,
|
|
114
|
+
reader: pinned.reader,
|
|
115
|
+
async resolve(id, dimensions = {}) {
|
|
116
|
+
const loader = await semantic();
|
|
117
|
+
return loader ? loader.resolve(id, factContext(pinned.reader, id, {
|
|
118
|
+
...context,
|
|
119
|
+
...dimensions
|
|
120
|
+
})) : { status: "not-found" };
|
|
121
|
+
},
|
|
122
|
+
/** Resolve only the members declared by a published facts collection, in its recorded order. */
|
|
123
|
+
async collection(name, project, options = {}) {
|
|
124
|
+
const loader = await semantic();
|
|
125
|
+
const catalog = "catalog:" + name;
|
|
126
|
+
if (!loader || (await loader.resolve(catalog, factContext(pinned.reader, catalog, context))).status !== "exact") throw new DocsError("DOCS_QUERY_SOURCE", "Missing published collection " + name + ".");
|
|
127
|
+
const membership = await loader.relations(catalog, {
|
|
128
|
+
type: "contains",
|
|
129
|
+
context: factContext(pinned.reader, catalog, context)
|
|
130
|
+
});
|
|
131
|
+
const ids = [...new Set(membership.map((edge) => edge.target))].filter((id) => !options.type || id.startsWith(options.type + ":"));
|
|
132
|
+
const records = [];
|
|
133
|
+
for (const id of ids) {
|
|
134
|
+
const selected = await loader.resolve(id, factContext(pinned.reader, id, context));
|
|
135
|
+
if (selected.status === "ambiguous") throw new DocsError("DOCS_QUERY_AMBIGUOUS", "Ambiguous editions of " + id + ".");
|
|
136
|
+
if (selected.status !== "exact") continue;
|
|
137
|
+
const variant = selected.value;
|
|
138
|
+
records.push({
|
|
139
|
+
id,
|
|
140
|
+
context: variant.context,
|
|
141
|
+
revision: variant.revision,
|
|
142
|
+
value: project(variant.data, id),
|
|
143
|
+
provenance: variant.provenance
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
return records;
|
|
147
|
+
},
|
|
148
|
+
async entities(type, project, subjectPrefix = "") {
|
|
149
|
+
const records = [];
|
|
150
|
+
const loader = await semantic();
|
|
151
|
+
if (!loader) return records;
|
|
152
|
+
for await (const entity of loader.entities({
|
|
153
|
+
type,
|
|
154
|
+
subjectPrefix
|
|
155
|
+
})) {
|
|
156
|
+
const selected = resolveContext(entity.variants, factContext(pinned.reader, entity.id, context));
|
|
157
|
+
if (selected.status === "ambiguous") throw new DocsError("DOCS_QUERY_AMBIGUOUS", "Ambiguous editions of " + entity.id + ".");
|
|
158
|
+
if (selected.status !== "exact") continue;
|
|
159
|
+
const variant = selected.value;
|
|
160
|
+
records.push({
|
|
161
|
+
id: entity.id,
|
|
162
|
+
context: variant.context,
|
|
163
|
+
revision: variant.revision,
|
|
164
|
+
provenance: variant.provenance,
|
|
165
|
+
value: project(variant.data, entity.id)
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
return records;
|
|
169
|
+
},
|
|
170
|
+
retrieve,
|
|
171
|
+
async search(query, options) {
|
|
172
|
+
options?.signal?.throwIfAborted();
|
|
173
|
+
if (!pinned.manifest.search || !query.trim()) return [];
|
|
174
|
+
return (await retrieve(query, options)).hits.map(({ result }) => result);
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
function bindContext(requested) {
|
|
179
|
+
const invalid = pinned.reader.context({
|
|
180
|
+
scope: "",
|
|
181
|
+
id: ""
|
|
182
|
+
}, { requested }).reasons.find(({ reason }) => reason === "value");
|
|
183
|
+
if (invalid) throw new DocsError("DOCS_CONTEXT_UNAVAILABLE", "Unsupported " + invalid.key + " choice: " + invalid.requested, { field: invalid.key });
|
|
184
|
+
return {
|
|
185
|
+
...scoped(requested),
|
|
186
|
+
withContext(dimensions) {
|
|
187
|
+
return bindContext({
|
|
188
|
+
...requested,
|
|
189
|
+
...dimensions
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
return bindContext({});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export { openDocs };
|
|
198
|
+
//# sourceMappingURL=open-docs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"open-docs.js","names":[],"sources":["../src/open-docs.ts"],"sourcesContent":["import { factContext } from \"./document-reader.js\"\nimport { createDocumentSelector } from \"./document-selection.js\"\nimport { contextualSearch } from \"./context-search.js\"\nimport { openPinnedReader, type PublicationHost } from \"./document-reader.js\"\nimport { DocsError } from \"./model.js\"\nimport type { OpenDocumentModule } from \"./module.js\"\nimport { doc, type ContentView, type packageDoc } from \"./references.js\"\nimport type { CollectionRecord } from \"./collection-view.js\"\nimport type { createSemanticLoader } from \"./reader.js\"\nimport { resolveContext, type Context, type EntityType } from \"./semantic.js\"\nimport type { createRetrieval, RetrievalQueryOptions, SearchDefinition } from \"./retrieval.js\"\n\ntype LocalRef = string | ReturnType<typeof doc>\ntype PackageRef = ReturnType<typeof packageDoc>\ntype ContextBound<Reader> = Reader & {\n withContext(dimensions: Context): ContextBound<Reader>\n}\n\nexport async function openDocs({\n revision,\n host,\n packages = [],\n publicSearch,\n}: {\n revision: string\n host: PublicationHost\n packages?: readonly OpenDocumentModule[]\n /** Opt in only for pinned public artifacts. The adapter receives search queries. */\n publicSearch?: SearchDefinition\n}) {\n const modules = new Map(packages.map((module) => [module.software.name, module]))\n if (modules.size !== packages.length)\n throw new DocsError(\n \"DOCS_REFERENCE_CONFLICT\",\n \"Select one publication per imported software package.\",\n )\n const pinned = await openPinnedReader(revision, host)\n const selectDocument = createDocumentSelector(pinned.reader, pinned.manifest)\n let retrieval: Promise<ReturnType<typeof createRetrieval>> | undefined\n function searchEngine() {\n retrieval ??= Promise.all([import(\"./retrieval.js\"), import(\"./search-view.js\")])\n .then(async ([{ createRetrieval }, { readPublishedSearchIndex }]) => {\n const index = await readPublishedSearchIndex(pinned)\n const publication = { revision: pinned.buildRevision, inventoryRevision: revision, index }\n return createRetrieval(\n publicSearch ? { ...publication, audience: \"public\", search: publicSearch } : publication,\n )\n })\n .catch((error) => {\n retrieval = undefined\n throw error\n })\n return retrieval\n }\n let facts: Promise<ReturnType<typeof createSemanticLoader>> | undefined\n async function semantic() {\n const pointer = pinned.manifest.semantic\n if (!pointer) return undefined\n facts ??= import(\"./reader.js\")\n .then(({ createSemanticLoader }) => createSemanticLoader(pointer, pinned.readText))\n .catch((error) => {\n facts = undefined\n throw error\n })\n return facts\n }\n function scoped(context: Context) {\n async function read(\n ref: string | ReturnType<typeof doc>,\n options?: { section?: string; fallback?: Context },\n ) {\n const target = typeof ref === \"string\" ? doc(ref) : ref\n const section = options?.section ?? target.section\n const result = selectDocument(\n { ...target, dimensions: context },\n { fallback: options?.fallback },\n )\n if (result.status === \"ambiguous\") return result\n if (result.status === \"unsupported-context\")\n return { status: \"unavailable\" as const, available: result.available }\n if (result.status === \"not-found\") return { status: \"missing\" as const }\n const value = await pinned.reader.read(\n result.value.identity,\n section,\n undefined,\n result.effective,\n )\n if (!value) return { status: \"missing\" as const }\n return {\n status: result.status,\n value,\n identity: result.value.identity,\n revision,\n ...(result.resolution && { resolution: result.resolution }),\n }\n }\n async function readPackage(ref: PackageRef) {\n const module = modules.get(ref.package)\n if (!module)\n throw new DocsError(\n \"DOCS_REFERENCE_DENIED\",\n \"The host has not supplied a pinned publication for \" + ref.package + \".\",\n )\n const result = await module.readExport(ref.export, context)\n if (result.status === \"exact\" && result.kind !== \"markdown\")\n throw new DocsError(\n \"DOCS_REFERENCE_INVALID\",\n \"Document composition requires a page or section export: \" + ref.export,\n )\n if (result.status === \"not-found\") return { status: \"missing\" as const, ref }\n if (result.status === \"unsupported-context\")\n return { status: \"unavailable\" as const, available: result.available, ref }\n return result\n }\n function compose(refs: readonly LocalRef[]): Promise<Awaited<ReturnType<typeof read>>[]>\n function compose(\n refs: readonly (LocalRef | PackageRef)[] | ContentView,\n ): Promise<(Awaited<ReturnType<typeof read>> | Awaited<ReturnType<typeof readPackage>>)[]>\n function compose(refs: readonly (LocalRef | PackageRef)[] | ContentView) {\n const targets = \"content\" in refs ? refs.content.map((item) => item.include) : refs\n return Promise.all(\n targets.map((ref) =>\n typeof ref === \"object\" && \"package\" in ref ? readPackage(ref) : read(ref),\n ),\n )\n }\n async function retrieve(query: string, options: RetrievalQueryOptions = {}) {\n options.signal?.throwIfAborted()\n const requested = { ...context, ...options.context }\n const retrieval = await searchEngine()\n if (!pinned.manifest.dimensionDeclarations?.length)\n return retrieval.query(query, { ...options, context: requested })\n const edition = contextualSearch(pinned.manifest, pinned.reader, requested).options\n return retrieval.query(query, {\n ...options,\n locale: options.locale ?? edition.locale,\n context: undefined,\n acceptDocument: (variant) =>\n edition.acceptDocument(variant) &&\n (!options.acceptDocument || options.acceptDocument(variant)),\n })\n }\n return {\n revision,\n read,\n readPackage,\n compose,\n manifest: pinned.manifest,\n readText: pinned.readText,\n reader: pinned.reader,\n async resolve(id: string, dimensions: Context = {}) {\n const loader = await semantic()\n return loader\n ? loader.resolve(id, factContext(pinned.reader, id, { ...context, ...dimensions }))\n : { status: \"not-found\" as const }\n },\n /** Resolve only the members declared by a published facts collection, in its recorded order. */\n async collection<Value>(\n name: string,\n project: (data: unknown, id: string) => Value,\n options: { type?: EntityType } = {},\n ) {\n const loader = await semantic()\n const catalog = \"catalog:\" + name\n if (\n !loader ||\n (await loader.resolve(catalog, factContext(pinned.reader, catalog, context))).status !==\n \"exact\"\n )\n throw new DocsError(\"DOCS_QUERY_SOURCE\", \"Missing published collection \" + name + \".\")\n const membership = await loader.relations(catalog, {\n type: \"contains\",\n context: factContext(pinned.reader, catalog, context),\n })\n const ids = [...new Set(membership.map((edge) => edge.target))].filter(\n (id) => !options.type || id.startsWith(options.type + \":\"),\n )\n const records: CollectionRecord<Value>[] = []\n for (const id of ids) {\n const selected = await loader.resolve(id, factContext(pinned.reader, id, context))\n if (selected.status === \"ambiguous\")\n throw new DocsError(\"DOCS_QUERY_AMBIGUOUS\", \"Ambiguous editions of \" + id + \".\")\n if (selected.status !== \"exact\") continue\n const variant = selected.value\n records.push({\n id,\n context: variant.context,\n revision: variant.revision,\n value: project(variant.data, id),\n provenance: variant.provenance,\n })\n }\n return records\n },\n async entities<Value>(\n type: EntityType,\n project: (data: unknown, id: string) => Value,\n subjectPrefix = \"\",\n ) {\n const records: CollectionRecord<Value>[] = []\n const loader = await semantic()\n if (!loader) return records\n for await (const entity of loader.entities({ type, subjectPrefix })) {\n const selected = resolveContext(\n entity.variants,\n factContext(pinned.reader, entity.id, context),\n )\n if (selected.status === \"ambiguous\")\n throw new DocsError(\"DOCS_QUERY_AMBIGUOUS\", \"Ambiguous editions of \" + entity.id + \".\")\n if (selected.status !== \"exact\") continue\n const variant = selected.value\n records.push({\n id: entity.id,\n context: variant.context,\n revision: variant.revision,\n provenance: variant.provenance,\n value: project(variant.data, entity.id),\n })\n }\n return records\n },\n retrieve,\n async search(query: string, options?: RetrievalQueryOptions) {\n options?.signal?.throwIfAborted()\n if (!pinned.manifest.search || !query.trim()) return []\n return (await retrieve(query, options)).hits.map(({ result }) => result)\n },\n }\n }\n function bindContext(requested: Context): ContextBound<ReturnType<typeof scoped>> {\n const invalid = pinned.reader\n .context({ scope: \"\", id: \"\" }, { requested })\n .reasons.find(({ reason }) => reason === \"value\")\n if (invalid)\n throw new DocsError(\n \"DOCS_CONTEXT_UNAVAILABLE\",\n \"Unsupported \" + invalid.key + \" choice: \" + invalid.requested,\n {\n field: invalid.key,\n },\n )\n return {\n ...scoped(requested),\n withContext(dimensions) {\n return bindContext({ ...requested, ...dimensions })\n },\n }\n }\n return bindContext({})\n}\n"],"mappings":";;;;;;;;AAkBA,eAAsB,SAAS,EAC7B,UACA,MACA,WAAW,CAAC,GACZ,gBAOC;CACD,MAAM,UAAU,IAAI,IAAI,SAAS,KAAK,WAAW,CAAC,OAAO,SAAS,MAAM,MAAM,CAAC,CAAC;CAChF,IAAI,QAAQ,SAAS,SAAS,QAC5B,MAAM,IAAI,UACR,2BACA,uDACF;CACF,MAAM,SAAS,MAAM,iBAAiB,UAAU,IAAI;CACpD,MAAM,iBAAiB,uBAAuB,OAAO,QAAQ,OAAO,QAAQ;CAC5E,IAAI;CACJ,SAAS,eAAe;EACtB,cAAc,QAAQ,IAAI,CAAC,OAAO,mBAAmB,OAAO,mBAAmB,CAAC,CAAC,CAC9E,KAAK,OAAO,CAAC,EAAE,mBAAmB,EAAE,gCAAgC;GACnE,MAAM,QAAQ,MAAM,yBAAyB,MAAM;GACnD,MAAM,cAAc;IAAE,UAAU,OAAO;IAAe,mBAAmB;IAAU;GAAM;GACzF,OAAO,gBACL,eAAe;IAAE,GAAG;IAAa,UAAU;IAAU,QAAQ;GAAa,IAAI,WAChF;EACF,CAAC,CAAC,CACD,OAAO,UAAU;GAChB,YAAY;GACZ,MAAM;EACR,CAAC;EACH,OAAO;CACT;CACA,IAAI;CACJ,eAAe,WAAW;EACxB,MAAM,UAAU,OAAO,SAAS;EAChC,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KACzB,QAAQ,KAAK;MACX,IAAI,OAAO;MACX,SAAS,QAAQ;MACjB,UAAU,QAAQ;MAClB,YAAY,QAAQ;MACpB,OAAO,QAAQ,QAAQ,MAAM,OAAO,EAAE;KACxC,CAAC;IACH;IACA,OAAO;GACT;GACA;GACA,MAAM,OAAO,OAAe,SAAiC;IAC3D,SAAS,QAAQ,eAAe;IAChC,IAAI,CAAC,OAAO,SAAS,UAAU,CAAC,MAAM,KAAK,GAAG,OAAO,CAAC;IACtD,QAAQ,MAAM,SAAS,OAAO,OAAO,EAAC,CAAE,KAAK,KAAK,EAAE,aAAa,MAAM;GACzE;EACF;CACF;CACA,SAAS,YAAY,WAA6D;EAChF,MAAM,UAAU,OAAO,OACpB,QAAQ;GAAE,OAAO;GAAI,IAAI;EAAG,GAAG,EAAE,UAAU,CAAC,CAAC,CAC7C,QAAQ,MAAM,EAAE,aAAa,WAAW,OAAO;EAClD,IAAI,SACF,MAAM,IAAI,UACR,4BACA,iBAAiB,QAAQ,MAAM,cAAc,QAAQ,WACrD,EACE,OAAO,QAAQ,IACjB,CACF;EACF,OAAO;GACL,GAAG,OAAO,SAAS;GACnB,YAAY,YAAY;IACtB,OAAO,YAAY;KAAE,GAAG;KAAW,GAAG;IAAW,CAAC;GACpD;EACF;CACF;CACA,OAAO,YAAY,CAAC,CAAC;AACvB"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { type Provenance } from "./model.js";
|
|
2
|
+
/** Describe a rendered anchor using the domain vocabulary of its producer. */
|
|
3
|
+
export interface OutlineContribution {
|
|
4
|
+
anchor: string;
|
|
5
|
+
kind: string;
|
|
6
|
+
text?: string;
|
|
7
|
+
reference?: string;
|
|
8
|
+
parent?: string;
|
|
9
|
+
/** Independent of headings. The index counts preceding rendered headings within the projection. */
|
|
10
|
+
position?: {
|
|
11
|
+
index: number;
|
|
12
|
+
level: number;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export type ProjectedOutline = Omit<OutlineContribution, "position" | "anchor"> & {
|
|
16
|
+
anchor?: string;
|
|
17
|
+
level?: number;
|
|
18
|
+
};
|
|
19
|
+
export interface OutlineEntry extends Omit<OutlineContribution, "position"> {
|
|
20
|
+
text: string;
|
|
21
|
+
level: number;
|
|
22
|
+
section: string;
|
|
23
|
+
/** A view may group an entry without changing its parent or anchor. */
|
|
24
|
+
group?: string;
|
|
25
|
+
provenance?: readonly Provenance[];
|
|
26
|
+
}
|
|
27
|
+
/** Serializable policy for a selected view. */
|
|
28
|
+
export interface OutlineView {
|
|
29
|
+
mode?: "compact" | "full";
|
|
30
|
+
include?: readonly string[];
|
|
31
|
+
exclude?: readonly string[];
|
|
32
|
+
depth?: readonly [number, number];
|
|
33
|
+
groupBy?: "kind" | "section";
|
|
34
|
+
sort?: "document" | "title";
|
|
35
|
+
}
|
|
36
|
+
/** An authoring definition. Discovery supplies the name when omitted. */
|
|
37
|
+
export interface OutlineDefinition extends OutlineView {
|
|
38
|
+
id?: string;
|
|
39
|
+
}
|
|
40
|
+
/** null clears inherited policies and restores the host's default view. */
|
|
41
|
+
export type OutlineSetting = string | OutlineView | null;
|
|
42
|
+
export declare function validateOutlineSetting(setting: unknown): setting is OutlineSetting;
|
|
43
|
+
/** Additional organization for an in-memory view. Callbacks receive canonical entries. */
|
|
44
|
+
export interface OutlineProjection extends OutlineDefinition {
|
|
45
|
+
filter?: (entry: Readonly<OutlineEntry>) => boolean;
|
|
46
|
+
group?: (entry: Readonly<OutlineEntry>) => string | undefined;
|
|
47
|
+
compare?: (left: Readonly<OutlineEntry>, right: Readonly<OutlineEntry>) => number;
|
|
48
|
+
}
|
|
49
|
+
export declare const outlineViews: {
|
|
50
|
+
readonly compact: {
|
|
51
|
+
readonly mode: "compact";
|
|
52
|
+
readonly exclude: readonly ["api-parameter", "api-response"];
|
|
53
|
+
};
|
|
54
|
+
readonly full: {
|
|
55
|
+
readonly mode: "full";
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
export declare function defineOutline(view: OutlineDefinition & {
|
|
59
|
+
id: string;
|
|
60
|
+
}): OutlineDefinition & {
|
|
61
|
+
id: string;
|
|
62
|
+
};
|
|
63
|
+
export declare function defineOutline(view?: OutlineProjection): OutlineProjection;
|
|
64
|
+
interface OutlineSource {
|
|
65
|
+
headings?: readonly {
|
|
66
|
+
id: string;
|
|
67
|
+
text: string;
|
|
68
|
+
depth: number;
|
|
69
|
+
}[];
|
|
70
|
+
provenance?: readonly Provenance[];
|
|
71
|
+
outline?: readonly OutlineContribution[];
|
|
72
|
+
}
|
|
73
|
+
export interface OutlineDocument {
|
|
74
|
+
outlineView?: OutlineView | null;
|
|
75
|
+
sections: readonly (OutlineSource & {
|
|
76
|
+
id: string;
|
|
77
|
+
title?: string;
|
|
78
|
+
artifacts?: readonly OutlineSource[];
|
|
79
|
+
})[];
|
|
80
|
+
}
|
|
81
|
+
declare const outlineContract: {
|
|
82
|
+
contribution(item: OutlineContribution, existing: ReadonlyMap<string, OutlineContribution>, section: string): void;
|
|
83
|
+
entry(anchor: string, item: OutlineContribution | undefined, existing: ReadonlySet<string>, section: string): void;
|
|
84
|
+
position(item: OutlineContribution, headingCount: number, section: string): void;
|
|
85
|
+
finish(remaining: ReadonlyMap<string, OutlineContribution>, section: string): void;
|
|
86
|
+
};
|
|
87
|
+
/** Validate and select entries from the canonical outline. A hidden parent promotes its children. */
|
|
88
|
+
export declare function semanticOutline(document: OutlineDocument, view?: OutlineProjection): OutlineEntry[];
|
|
89
|
+
/** Publications already passed the compiler's structural anchor checks. */
|
|
90
|
+
export declare function publishedOutline(document: OutlineDocument, view?: OutlineProjection, validate?: typeof outlineContract): OutlineEntry[];
|
|
91
|
+
export interface OutlineBranch {
|
|
92
|
+
entry: OutlineEntry;
|
|
93
|
+
children: OutlineBranch[];
|
|
94
|
+
}
|
|
95
|
+
/** Group a selected outline by visible parent without needing the document or inspecting the DOM. */
|
|
96
|
+
export declare function outlineTree(entries: readonly OutlineEntry[]): OutlineBranch[];
|
|
97
|
+
export {};
|
package/dist/outline.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { DocsError } from "./model.js";
|
|
2
|
+
|
|
3
|
+
// src/outline.ts
|
|
4
|
+
function validateOutlineSetting(setting) {
|
|
5
|
+
if (setting === null) return true;
|
|
6
|
+
if (typeof setting === "string") return /^[a-zA-Z0-9][a-zA-Z0-9_./-]*$/.test(setting);
|
|
7
|
+
if (!setting || typeof setting !== "object" || Array.isArray(setting)) return false;
|
|
8
|
+
if (Object.keys(setting).some((key) => ![
|
|
9
|
+
"mode",
|
|
10
|
+
"include",
|
|
11
|
+
"exclude",
|
|
12
|
+
"depth",
|
|
13
|
+
"groupBy",
|
|
14
|
+
"sort"
|
|
15
|
+
].includes(key))) return false;
|
|
16
|
+
defineOutline(setting);
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
const outlineViews = {
|
|
20
|
+
compact: {
|
|
21
|
+
mode: "compact",
|
|
22
|
+
exclude: ["api-parameter", "api-response"]
|
|
23
|
+
},
|
|
24
|
+
full: { mode: "full" }
|
|
25
|
+
};
|
|
26
|
+
function defineOutline(view = {}) {
|
|
27
|
+
for (const kinds of [view.include, view.exclude]) if (kinds !== void 0 && (!Array.isArray(kinds) || kinds.some((kind) => typeof kind !== "string" || !kind.trim()))) throw new DocsError("DOCS_OUTLINE_INVALID", "Specify outline kinds as a list of names.");
|
|
28
|
+
if (view.groupBy !== void 0 && view.groupBy !== "kind" && view.groupBy !== "section") throw new DocsError("DOCS_OUTLINE_INVALID", "Expected an outline group of kind or section.");
|
|
29
|
+
if (view.sort !== void 0 && view.sort !== "title" && view.sort !== "document") throw new DocsError("DOCS_OUTLINE_INVALID", "Expected document or title outline order.");
|
|
30
|
+
if (view.mode !== void 0 && view.mode !== "compact" && view.mode !== "full") throw new DocsError("DOCS_OUTLINE_INVALID", "Expected a compact or full outline view.");
|
|
31
|
+
if (view.depth !== void 0 && (!Array.isArray(view.depth) || view.depth.length !== 2 || !view.depth.every((value) => Number.isInteger(value) && value >= 1 && value <= 6) || view.depth[0] > view.depth[1])) throw new DocsError("DOCS_OUTLINE_DEPTH", "Outline depth must be an inclusive range between 1 and 6.");
|
|
32
|
+
return view;
|
|
33
|
+
}
|
|
34
|
+
const outlineContract = {
|
|
35
|
+
contribution(item, existing, section) {
|
|
36
|
+
if (!item.kind || existing.has(item.anchor) || item.position && item.anchor === section) throw new DocsError("DOCS_OUTLINE_INVALID", "Invalid or duplicate outline contribution.", {
|
|
37
|
+
section,
|
|
38
|
+
field: item.anchor
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
entry(anchor, item, existing, section) {
|
|
42
|
+
if (existing.has(anchor)) throw new DocsError("DOCS_OUTLINE_INVALID", "Duplicate outline anchor " + anchor, { section });
|
|
43
|
+
if (item?.parent !== void 0 && !existing.has(item.parent)) throw new DocsError("DOCS_OUTLINE_INVALID", "Outline parent must name an earlier anchor.", {
|
|
44
|
+
section,
|
|
45
|
+
field: item.parent
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
position(item, headingCount, section) {
|
|
49
|
+
const { index, level } = item.position;
|
|
50
|
+
if (!item.text?.trim() || !Number.isSafeInteger(index) || index < 0 || index > headingCount || !Number.isSafeInteger(level) || level < 1 || level > 6) throw new DocsError("DOCS_OUTLINE_INVALID", "Invalid semantic outline position.", {
|
|
51
|
+
section,
|
|
52
|
+
field: item.anchor
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
finish(remaining, section) {
|
|
56
|
+
if (remaining.size) throw new DocsError("DOCS_OUTLINE_INVALID", "Missing rendered anchors: " + [...remaining.keys()].join(", "), { section });
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
/** Canonical ordering and parentage are independent of any view. */
|
|
60
|
+
function outlineModel(document, compact, validate) {
|
|
61
|
+
const entries = [];
|
|
62
|
+
const anchors = validate && /* @__PURE__ */ new Set();
|
|
63
|
+
const deepHeadings = /* @__PURE__ */ new Set();
|
|
64
|
+
for (const section of document.sections) {
|
|
65
|
+
const sources = section.artifacts ?? [section];
|
|
66
|
+
const contributions = /* @__PURE__ */ new Map();
|
|
67
|
+
for (const source of sources) for (const item of source.outline ?? []) {
|
|
68
|
+
validate?.contribution(item, contributions, section.id);
|
|
69
|
+
contributions.set(item.anchor, item);
|
|
70
|
+
}
|
|
71
|
+
const parents = [];
|
|
72
|
+
const hasSection = section.title || contributions.has(section.id);
|
|
73
|
+
const sectionHeadings = [];
|
|
74
|
+
let shallowest = 7;
|
|
75
|
+
function add(anchor, text, level, kind, provenance) {
|
|
76
|
+
const item = contributions.get(anchor);
|
|
77
|
+
contributions.delete(anchor);
|
|
78
|
+
validate?.entry(anchor, item, anchors, section.id);
|
|
79
|
+
while (parents.length && parents.at(-1).level >= level) parents.pop();
|
|
80
|
+
const parent = item?.parent ?? parents.at(-1)?.anchor ?? (hasSection && anchor !== section.id ? section.id : void 0);
|
|
81
|
+
parents.push({
|
|
82
|
+
anchor,
|
|
83
|
+
level
|
|
84
|
+
});
|
|
85
|
+
anchors?.add(anchor);
|
|
86
|
+
const entry = {
|
|
87
|
+
anchor,
|
|
88
|
+
text: item?.text ?? text,
|
|
89
|
+
kind: item?.kind ?? kind,
|
|
90
|
+
level,
|
|
91
|
+
section: section.id
|
|
92
|
+
};
|
|
93
|
+
if (parent) entry.parent = parent;
|
|
94
|
+
if (item?.reference) entry.reference = item.reference;
|
|
95
|
+
if (provenance?.length) entry.provenance = provenance;
|
|
96
|
+
if (compact && !item?.position && kind === "heading") {
|
|
97
|
+
sectionHeadings.push(entry);
|
|
98
|
+
shallowest = Math.min(shallowest, level);
|
|
99
|
+
}
|
|
100
|
+
entries.push(entry);
|
|
101
|
+
}
|
|
102
|
+
if (hasSection) add(section.id, section.title ?? section.id, 2, "section", section.provenance ?? sources[0]?.provenance);
|
|
103
|
+
for (const source of sources) {
|
|
104
|
+
const headings = source.headings ?? [];
|
|
105
|
+
const blocks = (source.outline ?? []).filter((item) => item.position);
|
|
106
|
+
for (const item of blocks) validate?.position(item, headings.length, section.id);
|
|
107
|
+
blocks.sort((left, right) => left.position.index - right.position.index);
|
|
108
|
+
let next = 0;
|
|
109
|
+
for (let index = 0; index <= headings.length; index++) {
|
|
110
|
+
while (blocks[next]?.position.index === index) {
|
|
111
|
+
const item = blocks[next++];
|
|
112
|
+
add(item.anchor, item.text, item.position.level, item.kind, source.provenance);
|
|
113
|
+
}
|
|
114
|
+
const heading = headings[index];
|
|
115
|
+
if (heading) add(heading.id, heading.text, heading.depth, "heading", source.provenance);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
for (const heading of sectionHeadings) if (heading.level > shallowest) deepHeadings.add(heading);
|
|
119
|
+
validate?.finish(contributions, section.id);
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
entries,
|
|
123
|
+
deepHeadings
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/** Validate and select entries from the canonical outline. A hidden parent promotes its children. */
|
|
127
|
+
function semanticOutline(document, view = outlineViews.full) {
|
|
128
|
+
return publishedOutline(document, defineOutline(view), outlineContract);
|
|
129
|
+
}
|
|
130
|
+
/** Publications already passed the compiler's structural anchor checks. */
|
|
131
|
+
function publishedOutline(document, view = outlineViews.full, validate) {
|
|
132
|
+
const { entries, deepHeadings } = outlineModel(document, view.mode === "compact", validate);
|
|
133
|
+
if (view === outlineViews.full) return entries;
|
|
134
|
+
const retained = /* @__PURE__ */ new Map();
|
|
135
|
+
const visible = [];
|
|
136
|
+
for (const entry of entries) {
|
|
137
|
+
const parent = retained.get(entry.parent);
|
|
138
|
+
if (deepHeadings.has(entry) || view.include && !view.include.includes(entry.kind) || view.exclude?.includes(entry.kind) || view.depth && (entry.level < view.depth[0] || entry.level > view.depth[1]) || view.filter && !view.filter(entry)) {
|
|
139
|
+
retained.set(entry.anchor, parent);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const group = view.group?.(entry) ?? (view.groupBy && entry[view.groupBy]);
|
|
143
|
+
retained.set(entry.anchor, entry.anchor);
|
|
144
|
+
const selected = { ...entry };
|
|
145
|
+
if (parent) selected.parent = parent;
|
|
146
|
+
else delete selected.parent;
|
|
147
|
+
if (group) selected.group = group;
|
|
148
|
+
visible.push(selected);
|
|
149
|
+
}
|
|
150
|
+
const compare = view.compare ?? (view.sort === "title" ? (left, right) => left.text.localeCompare(right.text, "en") : void 0);
|
|
151
|
+
if (!compare) return visible;
|
|
152
|
+
const order = (branches) => branches.sort((left, right) => compare(left.entry, right.entry)).flatMap(({ entry, children }) => [entry, ...order(children)]);
|
|
153
|
+
return order(outlineTree(visible));
|
|
154
|
+
}
|
|
155
|
+
/** Group a selected outline by visible parent without needing the document or inspecting the DOM. */
|
|
156
|
+
function outlineTree(entries) {
|
|
157
|
+
const tree = [];
|
|
158
|
+
const branches = /* @__PURE__ */ new Map();
|
|
159
|
+
for (const entry of entries) {
|
|
160
|
+
const branch = {
|
|
161
|
+
entry,
|
|
162
|
+
children: []
|
|
163
|
+
};
|
|
164
|
+
(branches.get(entry.parent)?.children ?? tree).push(branch);
|
|
165
|
+
branches.set(entry.anchor, branch);
|
|
166
|
+
}
|
|
167
|
+
return tree;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export { defineOutline, outlineTree, outlineViews, publishedOutline, semanticOutline, validateOutlineSetting };
|
|
171
|
+
//# sourceMappingURL=outline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"outline.js","names":[],"sources":["../src/outline.ts"],"sourcesContent":["import { DocsError, type Provenance } from \"./model.js\"\n\n/** Describe a rendered anchor using the domain vocabulary of its producer. */\nexport interface OutlineContribution {\n anchor: string\n kind: string\n text?: string\n reference?: string\n parent?: string\n /** Independent of headings. The index counts preceding rendered headings within the projection. */\n position?: { index: number; level: number }\n}\nexport type ProjectedOutline = Omit<OutlineContribution, \"position\" | \"anchor\"> & {\n anchor?: string\n level?: number\n}\nexport interface OutlineEntry extends Omit<OutlineContribution, \"position\"> {\n text: string\n level: number\n section: string\n /** A view may group an entry without changing its parent or anchor. */\n group?: string\n provenance?: readonly Provenance[]\n}\n/** Serializable policy for a selected view. */\nexport interface OutlineView {\n mode?: \"compact\" | \"full\"\n include?: readonly string[]\n exclude?: readonly string[]\n depth?: readonly [number, number]\n groupBy?: \"kind\" | \"section\"\n sort?: \"document\" | \"title\"\n}\n\n/** An authoring definition. Discovery supplies the name when omitted. */\nexport interface OutlineDefinition extends OutlineView {\n id?: string\n}\n\n/** null clears inherited policies and restores the host's default view. */\nexport type OutlineSetting = string | OutlineView | null\n\nexport function validateOutlineSetting(setting: unknown): setting is OutlineSetting {\n if (setting === null) return true\n if (typeof setting === \"string\") return /^[a-zA-Z0-9][a-zA-Z0-9_./-]*$/.test(setting)\n if (!setting || typeof setting !== \"object\" || Array.isArray(setting)) return false\n if (\n Object.keys(setting).some(\n (key) => ![\"mode\", \"include\", \"exclude\", \"depth\", \"groupBy\", \"sort\"].includes(key),\n )\n )\n return false\n defineOutline(setting)\n return true\n}\n\n/** Additional organization for an in-memory view. Callbacks receive canonical entries. */\nexport interface OutlineProjection extends OutlineDefinition {\n filter?: (entry: Readonly<OutlineEntry>) => boolean\n group?: (entry: Readonly<OutlineEntry>) => string | undefined\n compare?: (left: Readonly<OutlineEntry>, right: Readonly<OutlineEntry>) => number\n}\n\nexport const outlineViews = {\n compact: { mode: \"compact\", exclude: [\"api-parameter\", \"api-response\"] },\n full: { mode: \"full\" },\n} as const satisfies Record<string, OutlineView>\n\nexport function defineOutline(\n view: OutlineDefinition & { id: string },\n): OutlineDefinition & { id: string }\nexport function defineOutline(view?: OutlineProjection): OutlineProjection\nexport function defineOutline(view: OutlineProjection = {}): OutlineProjection {\n for (const kinds of [view.include, view.exclude])\n if (\n kinds !== undefined &&\n (!Array.isArray(kinds) || kinds.some((kind) => typeof kind !== \"string\" || !kind.trim()))\n )\n throw new DocsError(\"DOCS_OUTLINE_INVALID\", \"Specify outline kinds as a list of names.\")\n if (view.groupBy !== undefined && view.groupBy !== \"kind\" && view.groupBy !== \"section\")\n throw new DocsError(\"DOCS_OUTLINE_INVALID\", \"Expected an outline group of kind or section.\")\n if (view.sort !== undefined && view.sort !== \"title\" && view.sort !== \"document\")\n throw new DocsError(\"DOCS_OUTLINE_INVALID\", \"Expected document or title outline order.\")\n if (view.mode !== undefined && view.mode !== \"compact\" && view.mode !== \"full\")\n throw new DocsError(\"DOCS_OUTLINE_INVALID\", \"Expected a compact or full outline view.\")\n if (\n view.depth !== undefined &&\n (!Array.isArray(view.depth) ||\n view.depth.length !== 2 ||\n !view.depth.every((value) => Number.isInteger(value) && value >= 1 && value <= 6) ||\n view.depth[0] > view.depth[1])\n )\n throw new DocsError(\n \"DOCS_OUTLINE_DEPTH\",\n \"Outline depth must be an inclusive range between 1 and 6.\",\n )\n return view\n}\ninterface OutlineSource {\n headings?: readonly { id: string; text: string; depth: number }[]\n provenance?: readonly Provenance[]\n outline?: readonly OutlineContribution[]\n}\nexport interface OutlineDocument {\n outlineView?: OutlineView | null\n sections: readonly (OutlineSource & {\n id: string\n title?: string\n artifacts?: readonly OutlineSource[]\n })[]\n}\n\nconst outlineContract = {\n contribution(\n item: OutlineContribution,\n existing: ReadonlyMap<string, OutlineContribution>,\n section: string,\n ) {\n if (!item.kind || existing.has(item.anchor) || (item.position && item.anchor === section))\n throw new DocsError(\"DOCS_OUTLINE_INVALID\", \"Invalid or duplicate outline contribution.\", {\n section,\n field: item.anchor,\n })\n },\n entry(\n anchor: string,\n item: OutlineContribution | undefined,\n existing: ReadonlySet<string>,\n section: string,\n ) {\n if (existing.has(anchor))\n throw new DocsError(\"DOCS_OUTLINE_INVALID\", \"Duplicate outline anchor \" + anchor, { section })\n if (item?.parent !== undefined && !existing.has(item.parent))\n throw new DocsError(\"DOCS_OUTLINE_INVALID\", \"Outline parent must name an earlier anchor.\", {\n section,\n field: item.parent,\n })\n },\n position(item: OutlineContribution, headingCount: number, section: string) {\n const { index, level } = item.position!\n if (\n !item.text?.trim() ||\n !Number.isSafeInteger(index) ||\n index < 0 ||\n index > headingCount ||\n !Number.isSafeInteger(level) ||\n level < 1 ||\n level > 6\n )\n throw new DocsError(\"DOCS_OUTLINE_INVALID\", \"Invalid semantic outline position.\", {\n section,\n field: item.anchor,\n })\n },\n finish(remaining: ReadonlyMap<string, OutlineContribution>, section: string) {\n if (remaining.size)\n throw new DocsError(\n \"DOCS_OUTLINE_INVALID\",\n \"Missing rendered anchors: \" + [...remaining.keys()].join(\", \"),\n { section },\n )\n },\n}\n\n/** Canonical ordering and parentage are independent of any view. */\nfunction outlineModel(\n document: OutlineDocument,\n compact: boolean,\n validate?: typeof outlineContract,\n) {\n const entries: OutlineEntry[] = []\n const anchors = validate && new Set<string>()\n const deepHeadings = new Set<OutlineEntry>()\n for (const section of document.sections) {\n const sources = section.artifacts ?? [section]\n const contributions = new Map<string, OutlineContribution>()\n for (const source of sources)\n for (const item of source.outline ?? []) {\n validate?.contribution(item, contributions, section.id)\n contributions.set(item.anchor, item)\n }\n const parents: { anchor: string; level: number }[] = []\n const hasSection = section.title || contributions.has(section.id)\n const sectionHeadings: OutlineEntry[] = []\n let shallowest = 7\n function add(\n anchor: string,\n text: string,\n level: number,\n kind: string,\n provenance?: readonly Provenance[],\n ) {\n const item = contributions.get(anchor)\n contributions.delete(anchor)\n validate?.entry(anchor, item, anchors!, section.id)\n while (parents.length && parents.at(-1)!.level >= level) parents.pop()\n // The section is a semantic container even if its first heading is also level 2.\n const parent =\n item?.parent ??\n parents.at(-1)?.anchor ??\n (hasSection && anchor !== section.id ? section.id : undefined)\n parents.push({ anchor, level })\n anchors?.add(anchor)\n const entry: OutlineEntry = {\n anchor,\n text: item?.text ?? text,\n kind: item?.kind ?? kind,\n level,\n section: section.id,\n }\n if (parent) entry.parent = parent\n if (item?.reference) entry.reference = item.reference\n if (provenance?.length) entry.provenance = provenance\n if (compact && !item?.position && kind === \"heading\") {\n sectionHeadings.push(entry)\n shallowest = Math.min(shallowest, level)\n }\n entries.push(entry)\n }\n if (hasSection)\n add(\n section.id,\n section.title ?? section.id,\n 2,\n \"section\",\n section.provenance ?? sources[0]?.provenance,\n )\n for (const source of sources) {\n const headings = source.headings ?? []\n const blocks = (source.outline ?? []).filter((item) => item.position)\n for (const item of blocks) validate?.position(item, headings.length, section.id)\n blocks.sort((left, right) => left.position!.index - right.position!.index)\n let next = 0\n for (let index = 0; index <= headings.length; index++) {\n while (blocks[next]?.position!.index === index) {\n const item = blocks[next++]!\n add(item.anchor, item.text!, item.position!.level, item.kind, source.provenance)\n }\n const heading = headings[index]\n if (heading) add(heading.id, heading.text, heading.depth, \"heading\", source.provenance)\n }\n }\n for (const heading of sectionHeadings) if (heading.level > shallowest) deepHeadings.add(heading)\n validate?.finish(contributions, section.id)\n }\n return { entries, deepHeadings }\n}\n\n/** Validate and select entries from the canonical outline. A hidden parent promotes its children. */\nexport function semanticOutline(\n document: OutlineDocument,\n view: OutlineProjection = outlineViews.full,\n): OutlineEntry[] {\n return publishedOutline(document, defineOutline(view), outlineContract)\n}\n\n/** Publications already passed the compiler's structural anchor checks. */\nexport function publishedOutline(\n document: OutlineDocument,\n view: OutlineProjection = outlineViews.full,\n validate?: typeof outlineContract,\n): OutlineEntry[] {\n const { entries, deepHeadings } = outlineModel(document, view.mode === \"compact\", validate)\n if (view === outlineViews.full) return entries\n const retained = new Map<string | undefined, string | undefined>()\n const visible: OutlineEntry[] = []\n for (const entry of entries) {\n const parent = retained.get(entry.parent)\n if (\n deepHeadings.has(entry) ||\n (view.include && !view.include.includes(entry.kind)) ||\n view.exclude?.includes(entry.kind) ||\n (view.depth && (entry.level < view.depth[0] || entry.level > view.depth[1])) ||\n (view.filter && !view.filter(entry))\n ) {\n retained.set(entry.anchor, parent)\n continue\n }\n const group = view.group?.(entry) ?? (view.groupBy && entry[view.groupBy])\n retained.set(entry.anchor, entry.anchor)\n const selected = { ...entry }\n if (parent) selected.parent = parent\n else delete selected.parent\n if (group) selected.group = group\n visible.push(selected)\n }\n const compare =\n view.compare ??\n (view.sort === \"title\"\n ? (left: OutlineEntry, right: OutlineEntry) => left.text.localeCompare(right.text, \"en\")\n : undefined)\n if (!compare) return visible\n const order = (branches: OutlineBranch[]): OutlineEntry[] =>\n branches\n .sort((left, right) => compare(left.entry, right.entry))\n .flatMap(({ entry, children }) => [entry, ...order(children)])\n return order(outlineTree(visible))\n}\n\nexport interface OutlineBranch {\n entry: OutlineEntry\n children: OutlineBranch[]\n}\n\n/** Group a selected outline by visible parent without needing the document or inspecting the DOM. */\nexport function outlineTree(entries: readonly OutlineEntry[]): OutlineBranch[] {\n const tree: OutlineBranch[] = []\n const branches = new Map<string | undefined, OutlineBranch>()\n for (const entry of entries) {\n const branch = { entry, children: [] }\n const parent = branches.get(entry.parent)\n ;(parent?.children ?? tree).push(branch)\n branches.set(entry.anchor, branch)\n }\n return tree\n}\n"],"mappings":";;;AA0CA,SAAgB,uBAAuB,SAA6C;CAClF,IAAI,YAAY,MAAM,OAAO;CAC7B,IAAI,OAAO,YAAY,UAAU,OAAO,gCAAgC,KAAK,OAAO;CACpF,IAAI,CAAC,WAAW,OAAO,YAAY,YAAY,MAAM,QAAQ,OAAO,GAAG,OAAO;CAC9E,IACE,OAAO,KAAK,OAAO,CAAC,CAAC,MAClB,QAAQ,CAAC;EAAC;EAAQ;EAAW;EAAW;EAAS;EAAW;CAAM,CAAC,CAAC,SAAS,GAAG,CACnF,GAEA,OAAO;CACT,cAAc,OAAO;CACrB,OAAO;AACT;AASA,MAAa,eAAe;CAC1B,SAAS;EAAE,MAAM;EAAW,SAAS,CAAC,iBAAiB,cAAc;CAAE;CACvE,MAAM,EAAE,MAAM,OAAO;AACvB;AAMA,SAAgB,cAAc,OAA0B,CAAC,GAAsB;CAC7E,KAAK,MAAM,SAAS,CAAC,KAAK,SAAS,KAAK,OAAO,GAC7C,IACE,UAAU,WACT,CAAC,MAAM,QAAQ,KAAK,KAAK,MAAM,MAAM,SAAS,OAAO,SAAS,YAAY,CAAC,KAAK,KAAK,CAAC,IAEvF,MAAM,IA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AAC,OAAO,GAAG,MAAM,QAAQ,CAAC,CAAC;CACjE,OAAO,MAAM,YAAY,OAAO,CAAC;AACnC;;AAQA,SAAgB,YAAY,SAAmD;CAC7E,MAAM,OAAwB,CAAC;CAC/B,MAAM,2BAAW,IAAI,IAAuC;CAC5D,KAAK,MAAM,SAAS,SAAS;EAC3B,MAAM,SAAS;GAAE;GAAO,UAAU,CAAC;EAAE;EAEpC,CADc,SAAS,IAAI,MAAM,MAC3B,CAAC,EAAE,YAAY,KAAI,CAAE,KAAK,MAAM;EACvC,SAAS,IAAI,MAAM,QAAQ,MAAM;CACnC;CACA,OAAO;AACT"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { type JsonValue } from "./content-schema.js";
|
|
2
|
+
import type { Identity } from "./model.js";
|
|
3
|
+
type Awaitable<T> = T | PromiseLike<T>;
|
|
4
|
+
/** Project IDs may also use an installed package's scoped npm name. */
|
|
5
|
+
export declare function validPersistenceId(value: unknown): value is string;
|
|
6
|
+
export type PersistenceAdapterId = "memory" | "local" | "session" | "cookie" | (string & {});
|
|
7
|
+
export type PersistenceMigration<T extends JsonValue = JsonValue> = (value: unknown, fromVersion: number) => Awaitable<T | undefined>;
|
|
8
|
+
export interface PersistenceOptions<Migration = string> {
|
|
9
|
+
adapter: PersistenceAdapterId;
|
|
10
|
+
sync?: boolean;
|
|
11
|
+
version?: number;
|
|
12
|
+
migrate?: Migration;
|
|
13
|
+
/** Time to live in milliseconds, measured from the last explicit selection. */
|
|
14
|
+
expiration?: number;
|
|
15
|
+
}
|
|
16
|
+
export type PublishedPersistencePolicy = PersistenceAdapterId | PersistenceOptions;
|
|
17
|
+
export type PersistencePolicy<T extends JsonValue = JsonValue> = PersistenceAdapterId | PersistenceOptions<string | PersistenceMigration<T>>;
|
|
18
|
+
export declare function validPersistencePolicy(policy: unknown, portable?: boolean): policy is PersistencePolicy;
|
|
19
|
+
export type ContextScopeKind = "project" | "scope" | "document" | "section" | "example";
|
|
20
|
+
export interface ContextScopeIdentity {
|
|
21
|
+
scope?: string;
|
|
22
|
+
document?: Identity;
|
|
23
|
+
section?: string;
|
|
24
|
+
example?: string;
|
|
25
|
+
}
|
|
26
|
+
export interface PersistenceAdapterContext {
|
|
27
|
+
signal: AbortSignal;
|
|
28
|
+
}
|
|
29
|
+
export interface PersistenceAdapter<Id extends string = string> {
|
|
30
|
+
id: Id;
|
|
31
|
+
read(key: string, context: PersistenceAdapterContext): Awaitable<string | undefined>;
|
|
32
|
+
write(key: string, record: string, context: PersistenceAdapterContext): Awaitable<void>;
|
|
33
|
+
remove(key: string, context: PersistenceAdapterContext): Awaitable<void>;
|
|
34
|
+
subscribe?(key: string, invalidate: () => void): () => void;
|
|
35
|
+
}
|
|
36
|
+
export declare function definePersistenceAdapter<const Id extends string>(adapter: PersistenceAdapter<Id>): PersistenceAdapter<Id>;
|
|
37
|
+
/** Serialized, validated preference records for the initial server and client render. */
|
|
38
|
+
export type PersistenceSnapshot = Readonly<Record<string, string>>;
|
|
39
|
+
export interface PersistenceDiagnostic {
|
|
40
|
+
code: "unknown-adapter" | "missing-identity" | "invalid-record" | "migration" | "adapter";
|
|
41
|
+
key: string;
|
|
42
|
+
adapter?: string;
|
|
43
|
+
cause?: unknown;
|
|
44
|
+
}
|
|
45
|
+
export interface PersistenceRuntimeOptions {
|
|
46
|
+
projectId: string;
|
|
47
|
+
namespace?: string;
|
|
48
|
+
account?: string;
|
|
49
|
+
adapters?: readonly PersistenceAdapter[];
|
|
50
|
+
migrations?: Readonly<Record<string, PersistenceMigration>>;
|
|
51
|
+
initial?: PersistenceSnapshot;
|
|
52
|
+
defer?: boolean;
|
|
53
|
+
onError?(diagnostic: PersistenceDiagnostic): void;
|
|
54
|
+
now?(): number;
|
|
55
|
+
}
|
|
56
|
+
export interface ContextPersistenceBinding {
|
|
57
|
+
id: string;
|
|
58
|
+
scope?: ContextScopeKind;
|
|
59
|
+
identity?: ContextScopeIdentity;
|
|
60
|
+
policy?: PublishedPersistencePolicy;
|
|
61
|
+
}
|
|
62
|
+
type Binding = Omit<ContextPersistenceBinding, "policy"> & {
|
|
63
|
+
policy?: PersistencePolicy;
|
|
64
|
+
validate(value: JsonValue): JsonValue;
|
|
65
|
+
};
|
|
66
|
+
export declare function contextScopeAddress(kind: ContextScopeKind, identity?: ContextScopeIdentity): (string | null)[] | undefined;
|
|
67
|
+
export interface PersistenceSlot {
|
|
68
|
+
value(): JsonValue | undefined;
|
|
69
|
+
persisted(): JsonValue | undefined;
|
|
70
|
+
record(): readonly [key: string, record: string] | undefined;
|
|
71
|
+
set(value: JsonValue | undefined): boolean;
|
|
72
|
+
subscribe(listener: () => void): () => void;
|
|
73
|
+
}
|
|
74
|
+
export declare function createPersistenceRuntime(options: PersistenceRuntimeOptions): {
|
|
75
|
+
batch: (work: () => void) => void;
|
|
76
|
+
bind(binding: Binding): PersistenceSlot;
|
|
77
|
+
restore(): Promise<void>;
|
|
78
|
+
snapshot(): PersistenceSnapshot;
|
|
79
|
+
dispose(): void;
|
|
80
|
+
};
|
|
81
|
+
export type PersistenceRuntime = ReturnType<typeof createPersistenceRuntime>;
|
|
82
|
+
export {};
|