use super::bundle::Bundle;
use super::partials::{InMemoryPartialResolver, expand_partials, load_partials};
use super::variables::{bind, frontmatter_variables};
use super::{
OccurrenceKind, ParseOptions, control_flow, crossrefs, frontmatter, functions, occurrences,
parse, parse_with_occurrences,
};
use crate::next::index;
use crate::next::parser::ParserArgs;
use crate::next::renderers::html;
use crate::next::schema::Config;
use crate::next::schema::value::Value;
use crate::types::{NodeType, Scalar};
const DOC: &str = "\u{feff}---\ntitle: Hello\nproducts: [Bot250, Bot600]\n---\n\
# {% $markdoc.frontmatter.title %} {% tag \"intro\" /%}\n\n\
<!-- vale off -->\n\n\
{% notice type=\"warning\" %}\n\
{% if include($markdoc.frontmatter.products, \"Bot250\") %}\nBot250 \"inside\"\n{% /if %}\n\
{% /notice %}\n\n\
{% for $markdoc.frontmatter.products as=\"p\" %}\n- {% $p %}\n{% /for %}\n\n\
{% switch $count %}\n{% case 2 %}\ntwo\n{% /case %}\n{% case %}\nother\n{% /case %}\n{% /switch %}\n\n\
{% partial file=\"part.md\" variables={who: \"me\"} /%}\n\n\
See {% tagref \"intro\" /%} and {% tagref \"gone\" /%}.\n\n\
{% callout %}\n indented\n {% /callout %}\n";
const UPSTREAM_HTML: &str = "<article><h1> </h1><p><!-- vale off --></p><ul><li></li></ul>\
<p>two</p><p>other</p><p>See and .</p><p>indented </p></article>";
const UPSTREAM_ERRORS: [&str; 13] = [
"tag-undefined",
"tag-undefined",
"tag-undefined",
"tag-undefined",
"tag-undefined",
"tag-undefined",
"attribute-value-invalid",
"tag-undefined",
"tag-undefined",
"missing-closing",
"tag-undefined",
"missing-opening",
"tag-undefined",
];
fn upstream_run() -> (String, Vec<String>, serde_json::Value) {
let doc = index::parse(DOC, &ParserArgs::default()).unwrap();
let config = Config::default();
let ids = index::validate(&doc, &config)
.unwrap()
.into_iter()
.map(|e| e.error.id)
.collect();
let out = html::render(&index::transform(&doc, &config).unwrap()).unwrap();
(out, ids, serde_json::to_value(&doc).unwrap())
}
const OVERLAY: &str = "[parse]\nallow_indentation = true\nallow_comments = true\n\
strip_bom = true\nfrontmatter = \"yaml\"\n\n[render]\ntypographer = true\n";
const PART: &str = "\u{feff}Hello from {% $who %}, {% $markdoc.frontmatter.title %}\n";
fn all_layers() -> (String, Vec<String>) {
let mut bundle = Bundle::builtin();
bundle.overlay(OVERLAY).unwrap();
let options = bundle.render_options();
let doc = parse(DOC, &options).unwrap();
assert_eq!(doc.children[0].node_type, NodeType::Heading, "BOM stripped");
let split = frontmatter::split(DOC).unwrap();
assert_eq!(split.yaml, "title: Hello\nproducts: [Bot250, Bot600]");
assert_eq!(
doc.attributes["frontmatter"],
Scalar::Object(frontmatter::parse_yaml(split.yaml).unwrap())
);
let mut config = bundle.config();
control_flow::apply_bundle(&bundle, &mut config);
config.functions.extend(functions::all());
frontmatter_variables(&doc).apply(&mut config);
bind(&mut config, "count", Value::Number(2.0));
let resolver = InMemoryPartialResolver::new().with("part.md", PART);
let partials = load_partials(&doc, &resolver, &options).unwrap();
config.partials = Some(partials.clone());
let ids: Vec<String> = index::validate(&doc, &config)
.unwrap()
.into_iter()
.map(|e| e.error.id)
.collect();
let out = html::render(&index::transform(&doc, &config).unwrap()).unwrap();
let evaluated = control_flow::evaluate_conditionals(&doc, &config).unwrap();
let again = html::render(&index::transform(&evaluated, &config).unwrap()).unwrap();
assert_eq!(again, out);
let expanded = expand_partials(&doc, &partials).unwrap();
assert!(expanded.walk().all(|n| n.tag.as_deref() != Some("partial")));
let checked = crossrefs::resolve_crossrefs(&expanded);
let broken: Vec<&str> = checked
.walk()
.flat_map(|n| n.errors.iter().map(|e| e.id.as_str()))
.filter(|id| id.starts_with("tagref/"))
.collect();
assert_eq!(broken, ["tagref/unknown-target"]);
assert!(crossrefs::collect_anchors(&expanded).contains_key("intro"));
let lint = bundle.parse_options();
let (ast, found) = parse_with_occurrences(DOC, &lint).unwrap();
assert_eq!(
serde_json::to_value(&ast).unwrap(),
serde_json::to_value(parse(DOC, &lint).unwrap()).unwrap()
);
let alone = occurrences(DOC, &lint).unwrap();
assert_eq!(alone.len(), found.len());
let opens = found
.iter()
.filter(|o| matches!(o.kind, OccurrenceKind::Open { .. }))
.count();
let closes = found
.iter()
.filter(|o| matches!(o.kind, OccurrenceKind::Close { .. }))
.count();
assert_eq!((opens, closes), (7, 7));
for (i, o) in found.iter().enumerate() {
if let Some(p) = o.partner {
assert_eq!(found[p].partner, Some(i));
}
assert!(DOC[o.span.clone()].starts_with("{%"));
}
(out, ids)
}
#[test]
fn every_layer_is_off_until_called() {
let before = upstream_run();
assert_eq!(before.0, UPSTREAM_HTML);
assert_eq!(before.1, UPSTREAM_ERRORS);
assert_eq!(
serde_json::to_value(parse(DOC, &ParseOptions::default()).unwrap()).unwrap(),
before.2
);
all_layers();
assert_eq!(upstream_run(), before);
let merged = index::merge_config(&Config::default());
let mut tags: Vec<&str> = merged.tags.keys().map(String::as_str).collect();
tags.sort_unstable();
assert_eq!(tags, ["else", "if", "partial", "slot", "table"]);
let mut names: Vec<&str> = merged.functions.keys().map(String::as_str).collect();
names.sort_unstable();
assert_eq!(names, ["and", "debug", "default", "equals", "not", "or"]);
assert!(merged.variables.is_none() && merged.partials.is_none());
let table = merged.nodes["table"].as_ref().unwrap();
assert!(
table
.attributes
.as_ref()
.is_none_or(|a| !a.contains_key("borders")),
"the bundle's [nodes.table] additions stay in the bundle's config"
);
}
#[test]
fn the_layers_fit_together() {
let (out, ids) = all_layers();
assert_eq!(
out,
"<article><h1>Hello <tag primary=\"intro\"></tag></h1>\
<callout type=\"warning\"><p>Bot250 \u{201c}inside\u{201d}</p></callout>\
<ul><li>Bot250</li><li>Bot600</li></ul><p>two</p>\
<p>Hello from me, Hello</p>\
<p>See <tagref primary=\"intro\"></tagref> and <tagref primary=\"gone\"></tagref>.</p>\
<callout type=\"note\"><p>indented</p></callout></article>"
);
assert_eq!(ids, ["variable-undefined"]);
}