Skip to content

Commit 3bcde10

Browse files
committed
Keep text and title references and previews together
1 parent 3788c61 commit 3bcde10

17 files changed

Lines changed: 355 additions & 140 deletions

‎examples/build_site/components.rs‎

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
use super::{
2+
Seed,
3+
content::{Parameter, at, pages},
4+
};
5+
use anyhow::{Context, Result};
6+
use serde::Deserialize;
7+
use serde_json::Value;
8+
use std::{collections::BTreeMap, fs, path::Path};
9+
10+
#[derive(Deserialize)]
11+
#[serde(untagged)]
12+
enum ComponentParameter {
13+
Shared(String),
14+
Inline(Parameter),
15+
}
16+
17+
#[derive(Default, Deserialize)]
18+
#[serde(deny_unknown_fields)]
19+
struct Component {
20+
icon: Option<String>,
21+
since: Option<String>,
22+
category: Option<String>,
23+
summary: Option<String>,
24+
#[serde(default)]
25+
top: Vec<ComponentParameter>,
26+
#[serde(default)]
27+
row: Vec<ComponentParameter>,
28+
}
29+
30+
#[derive(Default, Deserialize)]
31+
#[serde(deny_unknown_fields)]
32+
struct Example {
33+
// Mapping order is the column order in the SQL shown to readers.
34+
rows: Option<Vec<serde_json::Map<String, Value>>>,
35+
}
36+
37+
pub(super) fn insert(seed: &mut Seed, content: &Path) -> Result<()> {
38+
let shared_path = content.join("shared-parameters.yaml");
39+
let shared: BTreeMap<String, Parameter> = at(&shared_path, || {
40+
let entries: BTreeMap<String, Parameter> =
41+
serde_yaml_ng::from_str(&fs::read_to_string(&shared_path)?)?;
42+
for parameter in entries.values() {
43+
parameter.validate()?;
44+
}
45+
Ok(entries)
46+
})?;
47+
let dir = content.join("components");
48+
pages(&dir, |name, c: Component, body| {
49+
seed.row(
50+
"INSERT INTO component (name, description, icon, introduced_in_version, catalog_category, catalog_summary)
51+
VALUES (?, ?, ?, ?, ?, ?)",
52+
&[&name, &body, &c.icon, &c.since, &c.category, &c.summary],
53+
)?;
54+
for (top, parameters) in [(true, c.top), (false, c.row)] {
55+
for p in &parameters {
56+
let p = match p {
57+
ComponentParameter::Inline(p) => p,
58+
ComponentParameter::Shared(key) => shared
59+
.get(key)
60+
.with_context(|| format!("Unknown shared parameter {key:?}"))?,
61+
};
62+
p.validate()?;
63+
seed.row(
64+
"INSERT INTO parameter (component, top_level, name, type, description_md, optional)
65+
VALUES (?, ?, ?, ?, ?, ?)",
66+
&[&name, &top, &p.name, &p.r#type, &p.description, &!p.required],
67+
)?;
68+
}
69+
}
70+
Ok(())
71+
})?;
72+
73+
// Some examples move before their component reference. That reference
74+
// still comes from a checked-in migration in that case.
75+
let mut dirs = fs::read_dir(&dir)?.collect::<std::io::Result<Vec<_>>>()?;
76+
dirs.sort_by_key(fs::DirEntry::file_name);
77+
for entry in dirs {
78+
let examples = entry.path().join("examples");
79+
if entry.file_type()?.is_dir() && examples.try_exists()? {
80+
let name = entry.file_name();
81+
let component = name.to_str().context("Invalid component directory name")?;
82+
insert_examples(seed, component, &examples)?;
83+
}
84+
}
85+
Ok(())
86+
}
87+
88+
fn insert_examples(seed: &mut Seed, component: &str, dir: &Path) -> Result<()> {
89+
pages(dir, |_, e: Example, description| {
90+
let properties = e
91+
.rows
92+
.map(|rows| serde_json::to_string(&rows))
93+
.transpose()?;
94+
seed.row(
95+
"INSERT INTO example (component, description, properties) VALUES (?, ?, ?)",
96+
&[&component, &description, &properties],
97+
)
98+
})
99+
}
100+
101+
#[cfg(test)]
102+
mod tests {
103+
use super::*;
104+
use rusqlite::Connection;
105+
106+
#[test]
107+
fn examples_keep_file_order_column_order_and_large_integers() -> Result<()> {
108+
let examples = tempfile::tempdir()?;
109+
fs::write(
110+
examples.path().join("02-result.md"),
111+
"---\nrows:\n - component: text\n contents: \"Reader's choice\"\n \"2\": 9007199254740993\n \"1\": 42\n---\nResult\n",
112+
)?;
113+
fs::write(examples.path().join("01-notes.md"), "Just a note.\n")?;
114+
let schema = "CREATE TABLE example (component, description, properties);";
115+
let mut seed = Seed::new()?;
116+
seed.execute(schema)?;
117+
insert_examples(&mut seed, "text", examples.path())?;
118+
let db = Connection::open_in_memory()?;
119+
db.execute_batch(schema)?;
120+
db.execute_batch(&seed.finish())?;
121+
let rows = db
122+
.prepare("SELECT description, properties FROM example ORDER BY rowid")?
123+
.query_map([], |row| {
124+
Ok((row.get::<_, String>(0)?, row.get::<_, Option<String>>(1)?))
125+
})?
126+
.collect::<rusqlite::Result<Vec<_>>>()?;
127+
assert_eq!(
128+
rows,
129+
[
130+
("Just a note.".into(), None),
131+
("Result".into(), Some(r#"[{"component":"text","contents":"Reader's choice","2":9007199254740993,"1":42}]"#.into())),
132+
]
133+
);
134+
Ok(())
135+
}
136+
}

‎examples/build_site/main.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod blog;
2+
mod components;
23
mod configuration;
34
mod content;
45
mod directories;
@@ -93,6 +94,7 @@ async fn documentation_seed(site: &Path) -> Result<String> {
9394
}
9495
}
9596
blog::insert(&mut seed, &site.join("content/blog"))?;
97+
components::insert(&mut seed, &site.join("content"))?;
9698
configuration::insert(&mut seed)?;
9799
directories::insert(&mut seed, &site.join("content"))?;
98100
functions::insert(&mut seed, &site.join("content/functions"))?;

‎examples/official-site/README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ Function pages in `content/functions/` use their filename as the public function
4646
name. Keep the argument list in signature order and mark mandatory arguments with
4747
`required: true`. The Markdown body can include SQL examples without escaping.
4848

49+
## Component reference
50+
51+
Component pages in `content/components/` list parameters under `top` and `row`.
52+
Each `examples/*.md` file can contain `rows` in its YAML metadata. These rows
53+
drive both the live preview and the copyable SQL. The Markdown body is the
54+
example caption. Use numbered filenames to keep the examples in order.
55+
References that have not moved still come from the SQL migrations.
56+
4957
## Landing page
5058

5159
The home page is rendered by `index.sql` and the `landing-page` custom template.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
icon: align-left
3+
category: page-layout
4+
summary: Write paragraphs using plain text or Markdown.
5+
top:
6+
- name: article
7+
type: BOOLEAN
8+
description: Makes long texts more readable by increasing the line height, adding margins, using a serif
9+
font, and decorating the initial letter.
10+
- name: center
11+
type: BOOLEAN
12+
description: Whether to center the title.
13+
- name: contents
14+
type: TEXT
15+
description: A top-level paragraph of text to display, without any formatting, without having to make additional
16+
queries.
17+
- name: contents_md
18+
type: TEXT
19+
description: Rich text in the markdown format. Among others, this allows you to write bold text using `**bold**`,
20+
italics using `*italics*`, and links using `[text](https://example.com)`.
21+
- name: html
22+
type: TEXT
23+
description: Raw html code to include on the page. Don't use that if you are not sure what you are doing,
24+
it may have security implications.
25+
- id
26+
- name: title
27+
type: TEXT
28+
description: Text header before the paragraph.
29+
- name: unsafe_contents_md
30+
type: TEXT
31+
description: Markdown format with html blocks. Use this only with trusted content. See the html-blocks
32+
section of the Commonmark spec for additional info.
33+
row:
34+
- name: bold
35+
type: BOOLEAN
36+
description: Whether the span of text should be displayed as bold.
37+
- name: break
38+
type: BOOLEAN
39+
description: Indicates that the current span of text starts a new paragraph.
40+
- name: code
41+
type: BOOLEAN
42+
description: Use a monospace font. Useful to display the text as code.
43+
- name: color
44+
type: COLOR
45+
description: The name of a color for this span of text.
46+
- name: contents
47+
type: TEXT
48+
required: true
49+
description: A span of text to display
50+
- name: contents_md
51+
type: TEXT
52+
description: Rich text in the markdown format. Among others, this allows you to write bold text using `**bold**`,
53+
italics using `*italics*`, and links using `[text](https://example.com)`.
54+
- name: italics
55+
type: BOOLEAN
56+
description: Whether the span of text should be displayed as italics.
57+
- name: link
58+
type: URL
59+
description: An URL to which the user should be taken when they click on this span of text.
60+
- name: size
61+
type: INTEGER
62+
description: A number between 1 and 6 indicating the font size.
63+
- name: underline
64+
type: BOOLEAN
65+
description: Whether the span of text should be underlined.
66+
- name: unsafe_contents_md
67+
type: TEXT
68+
description: Markdown format with html blocks. Use this only with trusted content. See the html-blocks
69+
section of the Commonmark spec for additional info.
70+
---
71+
72+
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.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
rows:
3+
- component: text
4+
contents: Hello, world ! <3
5+
---
6+
7+
Rendering a simple text paragraph.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
rows:
3+
- component: text
4+
article: true
5+
contents_md: |+
6+
7+
# Markdown in SQLPage
8+
9+
## Simple formatting
10+
11+
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).
12+
13+
## Lists
14+
### Unordered lists
15+
* SQLPage is easy
16+
* SQLPage is fun
17+
* SQLPage is free
18+
19+
### Ordered lists
20+
1. SQLPage is fast
21+
2. SQLPage is safe
22+
3. SQLPage is open-source
23+
24+
## Code
25+
```sql
26+
SELECT 'list' AS component;
27+
SELECT name as title FROM users;
28+
```
29+
30+
## Tables
31+
32+
| SQLPage component | Description | Documentation link |
33+
| --- | --- | --- |
34+
| text | A paragraph of text. | [Documentation](https://sql-page.com/documentation.sql?component=text) |
35+
| list | A list of items. | [Documentation](https://sql-page.com/documentation.sql?component=list) |
36+
| steps | A progress indicator. | [Documentation](https://sql-page.com/documentation.sql?component=steps) |
37+
| form | A series of input fields. | [Documentation](https://sql-page.com/documentation.sql?component=form) |
38+
39+
## Quotes
40+
> Fantastic.
41+
>
42+
> — [HackerNews User](https://news.ycombinator.com/item?id=36194473#36209061) about SQLPage
43+
44+
## Images
45+
![SQLPage logo](https://sql-page.com/favicon.ico)
46+
47+
## Horizontal rules
48+
---
49+
50+
---
51+
52+
Rendering rich text using markdown
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
rows:
3+
- component: text
4+
title: About SQL
5+
- contents: SQL
6+
bold: true
7+
italics: true
8+
- contents: ' is a domain-specific language used in programming and designed for managing data held in a '
9+
- contents: relational database management system
10+
link: https://en.wikipedia.org/wiki/Relational_database
11+
- contents: . It is particularly useful in handling structured data.
12+
---
13+
14+
Rendering a paragraph with links and styling.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
rows:
3+
- component: text
4+
contents_md: This is a link to the [next paragraph](#my-paragraph). You can open this link in a new tab
5+
and the page will scroll to the paragraph on load.
6+
- component: text
7+
id: my-paragraph
8+
contents_md: This **is** the next paragraph.
9+
---
10+
11+
An intra-page link to a section of the page.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
icon: letter-case-upper
3+
since: 0.19.0
4+
category: page-layout
5+
summary: Add a heading and choose its level.
6+
top:
7+
- name: center
8+
type: BOOLEAN
9+
description: Whether to center the title.
10+
- class
11+
- name: contents
12+
type: TEXT
13+
required: true
14+
description: A text to display.
15+
- id
16+
- name: level
17+
type: INTEGER
18+
description: Set the heading level (default level is 1)
19+
---
20+
21+
Defines HTML headings. The level 1 is used for the maximal size and the level 6 is used for the minimal size.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
rows:
3+
- component: title
4+
contents: Level 1
5+
- component: title
6+
contents: Level 2
7+
level: 2
8+
- component: title
9+
contents: Level 3
10+
level: 3
11+
---
12+
13+
Displays several titles with different levels.

0 commit comments

Comments
 (0)