add: html footer config
This commit is contained in:
parent
4af112d615
commit
cf38022597
5 changed files with 35 additions and 3 deletions
|
@ -105,6 +105,9 @@ pub const LANG_EN_US: &str = include_str!("./langs/en-US.toml");
|
|||
pub const VENDOR_SPOTIFY_ICON: &str = include_str!("./public/images/vendor/spotify.svg");
|
||||
pub const VENDOR_LAST_FM_ICON: &str = include_str!("./public/images/vendor/last-fm.svg");
|
||||
|
||||
pub(crate) static HTML_FOOTER: LazyLock<RwLock<String>> =
|
||||
LazyLock::new(|| RwLock::new(String::new()));
|
||||
|
||||
/// A container for all loaded icons.
|
||||
pub(crate) static ICONS: LazyLock<RwLock<HashMap<String, String>>> =
|
||||
LazyLock::new(|| RwLock::new(HashMap::new()));
|
||||
|
@ -148,6 +151,24 @@ macro_rules! vendor_icon {
|
|||
/// * icons (with class specifier)
|
||||
/// * l10n text
|
||||
pub(crate) async fn replace_in_html(input: &str, config: &Config) -> String {
|
||||
let reader = HTML_FOOTER.read().await;
|
||||
|
||||
if reader.is_empty() {
|
||||
if !config.html_footer_path.is_empty() {
|
||||
drop(reader);
|
||||
|
||||
let mut writer = HTML_FOOTER.write().await;
|
||||
*writer = read_to_string(PathBufD::current().join(&config.html_footer_path)).unwrap();
|
||||
} else {
|
||||
drop(reader);
|
||||
}
|
||||
} else {
|
||||
drop(reader);
|
||||
}
|
||||
|
||||
let reader = HTML_FOOTER.read().await;
|
||||
|
||||
// ...
|
||||
let mut input = input.to_string();
|
||||
input = input.replace("<!-- prettier-ignore -->", "");
|
||||
|
||||
|
@ -195,6 +216,7 @@ pub(crate) async fn replace_in_html(input: &str, config: &Config) -> String {
|
|||
}
|
||||
|
||||
// return
|
||||
input = input.replacen("<!-- html_footer_goes_here -->", &format!("{reader}"), 1);
|
||||
input
|
||||
}
|
||||
|
||||
|
|
|
@ -90,6 +90,7 @@ macros -%}
|
|||
</main>
|
||||
</article>
|
||||
{% else %} {% block body %}{% endblock %} {% endif %}
|
||||
<!-- html_footer_goes_here -->
|
||||
</div>
|
||||
|
||||
<script data-turbo-permanent="true" id="init-script">
|
||||
|
|
|
@ -150,8 +150,7 @@ impl Default for TurnstileConfig {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||
#[derive(Default)]
|
||||
#[derive(Clone, Serialize, Deserialize, Debug, Default)]
|
||||
pub struct ConnectionsConfig {
|
||||
/// <https://developer.spotify.com/documentation/web-api>
|
||||
#[serde(default)]
|
||||
|
@ -164,7 +163,6 @@ pub struct ConnectionsConfig {
|
|||
pub last_fm_secret: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
/// Configuration file
|
||||
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||
pub struct Config {
|
||||
|
@ -222,6 +220,10 @@ pub struct Config {
|
|||
pub town_square: usize,
|
||||
#[serde(default)]
|
||||
pub connections: ConnectionsConfig,
|
||||
/// The path to the HTML footer file. The contents of this file are embedded
|
||||
/// into every HTML template. They support access to template fields like `{{ user }}`.
|
||||
#[serde(default)]
|
||||
pub html_footer_path: String,
|
||||
}
|
||||
|
||||
fn default_name() -> String {
|
||||
|
@ -308,6 +310,7 @@ impl Default for Config {
|
|||
turnstile: default_turnstile(),
|
||||
town_square: 0,
|
||||
connections: default_connections(),
|
||||
html_footer_path: String::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
5
example/public/footer.html
Normal file
5
example/public/footer.html
Normal file
|
@ -0,0 +1,5 @@
|
|||
{% if user %}
|
||||
<script>
|
||||
console.log("current user is {{ user.username }}");
|
||||
</script>
|
||||
{% endif %}
|
|
@ -18,6 +18,7 @@ banned_usernames = [
|
|||
"anonymous",
|
||||
]
|
||||
town_square = 166340372315581657
|
||||
html_footer_path = "public/footer.html"
|
||||
|
||||
[security]
|
||||
registration_enabled = true
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue