use std::path::{Path, PathBuf};
use brrrty_config::DirOptions;
use brrrty_core::{ConfigOptions, PassthroughEntry, Problem, codes};
use brrrty_js::Value;
use brrrty_js::runtime::{JsResult, JsRuntime};
const CONFIG_FILES: &[&str] = &[
".eleventy.js",
"eleventy.config.js",
"eleventy.config.mjs",
"eleventy.config.cjs",
];
fn get<'a>(v: &'a Value, key: &str) -> Option<&'a Value> {
match v {
Value::Object(o) => o.get(key),
_ => None,
}
}
fn strings(v: Option<&Value>) -> Vec<String> {
match v {
Some(Value::Array(a)) => a
.iter()
.filter_map(|x| match x {
Value::String(s) => Some(s.to_string()),
_ => None,
})
.collect(),
Some(Value::String(s)) => s
.split(',')
.map(|p| p.trim().to_string())
.filter(|p| !p.is_empty())
.collect(),
_ => Vec::new(),
}
}
fn text(v: Option<&Value>) -> Option<String> {
match v {
Some(Value::String(s)) => Some(s.to_string()),
_ => None,
}
}
fn problem(message: String, file: Option<&Path>, line: Option<u32>) -> Problem {
let mut p = Problem::error(codes::CONFIG_ERROR, message);
if let Some(f) = file {
p = p.with_path(f);
}
if let Some(l) = line {
p = p.at(l as usize, 1);
}
p
}
pub fn apply(
root: &Path,
node: &brrrty_node::NodeInfo,
config: &mut ConfigOptions,
dir: &mut DirOptions,
run_mode: brrrty_node::RunMode,
order_check: bool,
) -> Result<(), Vec<Problem>> {
let Some(file) = CONFIG_FILES
.iter()
.map(|f| root.join(f))
.find(|f| f.is_file())
else {
config.problems.push(
Problem::warning(
codes::CONFIG_UNSUPPORTED,
"config.set_js_config(\"node\") is on, but there is no 11ty config file here",
)
.with_help(format!("looked for {}", CONFIG_FILES.join(", "))),
);
return Ok(());
};
let shown = file.strip_prefix(root).unwrap_or(&file).to_path_buf();
let mut worker =
brrrty_node::Worker::start_with(node, root, run_mode, order_check).map_err(|e| {
vec![problem(
format!(
"Brrrty could not start Node to load {}: {e}",
shown.display()
),
None,
None,
)]
})?;
let loaded = match worker.load_config(&file) {
JsResult::Ok(d) => d,
JsResult::Thrown(e) => {
return Err(vec![problem(
format!("{} threw: {}", shown.display(), e.message),
e.file.as_deref().map(|f| f.strip_prefix(root).unwrap_or(f)),
e.line,
)]);
}
JsResult::Refused(r) => {
return Err(vec![problem(
format!("{} could not be loaded: {}", shown.display(), r.why),
Some(&shown),
None,
)]);
}
JsResult::Failed(f) => {
return Err(vec![problem(
format!(
"Node stopped while loading {}: {}",
shown.display(),
f.message
),
Some(&shown),
None,
)]);
}
};
let v = &loaded.value;
add_filters(v, &shown, root, run_mode, config);
let values = get(v, "values");
let val = |k: &str| values.and_then(|x| get(x, k));
let mut passthrough = Vec::new();
if let Some(Value::Array(a)) = val("passthroughCopy") {
for c in a.iter() {
let Some(from) = text(get(c, "input")) else {
continue;
};
if matches!(get(c, "options"), Some(Value::Object(o)) if !o.is_empty()) {
config.problems.push(
Problem::warning(
codes::CONFIG_UNSUPPORTED,
format!(
"addPassthroughCopy(\"{from}\") options (like `filter` or `expand`) are not supported by Brrrty yet, so they were ignored"
),
)
.with_path(shown.clone()),
);
}
match get(c, "output") {
Some(Value::String(to)) => passthrough.push(PassthroughEntry {
from,
to: Some(to.to_string()),
}),
Some(Value::Bool(false)) => {}
_ => passthrough.push(PassthroughEntry { from, to: None }),
}
}
}
passthrough.append(&mut config.passthrough);
config.passthrough = passthrough;
let mut ignores = strings(val("ignores"));
ignores.retain(|i| !config.ignores.contains(i));
ignores.append(&mut config.ignores);
config.ignores = ignores;
if let Some(Value::Object(o)) = val("globalData") {
for (k, x) in o.iter() {
config
.global_data
.entry(k.clone())
.or_insert_with(|| x.clone());
}
}
let fmt_err = |m: String| vec![problem(m, Some(&shown), None)];
let js_returned = get(v, "returned");
let normalize = |what: &str, v: &Value| -> Result<Vec<String>, Vec<Problem>> {
brrrty_config::normalize_template_formats(what, strings(Some(v))).map_err(fmt_err)
};
if !config.template_formats_set {
let set = match val("templateFormats") {
Some(f) => Some(normalize("`setTemplateFormats`", f)?),
None => match js_returned.and_then(|r| get(r, "templateFormats")) {
Some(f) => Some(normalize("`templateFormats`", f)?),
None => None,
},
};
let mut added = Vec::new();
if let Some(Value::Array(a)) = val("templateFormatsAdded") {
for x in a.iter() {
added.extend(normalize("`addTemplateFormats`", x)?);
}
}
added.extend(config.added_template_formats.iter().cloned());
let formats = match set {
Some(f) => Some(f),
None if !added.is_empty() => Some(
["liquid", "md", "njk", "html", "11ty.js"]
.map(String::from)
.to_vec(),
),
None => None,
}
.map(|mut f| {
for a in added {
if !f.contains(&a) {
f.push(a);
}
}
f
});
config.template_formats = formats.filter(|f| !f.is_empty());
}
if config.template_engines == brrrty_core::TemplateEngines::default() {
for (key, slot) in [("markdownTemplateEngine", 0), ("htmlTemplateEngine", 1)] {
let Some(e) = js_returned.and_then(|r| get(r, key)) else {
continue;
};
let lang = match e {
Value::Bool(false) => None,
Value::String(n) => match brrrty_core::TemplateLanguage::from_name(n) {
Some(l) => Some(l),
None => {
return Err(vec![problem(
format!(
"`{key}` is `{n}`, which Brrrty doesn't run there (use \"liquid\", \"njk\" or false)"
),
Some(&shown),
None,
)]);
}
},
_ => {
return Err(vec![problem(
format!("`{key}` should be a template language, like \"njk\", or false"),
Some(&shown),
None,
)]);
}
};
if slot == 0 {
config.template_engines.markdown = lang;
} else {
config.template_engines.html = lang;
}
}
}
if let Some(Value::Object(o)) = js_returned {
for (k, _) in o.iter() {
if !matches!(
k.as_str(),
"dir" | "templateFormats" | "markdownTemplateEngine" | "htmlTemplateEngine"
) {
let why = if k == "dataTemplateEngine" {
"11ty 3 doesn't read it"
} else {
"Brrrty doesn't support it yet"
};
config.problems.push(
Problem::warning(
codes::CONFIG_UNSUPPORTED,
format!("the config returns `{k}`, which was skipped: {why}"),
)
.with_path(shown.clone()),
);
}
}
}
let rhai = dir.clone();
for source in [val("dir"), get(v, "returned").and_then(|r| get(r, "dir"))] {
let Some(source) = source else { continue };
let theirs = DirOptions {
input: text(get(source, "input")),
output: text(get(source, "output")),
includes: text(get(source, "includes")),
data: text(get(source, "data")),
layouts: text(get(source, "layouts")),
};
if rhai.input.is_none() && theirs.input.is_some() {
dir.input = theirs.input;
}
if rhai.output.is_none() && theirs.output.is_some() {
dir.output = theirs.output;
}
if rhai.includes.is_none() && theirs.includes.is_some() {
dir.includes = theirs.includes;
}
if rhai.data.is_none() && theirs.data.is_some() {
dir.data = theirs.data;
}
if rhai.layouts.is_none() && theirs.layouts.is_some() {
dir.layouts = theirs.layouts;
}
}
config.includes = dir.includes.clone();
config.layouts = dir.layouts.clone();
config.data_folder = dir.data.clone();
if let Some(Value::Array(un)) = get(v, "unsupported") {
for u in un.iter() {
let method = text(get(u, "method")).unwrap_or_default();
let at = text(get(u, "at")).unwrap_or_default();
let mut p = Problem::warning(
codes::CONFIG_UNSUPPORTED,
format!("{at}: eleventyConfig.{method} is not supported by Brrrty yet, so it was skipped"),
)
.with_help("pages that need it may differ from 11ty's, or refuse");
if let Some((f, l)) = at.rsplit_once(':') {
p = p.with_path(PathBuf::from(f));
if let Ok(l) = l.parse::<usize>() {
p = p.at(l, 1);
}
}
config.problems.push(p);
}
}
config
.problems
.retain(|p| p.code != brrrty_core::codes::CONFIG_UNVERIFIED);
config.js_started = Some(brrrty_core::StartedJs(std::sync::Arc::new(
std::sync::Mutex::new(Some(Box::new(worker))),
)));
Ok(())
}
fn filter_this(eleventy: &Value) -> Value {
let mut o = indexmap::IndexMap::new();
if let Some((page, _)) = brrrty_js::functions::own_page() {
o.insert("page".to_string(), page);
}
o.insert("eleventy".to_string(), eleventy.clone());
Value::Object(std::sync::Arc::new(o))
}
const NUNJUCKS_THIS: &[(&str, &str)] = &[
("ctx", "Nunjucks's render context"),
("env", "the Nunjucks environment"),
];
const LIQUID_THIS: &[(&str, &str)] = &[
("context", "LiquidJS's variable scope"),
("liquid", "the LiquidJS engine"),
];
fn add_filters(
v: &Value,
shown: &Path,
root: &Path,
run_mode: brrrty_node::RunMode,
config: &mut ConfigOptions,
) {
use brrrty_core::{FilterEngine, JsConfigFilter, JsConfigShortcode, JsLiquidShortcode};
use brrrty_js::functions::{Settle, This};
let Some(Value::Array(fns)) = get(v, "functions") else {
return;
};
let eleventy = Value::from_json(&serde_json::json!({
"version": "3.1.8",
"generator": "Eleventy v3.1.8",
"env": { "source": "cli", "runMode": run_mode.as_str(), "root": root.display().to_string() },
}));
let mut transforms: Vec<(String, brrrty_core::JsConfigTransform)> = Vec::new();
for f in fns.iter() {
if text(get(f, "kind")).as_deref() == Some("transform") {
if let (Some(name), Some(Value::Function(h))) = (text(get(f, "name")), get(f, "fn")) {
let t = transform(*h, &name, shown);
match transforms.iter_mut().find(|(n, _)| *n == name) {
Some(slot) => slot.1 = t,
None => transforms.push((name, t)),
}
}
continue;
}
let (what, shortcode) = match text(get(f, "kind")).as_deref() {
Some("filter") => ("filter", None),
Some("shortcode") => ("shortcode", Some(false)),
Some("pairedShortcode") => ("shortcode", Some(true)),
_ => continue,
};
let (Some(name), Some(Value::Function(h))) = (text(get(f, "name")), get(f, "fn")) else {
continue;
};
let engine = match text(get(f, "engine")).as_deref() {
Some("all") => FilterEngine::All,
Some("nunjucks") => FilterEngine::Nunjucks,
Some("liquid") => FilterEngine::Liquid,
_ => continue,
};
let is_async = matches!(get(f, "async"), Some(Value::Bool(true)));
let make = |settle: Settle,
guard: &'static [(&'static str, &'static str)]|
-> brrrty_liquid::ShortcodeFn {
let (h, n, eleventy, shown, what) = (
*h,
name.clone(),
eleventy.clone(),
shown.display().to_string(),
what,
);
std::sync::Arc::new(move |all: &[Value], _| {
let Some(caller) = brrrty_js::functions::installed() else {
return Err(format!(
"{}the {what} `{n}` (from {shown}) can't run: Node isn't running",
brrrty_js::REFUSAL_PREFIX
));
};
let this = filter_this(&eleventy);
caller
.call(h, This::Guarded(&this, guard), all, settle)
.map_err(|why| {
format!(
"{}the {what} `{n}` (from {shown}) failed: {why}",
brrrty_js::REFUSAL_PREFIX
)
})
})
};
let njk = if is_async {
Settle::Await
} else {
Settle::AsIs
};
let as_filter = |run: brrrty_liquid::ShortcodeFn| -> brrrty_liquid::ArgsFilterFn {
std::sync::Arc::new(move |input, args, ctx| {
let mut all = Vec::with_capacity(args.len() + 1);
all.push(input.clone());
all.extend_from_slice(args);
run(&all, ctx)
})
};
if engine != FilterEngine::Liquid {
let run = make(njk, NUNJUCKS_THIS);
match shortcode {
Some(paired) => config.js_shortcodes.push(JsConfigShortcode {
name: name.clone(),
paired,
engine: FilterEngine::Nunjucks,
run: as_filter(run),
}),
None => config.js_filters.push(JsConfigFilter {
name: name.clone(),
engine: FilterEngine::Nunjucks,
run: as_filter(run),
}),
}
}
if engine != FilterEngine::Nunjucks {
let run = make(Settle::Await, LIQUID_THIS);
match shortcode {
Some(paired) => {
config
.js_liquid_shortcodes
.push(JsLiquidShortcode { name, paired, run })
}
None => config.js_filters.push(JsConfigFilter {
name,
engine: FilterEngine::Liquid,
run: as_filter(run),
}),
}
}
}
let index = |n: &str| {
n.parse::<u32>()
.ok()
.filter(|i| i.to_string() == n && *i != u32::MAX)
};
transforms.sort_by_key(|(n, _)| index(n).map_or((1, 0), |i| (0, i)));
config
.js_transforms
.extend(transforms.into_iter().map(|(_, t)| t));
}
fn transform(h: brrrty_js::FnHandle, name: &str, shown: &Path) -> brrrty_core::JsConfigTransform {
use brrrty_js::functions::{Settle, This};
let (name, shown) = (name.to_string(), shown.display().to_string());
let n = name.clone();
let run = std::sync::Arc::new(move |content: &str, page: &Value| {
let fail = |why: String| {
format!(
"{}the transform `{n}` (from {shown}) failed: {why}",
brrrty_js::REFUSAL_PREFIX
)
};
let Some(caller) = brrrty_js::functions::installed() else {
return Err(fail("Node isn't running".to_string()));
};
let field = |k: &str| get(page, k).cloned().unwrap_or(Value::Undefined);
let (input, output, url) = (field("inputPath"), field("outputPath"), field("url"));
let mut o = indexmap::IndexMap::new();
o.insert("inputPath".to_string(), input);
o.insert("outputPath".to_string(), output.clone());
o.insert("url".to_string(), url);
o.insert("page".to_string(), page.clone());
o.insert("baseHref".to_string(), Value::Undefined);
let this = Value::Object(std::sync::Arc::new(o));
let args = [Value::String(content.into()), output];
match caller.call(h, This::Value(&this), &args, Settle::Await) {
Ok(Value::String(s)) => Ok(s.to_string()),
Ok(other) => Err(fail(format!(
"it gave {}, not text, and 11ty can't write that",
other.to_js_string()
))),
Err(why) => Err(fail(why)),
}
});
brrrty_core::JsConfigTransform { name, run }
}