Skip to content

Commit

Permalink
fix #600
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasoa committed Sep 24, 2024
1 parent ce1b59f commit 38dbde4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Add new properties to the foldable component: `id`, `class`, and `expanded` (to control the state of the foldable item). The old behavior was having the first foldable item initially opened and the others closed. To keep the old behavior, you need to explicitly set `true as expanded` on the first foldable item.
- **divider component**: Add new properties to the divider component: `link`, `bold`, `italics`, `underline`, `size`.
- **form component**: fix slight misalignment and sizing issues of checkboxes and radio buttons.
- **table component**: fixed a bug where markdown contents of table cells would not be rendered as markdown if the column name contained uppercase letters on Postgres. Column name matching is now case-insensitive, so `'title' as markdown` will work the same as `'Title' as markdown`. In postgres, non-double-quoted identifiers are always folded to lowercase.

## 0.28.0 (2024-08-31)
- Chart component: fix the labels of pie charts displaying too many decimal places.
Expand Down
8 changes: 4 additions & 4 deletions sqlpage/templates/table.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
{{#if (not (starts_with @key '_sqlpage_'))}}
<th class="
{{~@key~}}
{{~#if (array_contains ../../align_right @key)}} text-end {{/if~}}
{{~#if (array_contains_case_insensitive ../../align_right @key)}} text-end {{/if~}}
">
{{~#if ../../sort~}}
<button class="table-sort sort d-inline" data-sort="{{@key}}">{{@key}}</button>
Expand All @@ -39,13 +39,13 @@
{{~#if (not (starts_with @key '_sqlpage_'))~}}
<td class="
{{~@key~}}
{{~#if (array_contains ../../align_right @key)
{{~#if (array_contains_case_insensitive ../../align_right @key)
}} text-end {{
/if}} align-middle">
{{~#if (array_contains ../../markdown @key)~}}
{{~#if (array_contains_case_insensitive ../../markdown @key)~}}
{{{markdown this}}}
{{~else~}}
{{~#if (array_contains ../../icon @key)~}}
{{~#if (array_contains_case_insensitive ../../icon @key)~}}
{{~icon_img this~}}
{{~else~}}
{{this}}
Expand Down
16 changes: 16 additions & 0 deletions src/template_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ pub fn register_all_helpers(h: &mut Handlebars<'_>, config: &AppConfig) {
});
h.register_helper("array_contains", Box::new(array_contains));

// array_contains_case_insensitive: check if an array contains an element case-insensitively. If the first argument is not an array, it is compared to the second argument case-insensitively.
handlebars_helper!(array_contains_case_insensitive: |array: Json, element: Json| {
match array {
JsonValue::Array(arr) => arr.iter().any(|v| json_eq_case_insensitive(v, element)),
other => json_eq_case_insensitive(other, element),
}
});
h.register_helper("array_contains_case_insensitive", Box::new(array_contains_case_insensitive));

// static_path helper: generate a path to a static file. Replaces sqpage.js by sqlpage.<hash>.js
register_helper(h, "static_path", StaticPathHelper(site_prefix.clone()));
register_helper(h, "app_config", AppConfigHelper(config.clone()));
Expand All @@ -55,6 +64,13 @@ pub fn register_all_helpers(h: &mut Handlebars<'_>, config: &AppConfig) {
register_helper(h, "csv_escape", csv_escape_helper as HH);
}

fn json_eq_case_insensitive(a: &JsonValue, b: &JsonValue) -> bool {
match (a, b) {
(JsonValue::String(a), JsonValue::String(b)) => a.eq_ignore_ascii_case(b),
_ => a == b,
}
}

fn stringify_helper(v: &JsonValue) -> JsonValue {
v.to_string().into()
}
Expand Down
6 changes: 6 additions & 0 deletions tests/sql_test_files/it_works_markdown_in_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- https://github.com/lovasoa/SQLpage/discussions/600
select 'table' as component, 'Link' AS markdown; -- uppercase Link
select '[It works !](https://example.com)
[comment]: <> (This is a comment. If this is visible, there is an error in markdown rendering)' as link; -- lowercase "link".
-- If the markdown is not rendered, the page will contain the string "error" and trigger a test failure.

0 comments on commit 38dbde4

Please sign in to comment.