refactor(cli): centralize content registry building

Extract buildRegistryFromIndex and normalizePathKey from the CLI serve
command and the browser completions module into the shared
content-registry module. This ensures both paths produce identical
pathIndex/docContent, including the spark-injection pass that was
previously only applied in the CLI.
This commit is contained in:
hyper
2026-08-07 12:46:08 +08:00
parent e8aa7165cb
commit 0a68122efd
3 changed files with 63 additions and 33 deletions
+6 -9
View File
@@ -16,7 +16,7 @@ import {
setInlineResolver,
} from "../../data-loader/file-index";
import {
scanDoc,
buildRegistryFromIndex,
deriveCompletions,
resolveInlineByPath,
type ContentRegistry,
@@ -108,19 +108,16 @@ async function tryServerRegistry(): Promise<void> {
async function scanClientSide(): Promise<JournalCompletions> {
const paths = await getPathsByExtension("md");
// Build a registry from the in-memory file index, then derive completions
// through the same shared pipeline as the CLI.
const registry: ContentRegistry = { pathIndex: {}, docContent: {} };
// First pass: load all .md content into the registry.
// Load all .md content into a raw index, then build the registry through
// the same shared pipeline as the CLI (scanDoc + spark injection).
const index: Record<string, string> = {};
for (const filePath of paths) {
const content = await getIndexedData(filePath);
if (!content) continue;
const result = scanDoc(content, filePath);
registry.pathIndex[filePath] = result.stripped;
registry.docContent[filePath] = result.content;
index[filePath] = content;
}
const registry = buildRegistryFromIndex(index);
activeRegistry = registry;
return deriveCompletions(registry);
}