Skip to content

Commit a14a2bb

Browse files
committed
Add support for markdown in tables
Closes #4 See #11
1 parent fc85aee commit a14a2bb

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sqlpage"
3-
version = "0.6.11"
3+
version = "0.6.12"
44
edition = "2021"
55
description = "A SQL-only web application framework. Takes .sql files and formats the query result using pre-made configurable professional-looking components."
66
keywords = ["web", "sql", "framework"]

examples/official-site/sqlpage/migrations/01_documentation.sql

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,22 @@ INSERT INTO component(name, icon, description) VALUES
317317
INSERT INTO parameter(component, name, description, type, top_level, optional) SELECT 'table', * FROM (VALUES
318318
-- top level
319319
('sort', 'Make the columns clickable to let the user sort by the value contained in the column.', 'BOOLEAN', TRUE, TRUE),
320-
('search', 'Add a search bar at the top of the table, letting users easily filter table rows by value.', 'BOOLEAN', TRUE, TRUE)
320+
('search', 'Add a search bar at the top of the table, letting users easily filter table rows by value.', 'BOOLEAN', TRUE, TRUE),
321+
('markdown', 'Set this to the name of a column whose content should be interpreted as markdown . Used to display rich text with links in the table. This argument can be repeated multiple times to intepret multiple columns as markdown.', 'TEXT', TRUE, TRUE)
321322
) x;
322323

323324
INSERT INTO example(component, description, properties) VALUES
325+
('table', 'The most basic table.',
326+
json('[{"component":"table"}, {"a": 1, "b": 2}, {"a": 3, "b": 4}]')),
324327
('table', 'A table of users with filtering and sorting.',
325328
json('[{"component":"table", "sort":true, "search":true}, '||
326329
'{"Forename": "Ophir", "Surname": "Lojkine", "Pseudonym": "lovasoa"},' ||
327-
'{"Forename": "Linus", "Surname": "Torvalds", "Pseudonym": "torvalds"}]'));
330+
'{"Forename": "Linus", "Surname": "Torvalds", "Pseudonym": "torvalds"}]')),
331+
('table', 'A table that uses markdown to display links',
332+
json('[{"component":"table", "markdown": "Documentation"}, '||
333+
'{"name": "table", "description": "Displays SQL results as a searchable table.", "Documentation": "[docs](documentation.sql?component=table)"},
334+
{"name": "chart", "description": "Show graphs based on numeric data.", "Documentation": "[docs](documentation.sql?component=chart)"}
335+
]'));
328336

329337

330338
INSERT INTO component(name, icon, description) VALUES

sqlpage/templates/table.handlebars

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@
2727

2828
<tr>
2929
{{~#each this~}}
30-
<td class="{{@key}}">{{this}}</td>
30+
<td class="{{@key}}">
31+
{{~#if (array_contains ../../markdown @key)~}}
32+
{{{markdown this}}}
33+
{{~else~}}
34+
{{this}}
35+
{{~/if~}}
36+
</td>
3137
{{~/each~}}
3238
</tr>
3339
{{~/each_row~}}

src/templates.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ impl AllTemplates {
190190
});
191191

192192
handlebars.register_helper("entries", Box::new(entries));
193+
194+
// delay helper: store a piece of information in memory that can be output later with flush_delayed
193195
handlebars.register_helper("delay", Box::new(delay_helper));
194196
handlebars.register_helper("flush_delayed", Box::new(flush_delayed_helper));
195197

@@ -201,16 +203,28 @@ impl AllTemplates {
201203

202204
handlebars.register_helper("sum", Box::new(sum_helper));
203205

206+
// to_array: convert a value to a single-element array. If the value is already an array, return it as-is.
204207
handlebars_helper!(to_array: |x: Json| match x {
205208
JsonValue::Array(arr) => arr.clone(),
206209
other => vec![other.clone()]
207210
});
208211
handlebars.register_helper("to_array", Box::new(to_array));
209212

213+
// array_contains: check if an array contains an element. If the first argument is not an array, it is compared to the second argument.
214+
handlebars_helper!(array_contains: |array: Json, element: Json| match array {
215+
JsonValue::Array(arr) => arr.contains(element),
216+
other => other == element
217+
});
218+
handlebars.register_helper("array_contains", Box::new(array_contains));
219+
220+
// static_path helper: generate a path to a static file. Replaces sqpage.js by sqlpage.<hash>.js
210221
handlebars_helper!(static_path: |x: str| match x {
211222
"sqlpage.js" => static_filename!("sqlpage.js"),
212223
"sqlpage.css" => static_filename!("sqlpage.css"),
213-
_ => "!!unknown static path!!"
224+
unknown => {
225+
log::error!("Unknown static path: {}", unknown);
226+
"!!unknown static path!!"
227+
}
214228
});
215229
handlebars.register_helper("static_path", Box::new(static_path));
216230

0 commit comments

Comments
 (0)