Skip to content

Commit

Permalink
Add support for markdown in tables
Browse files Browse the repository at this point in the history
Closes #4
See #11
  • Loading branch information
lovasoa committed Jun 11, 2023
1 parent fc85aee commit a14a2bb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sqlpage"
version = "0.6.11"
version = "0.6.12"
edition = "2021"
description = "A SQL-only web application framework. Takes .sql files and formats the query result using pre-made configurable professional-looking components."
keywords = ["web", "sql", "framework"]
Expand Down
12 changes: 10 additions & 2 deletions examples/official-site/sqlpage/migrations/01_documentation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,22 @@ INSERT INTO component(name, icon, description) VALUES
INSERT INTO parameter(component, name, description, type, top_level, optional) SELECT 'table', * FROM (VALUES
-- top level
('sort', 'Make the columns clickable to let the user sort by the value contained in the column.', 'BOOLEAN', TRUE, TRUE),
('search', 'Add a search bar at the top of the table, letting users easily filter table rows by value.', 'BOOLEAN', TRUE, TRUE)
('search', 'Add a search bar at the top of the table, letting users easily filter table rows by value.', 'BOOLEAN', TRUE, TRUE),
('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)
) x;

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


INSERT INTO component(name, icon, description) VALUES
Expand Down
8 changes: 7 additions & 1 deletion sqlpage/templates/table.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@

<tr>
{{~#each this~}}
<td class="{{@key}}">{{this}}</td>
<td class="{{@key}}">
{{~#if (array_contains ../../markdown @key)~}}
{{{markdown this}}}
{{~else~}}
{{this}}
{{~/if~}}
</td>
{{~/each~}}
</tr>
{{~/each_row~}}
Expand Down
16 changes: 15 additions & 1 deletion src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ impl AllTemplates {
});

handlebars.register_helper("entries", Box::new(entries));

// delay helper: store a piece of information in memory that can be output later with flush_delayed
handlebars.register_helper("delay", Box::new(delay_helper));
handlebars.register_helper("flush_delayed", Box::new(flush_delayed_helper));

Expand All @@ -201,16 +203,28 @@ impl AllTemplates {

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

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

// array_contains: check if an array contains an element. If the first argument is not an array, it is compared to the second argument.
handlebars_helper!(array_contains: |array: Json, element: Json| match array {
JsonValue::Array(arr) => arr.contains(element),
other => other == element
});
handlebars.register_helper("array_contains", Box::new(array_contains));

// static_path helper: generate a path to a static file. Replaces sqpage.js by sqlpage.<hash>.js
handlebars_helper!(static_path: |x: str| match x {
"sqlpage.js" => static_filename!("sqlpage.js"),
"sqlpage.css" => static_filename!("sqlpage.css"),
_ => "!!unknown static path!!"
unknown => {
log::error!("Unknown static path: {}", unknown);
"!!unknown static path!!"
}
});
handlebars.register_helper("static_path", Box::new(static_path));

Expand Down

0 comments on commit a14a2bb

Please sign in to comment.