Skip to main content

rumtk_web/components/html/
md.rs

1/*
2 *     rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 *     This toolkit aims to be reliable, simple, performant, and standards compliant.
4 *     Copyright (C) 2026  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
5 *     Copyright (C) 2026  MedicalMasses L.L.C. <contact@medicalmasses.com>
6 *
7 *     This program is free software: you can redistribute it and/or modify
8 *     it under the terms of the GNU General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 *
12 *     This program is distributed in the hope that it will be useful,
13 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *     GNU General Public License for more details.
16 *
17 *     You should have received a copy of the GNU General Public License
18 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 */
20use crate::defaults::{DEFAULT_NO_TEXT, DEFAULT_TEXT_ITEM, PARAMS_CONTENTS, PARAMS_CSS_CLASS};
21use crate::utils::types::{SharedAppState, URLParams, URLPath};
22use crate::{rumtk_web_get_config, rumtk_web_get_text_item, rumtk_web_render_markdown, sanitize_html, ComponentResult, RUMWebTemplate, RUMWebTemplateSafe};
23use rumtk_core::strings::RUMString;
24
25#[derive(RUMWebTemplate, Debug, Clone)]
26#[template(
27    source = "
28        {% if custom_css_enabled %}
29            <link href='/static/components/md.css' rel='stylesheet'>
30        {% endif %}
31        <pre class='md-{{css_class}} md'>
32            {{sanitized|safe}}
33        </pre>
34    ",
35    ext = "html"
36)]
37pub struct Markdown {
38    sanitized: RUMString,
39    css_class: RUMString,
40    custom_css_enabled: bool,
41}
42
43impl RUMWebTemplateSafe for Markdown {}
44
45pub fn markdown<'a>(_path_components: URLPath<'a, 'a>, params: URLParams<'a>, state: SharedAppState) -> ComponentResult<Markdown> {
46    let md = rumtk_web_get_text_item!(params, PARAMS_CONTENTS, DEFAULT_NO_TEXT);
47    let css_class = rumtk_web_get_text_item!(params, PARAMS_CSS_CLASS, DEFAULT_TEXT_ITEM).to_string();
48
49    let custom_css_enabled = rumtk_web_get_config!(state).flags.custom_css;
50
51    let rendered = rumtk_web_render_markdown!(md);
52    let sanitized = sanitize_html(&rendered, false);
53
54    Ok(Markdown { sanitized, css_class, custom_css_enabled })
55}
56