Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,23 @@ Schema tables handle:
- Nested objects: flattened as indented dotted rows (e.g., `owner.guid`)
- Arrays of objects: expanded inline with their properties
- `allOf` composition: merged into a single property table
- `anyOf`/`oneOf` composition: typed by the alternatives joined with `|`
(`object|null`, `string|integer`); a nullable object also lists the object's
properties, and a request or response body lists each alternative in turn
- Property attributes: required, read-only, nullable, default, minimum/maximum, enum, example, format

### Field tables and `llms-txt`

Quarto builds each `.llms.md` page from the rendered HTML with Pandoc's GFM
writer. A GFM table cell holds inline content only, so Pandoc writes `[TABLE]`
in place of any table with a paragraph break, list or line break in a cell.

Each field table is therefore emitted twice, inside
`content-hidden`/`content-visible` blocks with `when-format="llms-txt"`: the
table for the HTML page, and a bullet list with one item per field for
`.llms.md`, where descriptions keep their paragraphs, lists and code blocks.
Quarto removes the list from the HTML page and its search index.

## Project structure

```text
Expand All @@ -143,6 +158,7 @@ quarto-openapi/
refs.ts # $ref resolution
sections.ts # path grouping and endpoint rendering
schema.ts # schema-to-table conversion
fields.ts # Name/Type/Description field tables
markdown.ts # list table and markdown utilities
escape.ts # makes description prose safe as markdown
tests/ # node:test suites for lib/
Expand Down
82 changes: 82 additions & 0 deletions _extensions/quarto-openapi/lib/fields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Field tables: the Name / Type / Description tables for parameters and schema
* properties.
*
* Quarto builds a page's `.llms.md` by converting its rendered HTML with
* Pandoc's GFM writer. A GFM table cell holds inline content only, so a
* description with a second paragraph, a list or a line break would turn the
* whole table into `[TABLE]`. Each field table therefore comes in two forms:
* the list table for every format but `llms-txt`, and for `llms-txt` a bullet
* list with one item per field, where any markdown fits. Quarto keeps the
* `llms-txt` form out of the HTML page and its search index.
*/

import { listTable } from "./markdown.ts";

export interface FieldRow {
/** The field's name, dotted for nested properties (`owner.guid`). */
name: string;
/** Nesting level; 0 for top-level fields. */
depth: number;
/** The type label, without code formatting. */
type: string;
/** The field's description as markdown, possibly empty. */
description: string;
/** Attribute sentences such as "Required. Example: `x`.", possibly empty. */
attributes: string;
}

/** The description followed by the attributes, as separate paragraphs. */
function details(row: FieldRow): string {
return [row.description.trim(), row.attributes]
.filter((part) => part !== "")
.join("\n\n");
}

function nameCell(row: FieldRow): string {
return row.depth > 0
? `[\`${row.name}\`]{.schema-nest-${row.depth}}`
: `\`${row.name}\``;
}

/**
* The `llms-txt` form: one loose list item per field, headed by its name and
* type, with the details as the item's blocks.
*/
function fieldList(rows: FieldRow[]): string[] {
const lines: string[] = [];
for (const row of rows) {
lines.push(`- \`${row.name}\` (\`${row.type}\`)`, "");
const text = details(row);
if (text === "") continue;
for (const line of text.split("\n")) {
lines.push(line === "" ? "" : ` ${line}`);
}
lines.push("");
}
return lines;
}

/** Render rows as a field table, plus its `llms-txt` form. */
export function fieldTable(rows: FieldRow[]): string[] {
if (rows.length === 0) return [];

const table = listTable(
["Name", "Type", "Description"],
rows.map((row) => ({
cells: [nameCell(row), `\`${row.type}\``, details(row)],
})),
);
return [
'::: {.content-hidden when-format="llms-txt"}',
"",
...table,
"",
":::",
"",
'::: {.content-visible when-format="llms-txt"}',
"",
...fieldList(rows),
":::",
];
}
5 changes: 3 additions & 2 deletions _extensions/quarto-openapi/lib/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ export function listTable(headers: string[], rows: TableRow[]): string[] {
lines.push("");

const emitGroup = (cellLines: string[][]) => {
// Loose when any cell holds more than one block.
const loose = cellLines.some((cl) => cl.some((line) => line === ""));
// Loose when any cell holds more than one block. An empty cell is one
// empty line, not a block separator.
const loose = cellLines.some((cl) => cl.length > 1 && cl.includes(""));
cellLines.forEach((cl, col) => {
if (loose && col > 0) lines.push("");
lines.push((col === 0 ? "* * " : " * ") + cl[0]);
Expand Down
7 changes: 5 additions & 2 deletions _extensions/quarto-openapi/lib/refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ export function expandRefs<T>(
if (isReference(obj)) {
const ref = obj.$ref;
if (seen.has(ref)) {
// Circular reference — return a stub to avoid infinite recursion
return { description: `(circular reference: ${ref})` } as T;
// Circular reference: a stub that names the schema it would repeat.
return {
description: `(circular reference: ${ref})`,
"x-circular-ref": ref,
} as T;
}
const resolved = resolveRef(spec, ref);
if (resolved === undefined) {
Expand Down
Loading
Loading