Repository navigation
Add built-in JSON/JSONL parse plugin with multi-parser merging - #277
Conversation
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
|
|
||
| ## JSON plugin config | ||
|
|
||
| The built-in `json` plugin (`sctx-json`) runs alongside `sctx-markdown` by default and parses `.json` files (a single node object or an array of node objects) and `.jsonl` files (one node object per line) in the space. Set under `plugins.json` per space: |
There was a problem hiding this comment.
I'm thinking magic defaults like this are a slippery slope. We have a mechanism for specifying per plugin config (which also gives us a way to specify one or multiple parse plugins genetically) and i think we should use it. Including json automatically could have unintended side effects and reduce flexibility.
There was a problem hiding this comment.
I think a json plugin when specified without configuration could look in the path as currently defined, but could also override how and where json is sought with optional parameters
| With an empty config (`{}`), the plugin scans the space's `path` for `**/*.json` and `**/*.jsonl`. Optional fields: | ||
|
|
||
| - **`path`** — directory or single `.json`/`.jsonl` file to read instead of the space's `path` (relative paths resolve from the config root). | ||
| - **`include`** — glob patterns relative to `path` (default `["**/*.json", "**/*.jsonl"]`). |
|
|
||
| - **`path`** — directory or single `.json`/`.jsonl` file to read instead of the space's `path` (relative paths resolve from the config root). | ||
| - **`include`** — glob patterns relative to `path` (default `["**/*.json", "**/*.jsonl"]`). | ||
| - **`fieldMap`** — maps source JSON property names to canonical schema field names. |
|
|
||
| ## JSON plugin config | ||
|
|
||
| Enable the built-in `json` plugin (`sctx-json`) per space under `plugins.json` to parse `.json` files (a single node object or an array of node objects) and `.jsonl` files (one node object per line) alongside markdown: |
There was a problem hiding this comment.
Let's have it use json5 parser for .json and .json5
|
|
||
| function isPluginConfigured(plugins: Record<string, Record<string, unknown>> | undefined): boolean { | ||
| if (!plugins) return false; | ||
| return BUILTIN_PLUGIN_NAMES.json in plugins || shortenPluginName(BUILTIN_PLUGIN_NAMES.json) in plugins; |
There was a problem hiding this comment.
This feels a little odd to have at the plug-in level. Fine to leave it if there's a precedent and not a good workaround, but I wonder if built-in plug-in handling (eg prefixes) and detecting if enabled can be handled better outside of each built-in plug-in, so it's not importing the built-in plugins list.
| import { readSpace, refreshReadResult } from '../../../src/read/read-space'; | ||
| import { createSpaceContext } from '../../../src/space-context'; | ||
|
|
||
| const META = |
| } | ||
|
|
||
| throw new Error(`No plugin handled space at: ${context.space.path}`); | ||
| // Built-in filesystem parse plugins (e.g. markdown + json) run together and merge their results. |
There was a problem hiding this comment.
This logic is ok for right now, but it's sort of arbitrary and there's likely a more general approach that's more future proof. Constraints:
Preserve ease of config and use with markdown.
Assume it will be valuable to merge parse results from multiple configured plugins (don't assume first one wins for 3rd party)
Running parse calls (promises) in parallel, concurrency reasonably limited somehow, seems reasonable.
5870c8f to
dbd5299
Compare
Closes #112
Summary
sctx-jsonparse plugin (src/plugins/json/index.ts), enabled by default, that parses.json(single node object or array of node objects) and.jsonl(one node object per line with 1-based line numbers) files, with optionalplugins.json.enabledandplugins.json.fieldMap.options.filesrefreshes for watch mode.dispatchParseinsrc/read/read-space.tsso non-built-in parse plugins retain first-match-wins precedence while built-in filesystem parse plugins (sctx-markdown+sctx-json) run together and merge nodes, parse issues, excluded pages, and diagnostics before graph resolution.markdownPluginto returnnullfor non-.mdsingle-file spaces so single.json/.jsonlfile spaces dispatch tosctx-json.plugins.jsonindocs/config.mdand add tests intests/plugins/json/read-json.test.ts.