-
Notifications
You must be signed in to change notification settings - Fork 963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
inject site config during Sass compilation #2242
base: next
Are you sure you want to change the base?
Changes from all commits
8d16d17
b00ae42
1ed722c
6a5c241
c48b61a
7edf92b
7448309
4f77980
1321a83
b5a90db
961d07d
86532cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
mod common; | ||
use config::Config; | ||
|
||
#[test] | ||
fn can_transform_image() { | ||
let cases = vec![ | ||
"![haha](https://example.com/abc.jpg)", | ||
"![](https://example.com/abc.jpg)", | ||
"![ha\"h>a](https://example.com/abc.jpg)", | ||
"![__ha__*ha*](https://example.com/abc.jpg)", | ||
"![ha[ha](https://example.com)](https://example.com/abc.jpg)", | ||
]; | ||
|
||
let body = common::render(&cases.join("\n")).unwrap().body; | ||
insta::assert_snapshot!(body); | ||
} | ||
|
||
#[test] | ||
fn can_add_lazy_loading_and_async_decoding() { | ||
let cases = vec![ | ||
"![haha](https://example.com/abc.jpg)", | ||
"![](https://example.com/abc.jpg)", | ||
"![ha\"h>a](https://example.com/abc.jpg)", | ||
"![__ha__*ha*](https://example.com/abc.jpg)", | ||
"![ha[ha](https://example.com)](https://example.com/abc.jpg)", | ||
]; | ||
|
||
let mut config = Config::default_for_test(); | ||
config.markdown.lazy_async_image = true; | ||
|
||
let body = common::render_with_config(&cases.join("\n"), config).unwrap().body; | ||
insta::assert_snapshot!(body); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
source: components/markdown/tests/img.rs | ||
expression: body | ||
--- | ||
<p><img src="https://example.com/abc.jpg" alt="haha" loading="lazy" decoding="async" /> | ||
<img src="https://example.com/abc.jpg" alt="" loading="lazy" decoding="async" /> | ||
<img src="https://example.com/abc.jpg" alt="ha"h>a" loading="lazy" decoding="async" /> | ||
<img src="https://example.com/abc.jpg" alt="<strong>ha</strong><em>ha</em>" loading="lazy" decoding="async" /> | ||
<img src="https://example.com/abc.jpg" alt="ha<a href="https://example.com">ha</a>" loading="lazy" decoding="async" /></p> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
source: components/markdown/tests/img.rs | ||
expression: body | ||
--- | ||
<p><img src="https://example.com/abc.jpg" alt="haha" /> | ||
<img src="https://example.com/abc.jpg" alt="" /> | ||
<img src="https://example.com/abc.jpg" alt="ha"h>a" /> | ||
<img src="https://example.com/abc.jpg" alt="haha" /> | ||
<img src="https://example.com/abc.jpg" alt="haha" /></p> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,27 @@ | ||
use std::fs::create_dir_all; | ||
use std::path::{Path, PathBuf}; | ||
|
||
use config::Config; | ||
use libs::globset::Glob; | ||
use libs::grass::{from_path as compile_file, Options, OutputStyle}; | ||
use libs::walkdir::{DirEntry, WalkDir}; | ||
use tempfile::{tempdir, TempDir}; | ||
|
||
use crate::anyhow; | ||
use errors::{bail, Result}; | ||
use errors::{bail, Context, Result}; | ||
use utils::fs::{create_file, ensure_directory_exists}; | ||
|
||
pub fn compile_sass(base_path: &Path, output_path: &Path) -> Result<()> { | ||
mod serde; | ||
|
||
pub fn compile_sass(base_path: &Path, output_path: &Path, config: &Config) -> Result<()> { | ||
ensure_directory_exists(output_path)?; | ||
|
||
let sass_path = { | ||
let mut sass_path = PathBuf::from(base_path); | ||
sass_path.push("sass"); | ||
sass_path | ||
}; | ||
let sass_path = PathBuf::from(base_path).join("sass"); | ||
|
||
let dependencies_dir = build_dependencies_dir_from_config(config)?; | ||
|
||
let options = Options::default().style(OutputStyle::Compressed); | ||
let options = | ||
Options::default().style(OutputStyle::Compressed).load_path(dependencies_dir.path()); | ||
let files = get_non_partial_scss(&sass_path); | ||
let mut compiled_paths = Vec::new(); | ||
|
||
|
@@ -52,6 +55,24 @@ pub fn compile_sass(base_path: &Path, output_path: &Path) -> Result<()> { | |
Ok(()) | ||
} | ||
|
||
/// write out a subset of the Zola config document to a temporary SCSS file | ||
/// as an SCSS map variable literal. this will allow parts of the site's | ||
/// config to be usable during Sass compilation. this enables theme configuration | ||
/// like allowing the site owner to change header color. this function returns | ||
/// a tempdir holding a single `.scss` file. the tempdir should then be used as | ||
/// a load directory above when compiling the site's Sass files. the tempdir | ||
/// and contained `.scss` file will be deleted on drop of the returned `TempDir` | ||
/// struct, which should happen after Sass compilation finishes. | ||
fn build_dependencies_dir_from_config(config: &Config) -> Result<TempDir> { | ||
let dir = tempdir().context("failed to create tempdir for SASS dependencies")?; | ||
|
||
let config_serialized = serde::serialize_config(config)?; | ||
|
||
std::fs::write(dir.path().join("zola.scss"), format!("$config: {}", config_serialized))?; | ||
|
||
Ok(dir) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think of writing it to the sass directory so people can easily see what there is? It would be still overwritten on each serve/build but we can maybe not delete it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you're saying write it to the user's sass source directory? i figured that would be undesirable because then maybe it gets erroneously checked in. but i'm open to it if that seems better. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be ok to check it in? It's not going to change very often There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the more i think about it, the more i prefer not having the injected config in source. i don't particularly like it being checked in when the author didn't write it and when it's liable to get overwritten by the Zola compiler. that creates a scenario where the author is looking through the sass directory and erroneously edits that file, only to have their edits confusingly wiped out on the next zola build. we could solve that with a warning comment at the top of that file, but i prefer just hiding the source since the author isn't writing it. i added site docs explaining this feature, in my opinion that makes clear to the author what's going on. what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's fine to not add it the directory but it's a bit annoying that you can't see it to look at what exactly is defined There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any suggestions on how to surface this content to the site builder without putting it in the directory? i could create a non-tmp directory in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly I'm not too sure. Maybe it's ok to just show an example of how it looks like in the docs |
||
} | ||
|
||
fn is_partial_scss(entry: &DirEntry) -> bool { | ||
entry.file_name().to_str().map(|s| s.starts_with('_')).unwrap_or(false) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand the name of that function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here we take the config document and construct a directory that contains files that are ultimately added as dependencies when compiling Sass. the Sass compiler asks for a "load dir" during compilation, so instead of providing a single file we must provide a single directory containing a single file. so the function name is basically "builds a dependencies directory from the config document". happy to rename, do you have a suggestion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
write_temp_config_file
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that works for me. seems odd to me that a function would be named that but return a directory instead of a file, but whatever.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah fair enough
create_config_load_path
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i like it