feat: Option to hide original element

This commit is contained in:
2026-05-22 13:07:56 +02:00
parent dd2e2ae9d0
commit 0e8876eede
7 changed files with 128 additions and 17 deletions
Generated
+21
View File
@@ -1577,6 +1577,19 @@ dependencies = [
"zmij",
]
[[package]]
name = "serde_yaml"
version = "0.9.34+deprecated"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47"
dependencies = [
"indexmap",
"itoa",
"ryu",
"serde",
"unsafe-libyaml",
]
[[package]]
name = "simd-abstraction"
version = "0.7.1"
@@ -1676,7 +1689,9 @@ dependencies = [
"oxc_parser",
"oxc_span",
"rand",
"serde",
"serde_json",
"serde_yaml",
]
[[package]]
@@ -1755,6 +1770,12 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254"
[[package]]
name = "unsafe-libyaml"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"
[[package]]
name = "utf8parse"
version = "0.2.2"
+2
View File
@@ -4,6 +4,8 @@ version = "0.1.0"
edition = "2024"
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9"
serde_json = "1.0"
clap = { version = "4.0", features = ["derive"] }
indoc = "2"
+3
View File
@@ -117,10 +117,13 @@ fn construct_template(template_path: &str) -> Template {
);
});
let config_path = format!("{}/config.yaml", template_path);
Template::TemplateInjector {
template: template_content,
replace_selector: replace_selector.trim().to_string(),
scraper,
config_path: Some(config_path),
script: post_script,
style,
}
+50
View File
@@ -0,0 +1,50 @@
use serde::Deserialize;
#[derive(Debug, Deserialize, PartialEq)]
pub enum ReplaceType {
#[serde(rename = "hide")]
Hide,
#[serde(rename = "replace")]
Replace,
}
impl Default for ReplaceType {
fn default() -> Self {
ReplaceType::Replace
}
}
#[derive(Debug, Deserialize, Default)]
pub struct TemplateReplacer {
#[serde(default)]
pub replace_type: ReplaceType,
}
#[derive(Debug, Deserialize, Default)]
pub struct Scraper {
#[serde(default)]
pub rerender_key: String,
}
#[derive(Debug, Deserialize, Default)]
pub struct Config {
#[serde(default)]
pub template_replacer: TemplateReplacer,
#[serde(default)]
pub scraper: Scraper,
}
impl Config {
pub fn load(path: &Option<String>) -> Self {
match path {
Some(path_str) => match std::fs::File::open(path_str) {
Ok(file) => {
let reader = std::io::BufReader::new(file);
serde_yaml::from_reader(reader).unwrap_or_default()
}
Err(_) => Config::default(),
},
None => Config::default(),
}
}
}
+1
View File
@@ -1,2 +1,3 @@
pub mod compiler;
pub mod models;
pub mod config;
+1
View File
@@ -31,6 +31,7 @@ pub enum Template {
scraper: Option<String>,
style: Option<String>,
script: Option<String>,
config_path: Option<String>,
},
}
+35 -2
View File
@@ -1,6 +1,7 @@
use indoc::indoc;
use serde_json;
use crate::compiler::config::{Config, ReplaceType};
use crate::css::{minify_css, scope_css};
use crate::compiler::models::{PageRoute, Project, Template};
@@ -142,6 +143,7 @@ impl Template {
scraper,
script,
style,
config_path,
} => {
let scope_class = format!("template-style-{}", rand::random::<u32>());
let script = if let Some(script) = script {
@@ -154,6 +156,8 @@ impl Template {
String::new()
};
let config = Config::load(config_path);
let style_script = if let Some(style) = style {
if style.trim().is_empty() {
String::new()
@@ -175,7 +179,9 @@ impl Template {
String::new()
};
let script_template = indoc! { r#"
let script_template =
if config.template_replacer.replace_type == ReplaceType::Replace {
indoc! { r#"
(function() {
{style_script}
@@ -197,7 +203,34 @@ impl Template {
{script}
})();
})();
"# };
"#
}
} else {
indoc! { r#"
(function() {
{style_script}
const parsedData = eval({scraper} || "(() => ({}))");
const template = Handlebars.compile({template});
const rendered = template(parsedData);
const targetElement = document.querySelector({replace_selector});
targetElement.style.display = 'none';
const temp = document.createElement('template');
temp.innerHTML = rendered;
const newElement = temp.content.firstElementChild;
targetElement.after(newElement);
newElement.classList.add({scope_class});
(function() {
{script}
})();
})();
"# }
};
script_template
.replace("{style_script}", &style_script.trim())