feat: Option to hide original element
This commit is contained in:
Generated
+21
@@ -1577,6 +1577,19 @@ dependencies = [
|
|||||||
"zmij",
|
"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]]
|
[[package]]
|
||||||
name = "simd-abstraction"
|
name = "simd-abstraction"
|
||||||
version = "0.7.1"
|
version = "0.7.1"
|
||||||
@@ -1676,7 +1689,9 @@ dependencies = [
|
|||||||
"oxc_parser",
|
"oxc_parser",
|
||||||
"oxc_span",
|
"oxc_span",
|
||||||
"rand",
|
"rand",
|
||||||
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"serde_yaml",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1755,6 +1770,12 @@ version = "0.2.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254"
|
checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unsafe-libyaml"
|
||||||
|
version = "0.2.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "utf8parse"
|
name = "utf8parse"
|
||||||
version = "0.2.2"
|
version = "0.2.2"
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_yaml = "0.9"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
clap = { version = "4.0", features = ["derive"] }
|
clap = { version = "4.0", features = ["derive"] }
|
||||||
indoc = "2"
|
indoc = "2"
|
||||||
|
|||||||
@@ -117,10 +117,13 @@ fn construct_template(template_path: &str) -> Template {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let config_path = format!("{}/config.yaml", template_path);
|
||||||
|
|
||||||
Template::TemplateInjector {
|
Template::TemplateInjector {
|
||||||
template: template_content,
|
template: template_content,
|
||||||
replace_selector: replace_selector.trim().to_string(),
|
replace_selector: replace_selector.trim().to_string(),
|
||||||
scraper,
|
scraper,
|
||||||
|
config_path: Some(config_path),
|
||||||
script: post_script,
|
script: post_script,
|
||||||
style,
|
style,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,2 +1,3 @@
|
|||||||
pub mod compiler;
|
pub mod compiler;
|
||||||
pub mod models;
|
pub mod models;
|
||||||
|
pub mod config;
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ pub enum Template {
|
|||||||
scraper: Option<String>,
|
scraper: Option<String>,
|
||||||
style: Option<String>,
|
style: Option<String>,
|
||||||
script: Option<String>,
|
script: Option<String>,
|
||||||
|
config_path: Option<String>,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+50
-17
@@ -1,6 +1,7 @@
|
|||||||
use indoc::indoc;
|
use indoc::indoc;
|
||||||
use serde_json;
|
use serde_json;
|
||||||
|
|
||||||
|
use crate::compiler::config::{Config, ReplaceType};
|
||||||
use crate::css::{minify_css, scope_css};
|
use crate::css::{minify_css, scope_css};
|
||||||
|
|
||||||
use crate::compiler::models::{PageRoute, Project, Template};
|
use crate::compiler::models::{PageRoute, Project, Template};
|
||||||
@@ -142,6 +143,7 @@ impl Template {
|
|||||||
scraper,
|
scraper,
|
||||||
script,
|
script,
|
||||||
style,
|
style,
|
||||||
|
config_path,
|
||||||
} => {
|
} => {
|
||||||
let scope_class = format!("template-style-{}", rand::random::<u32>());
|
let scope_class = format!("template-style-{}", rand::random::<u32>());
|
||||||
let script = if let Some(script) = script {
|
let script = if let Some(script) = script {
|
||||||
@@ -154,6 +156,8 @@ impl Template {
|
|||||||
String::new()
|
String::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let config = Config::load(config_path);
|
||||||
|
|
||||||
let style_script = if let Some(style) = style {
|
let style_script = if let Some(style) = style {
|
||||||
if style.trim().is_empty() {
|
if style.trim().is_empty() {
|
||||||
String::new()
|
String::new()
|
||||||
@@ -175,29 +179,58 @@ impl Template {
|
|||||||
String::new()
|
String::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
let script_template = indoc! { r#"
|
let script_template =
|
||||||
(function() {
|
if config.template_replacer.replace_type == ReplaceType::Replace {
|
||||||
{style_script}
|
indoc! { r#"
|
||||||
|
(function() {
|
||||||
|
{style_script}
|
||||||
|
|
||||||
const parsedData = eval({scraper} || "(() => ({}))");
|
const parsedData = eval({scraper} || "(() => ({}))");
|
||||||
const template = Handlebars.compile({template});
|
const template = Handlebars.compile({template});
|
||||||
const rendered = template(parsedData);
|
const rendered = template(parsedData);
|
||||||
const targetElement = document.querySelector({replace_selector});
|
const targetElement = document.querySelector({replace_selector});
|
||||||
|
|
||||||
const temp = document.createElement('template');
|
const temp = document.createElement('template');
|
||||||
temp.innerHTML = rendered;
|
temp.innerHTML = rendered;
|
||||||
|
|
||||||
const newElement = temp.content.firstElementChild;
|
const newElement = temp.content.firstElementChild;
|
||||||
|
|
||||||
targetElement.replaceWith(newElement);
|
targetElement.replaceWith(newElement);
|
||||||
|
|
||||||
newElement.classList.add({scope_class});
|
newElement.classList.add({scope_class});
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
{script}
|
{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
|
script_template
|
||||||
.replace("{style_script}", &style_script.trim())
|
.replace("{style_script}", &style_script.trim())
|
||||||
|
|||||||
Reference in New Issue
Block a user