From ffce589b7de105fadc7cb5f4a87bfbdb1b877365 Mon Sep 17 00:00:00 2001 From: Ophir Lojkine Date: Sat, 28 Nov 2026 23:46:32 +0000 Subject: [PATCH] Build text and title references and live examples from authored rows --- examples/official-site/README.md | 8 ++ .../official-site/content/components/text.md | 72 ++++++++++++ .../components/text/examples/01-paragraph.md | 7 ++ .../components/text/examples/02-markdown.md | 52 +++++++++ .../components/text/examples/03-rich-text.md | 14 +++ .../text/examples/04-section-link.md | 11 ++ .../official-site/content/components/title.md | 21 ++++ .../components/title/examples/01-levels.md | 13 +++ .../components/title/examples/02-centered.md | 8 ++ .../content/shared-parameters.yaml | 10 ++ .../sqlpage/migrations/01_documentation.sql | 76 ------------- .../sqlpage/migrations/35_code_title.sql | 56 +--------- .../migrations/59_unsafe_contents_md.sql | 4 - .../migrations/82_reference_catalog.sql | 2 - .../99_shared_id_class_attributes.sql | 3 - scripts/site/components.mjs | 103 ++++++++++++++++++ scripts/site/content.mjs | 5 +- scripts/site/documentation.mjs | 2 + tests/site/components.test.mjs | 43 ++++++++ 19 files changed, 368 insertions(+), 142 deletions(-) create mode 100644 examples/official-site/content/components/text.md create mode 100644 examples/official-site/content/components/text/examples/01-paragraph.md create mode 100644 examples/official-site/content/components/text/examples/02-markdown.md create mode 100644 examples/official-site/content/components/text/examples/03-rich-text.md create mode 100644 examples/official-site/content/components/text/examples/04-section-link.md create mode 100644 examples/official-site/content/components/title.md create mode 100644 examples/official-site/content/components/title/examples/01-levels.md create mode 100644 examples/official-site/content/components/title/examples/02-centered.md create mode 100644 examples/official-site/content/shared-parameters.yaml delete mode 100644 examples/official-site/sqlpage/migrations/59_unsafe_contents_md.sql create mode 100644 scripts/site/components.mjs create mode 100644 tests/site/components.test.mjs diff --git a/examples/official-site/README.md b/examples/official-site/README.md index eb5ffe063..4445188e2 100644 --- a/examples/official-site/README.md +++ b/examples/official-site/README.md @@ -44,6 +44,14 @@ Function pages in `content/functions/` use their filename as the public function name. Keep the argument list in signature order and mark mandatory arguments with `required: true`. The Markdown body can include SQL examples without escaping. +## Component reference + +Component pages in `content/components/` list parameters under `top` and `row`. +Each `examples/*.md` file can contain `rows` in its YAML metadata. These rows +drive both the live preview and the copyable SQL. The Markdown body is the +example caption. Use numbered filenames to keep the examples in order. +References that have not moved still come from the SQL migrations. + ## Landing page The home page is rendered by `index.sql` and the `landing-page` custom template. diff --git a/examples/official-site/content/components/text.md b/examples/official-site/content/components/text.md new file mode 100644 index 000000000..07a002713 --- /dev/null +++ b/examples/official-site/content/components/text.md @@ -0,0 +1,72 @@ +--- +icon: align-left +category: page-layout +summary: Write paragraphs using plain text or Markdown. +top: + - name: article + type: BOOLEAN + description: Makes long texts more readable by increasing the line height, adding margins, using a serif + font, and decorating the initial letter. + - name: center + type: BOOLEAN + description: Whether to center the title. + - name: contents + type: TEXT + description: A top-level paragraph of text to display, without any formatting, without having to make additional + queries. + - name: contents_md + type: TEXT + description: Rich text in the markdown format. Among others, this allows you to write bold text using `**bold**`, + italics using `*italics*`, and links using `[text](https://example.com)`. + - name: html + type: TEXT + description: Raw html code to include on the page. Don't use that if you are not sure what you are doing, + it may have security implications. + - id + - name: title + type: TEXT + description: Text header before the paragraph. + - name: unsafe_contents_md + type: TEXT + description: Markdown format with html blocks. Use this only with trusted content. See the html-blocks + section of the Commonmark spec for additional info. +row: + - name: bold + type: BOOLEAN + description: Whether the span of text should be displayed as bold. + - name: break + type: BOOLEAN + description: Indicates that the current span of text starts a new paragraph. + - name: code + type: BOOLEAN + description: Use a monospace font. Useful to display the text as code. + - name: color + type: COLOR + description: The name of a color for this span of text. + - name: contents + type: TEXT + required: true + description: A span of text to display + - name: contents_md + type: TEXT + description: Rich text in the markdown format. Among others, this allows you to write bold text using `**bold**`, + italics using `*italics*`, and links using `[text](https://example.com)`. + - name: italics + type: BOOLEAN + description: Whether the span of text should be displayed as italics. + - name: link + type: URL + description: An URL to which the user should be taken when they click on this span of text. + - name: size + type: INTEGER + description: A number between 1 and 6 indicating the font size. + - name: underline + type: BOOLEAN + description: Whether the span of text should be underlined. + - name: unsafe_contents_md + type: TEXT + description: Markdown format with html blocks. Use this only with trusted content. See the html-blocks + section of the Commonmark spec for additional info. +--- + +A paragraph of text. The entire component will render as a single paragraph, with each item being rendered as a span of text inside it, the styling of which can be customized using parameters. diff --git a/examples/official-site/content/components/text/examples/01-paragraph.md b/examples/official-site/content/components/text/examples/01-paragraph.md new file mode 100644 index 000000000..8c26f7bf7 --- /dev/null +++ b/examples/official-site/content/components/text/examples/01-paragraph.md @@ -0,0 +1,7 @@ +--- +rows: + - component: text + contents: Hello, world ! <3 +--- + +Rendering a simple text paragraph. diff --git a/examples/official-site/content/components/text/examples/02-markdown.md b/examples/official-site/content/components/text/examples/02-markdown.md new file mode 100644 index 000000000..e9c3cb708 --- /dev/null +++ b/examples/official-site/content/components/text/examples/02-markdown.md @@ -0,0 +1,52 @@ +--- +rows: + - component: text + article: true + contents_md: |+ + + # Markdown in SQLPage + + ## Simple formatting + + SQLPage supports only plain text as column values, but markdown allows easily adding **bold**, *italics*, [external links](https://github.com/sqlpage/SQLPage), [links to other pages](/index.sql) and [intra-page links](#my-paragraph). + + ## Lists + ### Unordered lists + * SQLPage is easy + * SQLPage is fun + * SQLPage is free + + ### Ordered lists + 1. SQLPage is fast + 2. SQLPage is safe + 3. SQLPage is open-source + + ## Code + ```sql + SELECT 'list' AS component; + SELECT name as title FROM users; + ``` + + ## Tables + + | SQLPage component | Description | Documentation link | + | --- | --- | --- | + | text | A paragraph of text. | [Documentation](https://sql-page.com/documentation.sql?component=text) | + | list | A list of items. | [Documentation](https://sql-page.com/documentation.sql?component=list) | + | steps | A progress indicator. | [Documentation](https://sql-page.com/documentation.sql?component=steps) | + | form | A series of input fields. | [Documentation](https://sql-page.com/documentation.sql?component=form) | + + ## Quotes + > Fantastic. + > + > — [HackerNews User](https://news.ycombinator.com/item?id=36194473#36209061) about SQLPage + + ## Images + ![SQLPage logo](https://sql-page.com/favicon.ico) + + ## Horizontal rules + --- + +--- + +Rendering rich text using markdown diff --git a/examples/official-site/content/components/text/examples/03-rich-text.md b/examples/official-site/content/components/text/examples/03-rich-text.md new file mode 100644 index 000000000..30fb497fe --- /dev/null +++ b/examples/official-site/content/components/text/examples/03-rich-text.md @@ -0,0 +1,14 @@ +--- +rows: + - component: text + title: About SQL + - contents: SQL + bold: true + italics: true + - contents: ' is a domain-specific language used in programming and designed for managing data held in a ' + - contents: relational database management system + link: https://en.wikipedia.org/wiki/Relational_database + - contents: . It is particularly useful in handling structured data. +--- + +Rendering a paragraph with links and styling. diff --git a/examples/official-site/content/components/text/examples/04-section-link.md b/examples/official-site/content/components/text/examples/04-section-link.md new file mode 100644 index 000000000..968029b74 --- /dev/null +++ b/examples/official-site/content/components/text/examples/04-section-link.md @@ -0,0 +1,11 @@ +--- +rows: + - component: text + contents_md: This is a link to the [next paragraph](#my-paragraph). You can open this link in a new tab + and the page will scroll to the paragraph on load. + - component: text + id: my-paragraph + contents_md: This **is** the next paragraph. +--- + +An intra-page link to a section of the page. diff --git a/examples/official-site/content/components/title.md b/examples/official-site/content/components/title.md new file mode 100644 index 000000000..5c6f9d3e4 --- /dev/null +++ b/examples/official-site/content/components/title.md @@ -0,0 +1,21 @@ +--- +icon: letter-case-upper +since: 0.19.0 +category: page-layout +summary: Add a heading and choose its level. +top: + - name: center + type: BOOLEAN + description: Whether to center the title. + - class + - name: contents + type: TEXT + required: true + description: A text to display. + - id + - name: level + type: INTEGER + description: Set the heading level (default level is 1) +--- + +`level` sets the HTML heading (`h1` through `h6`). Use level 1 for the page title, level 2 for sections, and level 3 for subsections so readers can navigate by heading. diff --git a/examples/official-site/content/components/title/examples/01-levels.md b/examples/official-site/content/components/title/examples/01-levels.md new file mode 100644 index 000000000..8161c6827 --- /dev/null +++ b/examples/official-site/content/components/title/examples/01-levels.md @@ -0,0 +1,13 @@ +--- +rows: + - component: title + contents: Level 1 + - component: title + contents: Level 2 + level: 2 + - component: title + contents: Level 3 + level: 3 +--- + +Displays several titles with different levels. diff --git a/examples/official-site/content/components/title/examples/02-centered.md b/examples/official-site/content/components/title/examples/02-centered.md new file mode 100644 index 000000000..77497f3d6 --- /dev/null +++ b/examples/official-site/content/components/title/examples/02-centered.md @@ -0,0 +1,8 @@ +--- +rows: + - component: title + contents: Level 1 + center: true +--- + +Displays a centered title. diff --git a/examples/official-site/content/shared-parameters.yaml b/examples/official-site/content/shared-parameters.yaml new file mode 100644 index 000000000..573207c8c --- /dev/null +++ b/examples/official-site/content/shared-parameters.yaml @@ -0,0 +1,10 @@ +id: + name: id + type: TEXT + description: id attribute added to the container in HTML. It can be used to target this item through css + or for scrolling to this item through links (use "#id" in link url). +class: + name: class + type: TEXT + description: class attribute added to the container in HTML. It can be used to apply custom styling to this + item through css. Added in v0.18.0. diff --git a/examples/official-site/sqlpage/migrations/01_documentation.sql b/examples/official-site/sqlpage/migrations/01_documentation.sql index d82b0214e..051310b7d 100644 --- a/examples/official-site/sqlpage/migrations/01_documentation.sql +++ b/examples/official-site/sqlpage/migrations/01_documentation.sql @@ -165,82 +165,6 @@ INSERT INTO example(component, description, properties) VALUES '{"title": "Final account approval", "description": "ophir.dev", "link": "https://ophir.dev/", "icon":"eye-check", "active": true},' || '{"title":"Account creation", "icon":"check"}]')); -INSERT INTO component(name, icon, description) VALUES - ('text', 'align-left', 'A paragraph of text. The entire component will render as a single paragraph, with each item being rendered as a span of text inside it, the styling of which can be customized using parameters.'); - -INSERT INTO parameter(component, name, description, type, top_level, optional) SELECT 'text', * FROM (VALUES - -- top level - ('title', 'Text header before the paragraph.', 'TEXT', TRUE, TRUE), - ('center', 'Whether to center the title.', 'BOOLEAN', TRUE, TRUE), - ('html', 'Raw html code to include on the page. Don''t use that if you are not sure what you are doing, it may have security implications.', 'TEXT', TRUE, TRUE), - ('contents', 'A top-level paragraph of text to display, without any formatting, without having to make additional queries.', 'TEXT', TRUE, TRUE), - ('contents_md', 'Rich text in the markdown format. Among others, this allows you to write bold text using `**bold**`, italics using `*italics*`, and links using `[text](https://example.com)`.', 'TEXT', TRUE, TRUE), - ('article', 'Makes long texts more readable by increasing the line height, adding margins, using a serif font, and decorating the initial letter.', 'BOOLEAN', TRUE, TRUE), - -- item level - ('contents', 'A span of text to display', 'TEXT', FALSE, FALSE), - ('contents_md', 'Rich text in the markdown format. Among others, this allows you to write bold text using `**bold**`, italics using `*italics*`, and links using `[text](https://example.com)`.', 'TEXT', FALSE, TRUE), - ('link', 'An URL to which the user should be taken when they click on this span of text.', 'URL', FALSE, TRUE), - ('color', 'The name of a color for this span of text.', 'COLOR', FALSE, TRUE), - ('underline', 'Whether the span of text should be underlined.', 'BOOLEAN', FALSE, TRUE), - ('bold', 'Whether the span of text should be displayed as bold.', 'BOOLEAN', FALSE, TRUE), - ('code', 'Use a monospace font. Useful to display the text as code.', 'BOOLEAN', FALSE, TRUE), - ('italics', 'Whether the span of text should be displayed as italics.', 'BOOLEAN', FALSE, TRUE), - ('break', 'Indicates that the current span of text starts a new paragraph.', 'BOOLEAN', FALSE, TRUE), - ('size', 'A number between 1 and 6 indicating the font size.', 'INTEGER', FALSE, TRUE) -) x; - -INSERT INTO example(component, description, properties) VALUES - ('text', 'Rendering a simple text paragraph.', json('[{"component":"text", "contents":"Hello, world ! <3"}]')), - ('text', 'Rendering rich text using markdown', json('[{"component":"text", "article": true, "contents_md":"\n'|| - '# Markdown in SQLPage\n\n' || - '## Simple formatting\n\n' || - 'SQLPage supports only plain text as column values, but markdown allows easily adding **bold**, *italics*, [external links](https://github.com/sqlpage/SQLPage), [links to other pages](/index.sql) and [intra-page links](#my-paragraph). \n\n' || - '## Lists\n' || - '### Unordered lists\n' || - '* SQLPage is easy\n' || - '* SQLPage is fun\n' || - '* SQLPage is free\n\n' || - '### Ordered lists\n' || - '1. SQLPage is fast\n' || - '2. SQLPage is safe\n' || - '3. SQLPage is open-source\n\n' || - '## Code\n' || - '```sql\n' || - 'SELECT ''list'' AS component;\n' || - 'SELECT name as title FROM users;\n' || - '```\n\n' || - '## Tables\n\n' || - '| SQLPage component | Description | Documentation link |\n' || - '| --- | --- | --- |\n' || - '| text | A paragraph of text. | [Documentation](https://sql-page.com/documentation.sql?component=text) |\n' || - '| list | A list of items. | [Documentation](https://sql-page.com/documentation.sql?component=list) |\n' || - '| steps | A progress indicator. | [Documentation](https://sql-page.com/documentation.sql?component=steps) |\n' || - '| form | A series of input fields. | [Documentation](https://sql-page.com/documentation.sql?component=form) |\n\n' || - '## Quotes\n' || - '> Fantastic.\n>\n' || - '> — [HackerNews User](https://news.ycombinator.com/item?id=36194473#36209061) about SQLPage\n\n' || - '## Images\n' || - '![SQLPage logo](https://sql-page.com/favicon.ico)\n\n' || - '## Horizontal rules\n' || - '---\n\n' || - '"}]')), - ('text', 'Rendering a paragraph with links and styling.', - json('[{"component":"text", "title":"About SQL"}, '|| - '{"contents":"SQL", "bold":true, "italics": true}, '|| - '{"contents":" is a domain-specific language used in programming and designed for managing data held in a "},'|| - '{"contents": "relational database management system", "link": "https://en.wikipedia.org/wiki/Relational_database"},'|| - '{"contents": ". It is particularly useful in handling structured data."}]') - ), - ( - 'text', - 'An intra-page link to a section of the page.', - json('[ - {"component":"text", "contents_md":"This is a link to the [next paragraph](#my-paragraph). You can open this link in a new tab and the page will scroll to the paragraph on load."}, - {"component":"text", "id": "my-paragraph", "contents_md": "This **is** the next paragraph."} - ]') - ) -; - INSERT INTO component(name, icon, description) VALUES ('form', 'cursor-text', ' ## Building forms in SQL diff --git a/examples/official-site/sqlpage/migrations/35_code_title.sql b/examples/official-site/sqlpage/migrations/35_code_title.sql index bfa8b12cc..d6610a065 100644 --- a/examples/official-site/sqlpage/migrations/35_code_title.sql +++ b/examples/official-site/sqlpage/migrations/35_code_title.sql @@ -1,57 +1,3 @@ --- Documentation for the title component -INSERT INTO component (name, description, icon, introduced_in_version) VALUES ( - 'title', - '`level` sets the HTML heading (`h1` through `h6`). Use level 1 for the page title, level 2 for sections, and level 3 for subsections so readers can navigate by heading.', - 'letter-case-upper', - '0.19.0' -); - -INSERT INTO parameter (component,name,description,type,top_level,optional) VALUES ( - 'title', - 'center', - 'Whether to center the title.', - 'BOOLEAN', - TRUE, - TRUE -),( - 'title', - 'contents', - 'A text to display.', - 'TEXT', - TRUE, - FALSE -),( - 'title', - 'level', - 'Set the heading level (default level is 1)', - 'INTEGER', - TRUE, - TRUE -); - --- Insert example(s) for the component -INSERT INTO example(component, description, properties) VALUES ( - 'title', - 'Displays several titles with different levels.', - JSON( - '[ - {"component":"title","contents":"Level 1"}, - {"component":"title","contents":"Level 2","level": 2}, - {"component":"title","contents":"Level 3","level": 3} - ]' - ) -); - -INSERT INTO example(component, description, properties) VALUES ( - 'title', - 'Displays a centered title.', - JSON( - '[ - {"component":"title","contents":"Level 1","center": true} - ]' - ) -); - -- Documentation for the code component INSERT INTO component (name, description, icon, introduced_in_version) VALUES ( 'code', @@ -112,4 +58,4 @@ INSERT INTO example(component, description, properties) VALUES ( } ]' ) -); \ No newline at end of file +); diff --git a/examples/official-site/sqlpage/migrations/59_unsafe_contents_md.sql b/examples/official-site/sqlpage/migrations/59_unsafe_contents_md.sql deleted file mode 100644 index 5f8f2c97a..000000000 --- a/examples/official-site/sqlpage/migrations/59_unsafe_contents_md.sql +++ /dev/null @@ -1,4 +0,0 @@ -INSERT INTO parameter(component, name, description, type, top_level, optional) SELECT 'text', * FROM (VALUES -('unsafe_contents_md','Markdown format with html blocks. Use this only with trusted content. See the html-blocks section of the Commonmark spec for additional info.', 'TEXT', TRUE, TRUE), -('unsafe_contents_md','Markdown format with html blocks. Use this only with trusted content. See the html-blocks section of the Commonmark spec for additional info.', 'TEXT', FALSE, TRUE) -); diff --git a/examples/official-site/sqlpage/migrations/82_reference_catalog.sql b/examples/official-site/sqlpage/migrations/82_reference_catalog.sql index 462adbd05..b2a79f89f 100644 --- a/examples/official-site/sqlpage/migrations/82_reference_catalog.sql +++ b/examples/official-site/sqlpage/migrations/82_reference_catalog.sql @@ -59,8 +59,6 @@ WITH copy(name, category, summary) AS (VALUES ('hero', 'page-layout', 'Introduce a page with a headline, media, and a call to action.'), ('html', 'page-layout', 'Insert your own HTML markup into the page.'), ('shell', 'page-layout', 'Set the page layout, navigation, branding, and footer.'), - ('text', 'page-layout', 'Write paragraphs using plain text or Markdown.'), - ('title', 'page-layout', 'Add a heading and choose its level.'), ('cookie', 'http-responses', 'Set a cookie in the visitor''s browser.'), ('csv', 'http-responses', 'Export query rows as a downloadable CSV file.'), diff --git a/examples/official-site/sqlpage/migrations/99_shared_id_class_attributes.sql b/examples/official-site/sqlpage/migrations/99_shared_id_class_attributes.sql index 712cb683f..883c0b6a2 100644 --- a/examples/official-site/sqlpage/migrations/99_shared_id_class_attributes.sql +++ b/examples/official-site/sqlpage/migrations/99_shared_id_class_attributes.sql @@ -34,10 +34,8 @@ FROM (VALUES ('table', TRUE), ('timeline', TRUE), ('timeline', FALSE), - ('title', TRUE), ('toast', TRUE), ('tracking', TRUE), - ('text', TRUE), ('carousel', TRUE), ('login', TRUE), ('pagination', TRUE), @@ -77,7 +75,6 @@ FROM (VALUES ('table', TRUE), ('timeline', TRUE), ('timeline', FALSE), - ('title', TRUE), ('toast', TRUE), ('tracking', TRUE), ('carousel', TRUE), diff --git a/scripts/site/components.mjs b/scripts/site/components.mjs new file mode 100644 index 000000000..6939debfc --- /dev/null +++ b/scripts/site/components.mjs @@ -0,0 +1,103 @@ +import { readdirSync } from "node:fs"; +import { join } from "node:path"; +import { parse } from "yaml"; +import { atFile, fields, pages, parameter } from "./content.mjs"; + +export function insertComponents(seed, content) { + const shared = atFile( + join(content, "shared-parameters.yaml"), + (source) => + new Map( + Array.from(parse(source, { mapAsMap: true }), ([key, value]) => [ + key, + parameter(value), + ]), + ), + ); + const dir = join(content, "components"); + pages(dir, (name, meta, body) => { + const c = fields(meta, [ + "icon", + "since", + "category", + "summary", + "top", + "row", + ]); + seed.row( + `INSERT INTO component (name, description, icon, introduced_in_version, catalog_category, catalog_summary) + VALUES (?, ?, ?, ?, ?, ?)`, + name, + body, + c.icon ?? null, + c.since ?? null, + c.category ?? null, + c.summary ?? null, + ); + for (const [level, params] of [ + [1, c.top ?? []], + [0, c.row ?? []], + ]) { + for (const entry of params) { + const p = + typeof entry === "string" ? shared.get(entry) : parameter(entry); + if (!p) throw new Error(`Unknown shared parameter: ${entry}`); + seed.row( + `INSERT INTO parameter (component, top_level, name, type, description_md, optional) + VALUES (?, ?, ?, ?, ?, ?)`, + name, + level, + p.name, + p.type, + p.description, + p.optional, + ); + } + } + }); + // A component may still have a SQL-authored reference and Markdown examples. + const names = readdirSync(dir, { withFileTypes: true }) + .filter((e) => e.isDirectory()) + .map((e) => e.name) + .sort(); + for (const name of names) + insertExamples(seed, name, join(dir, name, "examples")); +} + +// Converting YAML maps to JS objects would move integer-like column names to +// the front. Converting YAML integers to JS numbers would round large IDs. +function orderedJson(value) { + if (Array.isArray(value)) return `[${value.map(orderedJson).join(",")}]`; + if (value instanceof Map) { + return `{${Array.from(value, ([key, v]) => { + if (typeof key !== "string") + throw new Error("Expected a text key in example rows"); + return `${JSON.stringify(key)}:${orderedJson(v)}`; + }).join(",")}}`; + } + if (typeof value === "bigint") return value.toString(); + if (typeof value === "number" && !Number.isFinite(value)) + throw new Error("Expected a finite JSON number"); + return JSON.stringify(value); +} + +export function insertExamples(seed, name, dir) { + pages( + dir, + (_, meta, caption) => { + const { rows } = fields(meta, ["rows"]); + if ( + rows !== undefined && + (!Array.isArray(rows) || !rows.every((r) => r instanceof Map)) + ) + throw new Error("Expected a list of rows"); + seed.row( + "INSERT INTO example (component, description, properties) VALUES (?, ?, ?)", + name, + caption, + rows === undefined ? null : orderedJson(rows), + ); + }, + { intAsBigInt: true }, + ); +} diff --git a/scripts/site/content.mjs b/scripts/site/content.mjs index 49104ed6d..49b902f6c 100644 --- a/scripts/site/content.mjs +++ b/scripts/site/content.mjs @@ -10,7 +10,7 @@ export function atFile(path, visit) { } } -export function pages(dir, visit) { +export function pages(dir, visit, yamlOptions = {}) { for (const file of readdirSync(dir) .filter((f) => f.endsWith(".md")) .sort()) { @@ -23,7 +23,8 @@ export function pages(dir, visit) { const end = /^---[ \t]*$/m.exec(rest); if (!end) throw new Error("Unclosed YAML front matter"); metadata = - parse(rest.slice(0, end.index), { mapAsMap: true }) ?? metadata; + parse(rest.slice(0, end.index), { mapAsMap: true, ...yamlOptions }) ?? + metadata; body = rest.slice(end.index + end[0].length); } visit(basename(file, ".md"), metadata, body.trim()); diff --git a/scripts/site/documentation.mjs b/scripts/site/documentation.mjs index 488f71504..2dc84c576 100644 --- a/scripts/site/documentation.mjs +++ b/scripts/site/documentation.mjs @@ -1,6 +1,7 @@ import { readdirSync, readFileSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { DatabaseSync } from "node:sqlite"; +import { insertComponents } from "./components.mjs"; import { fields, pages, text } from "./content.mjs"; import { insertDirectories } from "./directories.mjs"; import { insertFunctions } from "./functions.mjs"; @@ -71,6 +72,7 @@ export function writeDocumentation(content, migrations) { const seed = new Seed(db); insertDirectories(seed, content); insertPosts(seed, join(content, "blog")); + insertComponents(seed, content); insertFunctions(seed, join(content, "functions")); writeFileSync( join(migrations, `${version}_documentation_content.sql`), diff --git a/tests/site/components.test.mjs b/tests/site/components.test.mjs new file mode 100644 index 000000000..32a417d21 --- /dev/null +++ b/tests/site/components.test.mjs @@ -0,0 +1,43 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { DatabaseSync } from "node:sqlite"; +import test from "node:test"; +import { insertExamples } from "../../scripts/site/components.mjs"; +import { Seed } from "../../scripts/site/documentation.mjs"; + +test("example SQL retains file and column order and large integer values", (t) => { + const dir = mkdtempSync(join(tmpdir(), "sqlpage-examples-")); + t.after(() => rmSync(dir, { recursive: true, force: true })); + writeFileSync( + join(dir, "02-result.md"), + `--- +rows: + - component: text + contents: Reader's choice + "2": 9007199254740993 + "1": 42 +--- +Result`, + ); + writeFileSync(join(dir, "01-notes.md"), "Just a note."); + const db = new DatabaseSync(":memory:"); + t.after(() => db.close()); + db.exec("CREATE TABLE example (component, description, properties)"); + const seed = new Seed(db); + insertExamples(seed, "text", dir); + db.exec("DELETE FROM example"); + db.exec(seed.toString()); + const rows = db.prepare( + "SELECT description, properties FROM example ORDER BY rowid", + ); + rows.setReturnArrays(true); + assert.deepEqual(rows.all(), [ + ["Just a note.", null], + [ + "Result", + `[{"component":"text","contents":"Reader's choice","2":9007199254740993,"1":42}]`, + ], + ]); +});