Include line in --json search output
Problem
The CLI formatter includes line numbers in search results (e.g. qmd://blog/notebook/database_vector.qmd:2), but the --json formatter omits them. The line number is computed from chunkPos via extractSnippet in the CLI path (cli/qmd.js:1562) but isn't passed through to searchResultsToJson in formatter.js.
This makes it harder to build editor integrations. For example, VS Code's code --goto file:line works great with the CLI output, but parsing coloured terminal output is fragile. A --json workflow like:
qmd query "vector database" --json | jq -r '.[0] | "\(.file):\(.line)"' | xargs code --goto
would be much more robust, but currently .line is absent from the JSON.
Suggested fix
In cli/formatter.js, searchResultsToJson (around line 53), the result object is built as:
return {
docid: `#${row.docid}`,
score: Math.round(row.score * 100) / 100,
file: row.displayPath,
title: row.title,
...(row.context && { context: row.context }),
...(body && { body }),
...(snippet && { snippet }),
};
Adding the line number would be something like:
const { line, snippet: snip } = extractSnippet(bodyStr, query, 300, row.chunkPos, undefined, opts.intent);
return {
docid: `#${row.docid}`,
score: Math.round(row.score * 100) / 100,
file: row.displayPath,
line,
title: row.title,
...(row.context && { context: row.context }),
...(body && { body }),
...(snip && !opts.full && { snippet: snip }),
};
The extractSnippet function is already imported in qmd.js; it may need to be made available to formatter.js as well, or the line could be pre-computed and passed through on the result object.
Use case
Building shell/editor integrations that open search results at the matching line — VS Code (code --goto), Vim (:e +line file), Emacs, etc.
Include
linein--jsonsearch outputProblem
The CLI formatter includes line numbers in search results (e.g.
qmd://blog/notebook/database_vector.qmd:2), but the--jsonformatter omits them. The line number is computed fromchunkPosviaextractSnippetin the CLI path (cli/qmd.js:1562) but isn't passed through tosearchResultsToJsoninformatter.js.This makes it harder to build editor integrations. For example, VS Code's
code --goto file:lineworks great with the CLI output, but parsing coloured terminal output is fragile. A--jsonworkflow like:would be much more robust, but currently
.lineis absent from the JSON.Suggested fix
In
cli/formatter.js,searchResultsToJson(around line 53), the result object is built as:Adding the line number would be something like:
The
extractSnippetfunction is already imported inqmd.js; it may need to be made available toformatter.jsas well, or the line could be pre-computed and passed through on the result object.Use case
Building shell/editor integrations that open search results at the matching line — VS Code (
code --goto), Vim (:e +line file), Emacs, etc.