markdoc 0.1.0

Rust implementation of the Markdoc document language (CommonMark plus a {% tag %} extension)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//! Control flow beyond upstream's `if`/`else`: `{% switch %}` with `{% case %}`, and
//! `{% for %}` (an extension, off unless its schemas are put in the config).
//!
//! Upstream Markdoc has no loops in documents ("Markdoc does not support writing loops
//! directly in documents": its loop is a custom tag with a `transform` of its own). These are
//! such tags: [`schemas`] gives their [`Schema`]s, whose `transform` closures do the work at
//! transform time, as upstream's own `if` and `partial` do. Without them in `config.tags` a
//! document renders as upstream renders it: `switch`, `case` and `for` are undeclared tags,
//! which render their children once (and `validate` reports `tag-undefined`).
//!
//! - `{% switch $x %}` renders the children of its first `{% case %}` whose value is `===`
//!   the subject (upstream's `equals`), or of the first `{% case %}` without a value (a
//!   default; order decides, as with `{% else /%}`); nothing if none matches. Cases are
//!   found among the switch's children and inside the paragraphs and other non-tag nodes
//!   that hold them (`{% case 1 %}one{% /case %}` written on one line is inside a
//!   paragraph), never inside another tag, so a nested switch keeps its own cases. A case
//!   outside a switch renders its children.
//! - `{% for $items as="item" index="index" %}` renders its children once per element of the
//!   array, with `$item` and `$index` (0-based) bound for the body (the names default to
//!   `item` and `index`; a nested loop that reuses a name shadows the outer one). An
//!   undefined or `null` subject renders nothing. A subject of another kind, and `as` or
//!   `index` that are not literal non-empty strings, are an `Err` (crate behaviour: upstream
//!   never fails on a value). When the body begins and ends with a list of the same kind
//!   (`ordered`), the list one iteration ends with and the list the next begins with are one
//!   list, the first's attributes kept: a body that is one bullet makes one list.
//!
//! [`apply`] puts the schemas in a config, completing any the config already declares (with
//! a description, attributes or validation of its own); [`apply_bundle`] does so with the
//! declarations a schema bundle keeps for this layer (`layer = "control_flow"`, which
//! [`Bundle::config`] leaves out).
//!
//! [`evaluate_conditionals`] does the same, and evaluates `if`/`else` too, on the AST, for
//! callers that run AST passes before `transform`; it resolves values with
//! [`crate::next::transformer::resolve`] and decides with upstream's truthiness (anything but
//! `false`, `undefined` and `null`).
//!
//! ```
//! use markdoc::ext::control_flow;
//! use markdoc::next::schema::{Config, Variables, value::Value};
//! use markdoc::next::{index, parser::ParserArgs, renderers::html};
//!
//! let doc = index::parse(
//!     "{% for $robots as=\"robot\" %}\n- {% $robot %}\n{% /for %}\n\n\
//!      {% switch $count %}{% case 2 %}two{% /case %}{% case %}other{% /case %}{% /switch %}\n",
//!     &ParserArgs::default(),
//! )
//! .unwrap();
//! let mut config = Config::default();
//! control_flow::apply(&mut config);
//! config.variables = Some(Variables::Value(Value::from_json(&serde_json::json!({
//!     "robots": ["Bot100", "Bot250"],
//!     "count": 2
//! }))));
//! let tree = index::transform(&doc, &config).unwrap();
//! assert_eq!(
//!     html::render(&tree).unwrap(),
//!     "<article><ul><li>Bot100</li><li>Bot250</li></ul><p>two</p></article>"
//! );
//! ```

use crate::ast::{Node, PathSegment, Variable};
use crate::ext::bundle::Bundle;
use crate::next::index::merge_config;
use crate::next::schema::value::{self, Value, strict_equals};
use crate::next::schema::{Config, Schema, SchemaAttribute, TransformFn, Variables};
use crate::next::tags::conditional::truthy;
use crate::next::transformer::{ResolvedNode, flat_push, resolve, resolve_node, resolve_variable};
use crate::types::{JsObject, MarkdocError, NodeType, Result, Scalar};
use indexmap::IndexMap;
use std::sync::Arc;

/// How deeply `for` loops may nest directly in each other's bodies in
/// [`evaluate_conditionals`] (each level a recursion there); one more is an `Err`.
pub const MAX_LOOP_NESTING: usize = 200;

fn error(message: impl Into<String>) -> MarkdocError {
    MarkdocError::TransformError(message.into())
}

fn is_tag(node: &Node, name: &str) -> bool {
    node.node_type == NodeType::Tag && node.tag.as_deref() == Some(name)
}

fn attribute(name: &str, attribute: SchemaAttribute) -> (String, SchemaAttribute) {
    (
        name.to_string(),
        SchemaAttribute {
            render: Some(Value::Boolean(false)),
            ..attribute
        },
    )
}

/// The `switch` tag: `primary` (required) is the subject.
pub fn switch() -> Schema {
    let transform: TransformFn = Arc::new(transform_switch);
    Schema {
        attributes: Some(IndexMap::from([attribute(
            "primary",
            SchemaAttribute {
                required: true,
                ..SchemaAttribute::default()
            },
        )])),
        transform: Some(transform),
        description: Some("Select one of several bodies by matching a value".into()),
        ..Schema::default()
    }
}

/// The `case` tag: `primary` is the value it matches; without one it is the default. It has
/// no transform of its own: its switch transforms the children of the one that matches.
pub fn case() -> Schema {
    Schema {
        attributes: Some(IndexMap::from([attribute(
            "primary",
            SchemaAttribute::default(),
        )])),
        description: Some("One branch of a `{% switch %}`; no value makes it the default".into()),
        ..Schema::default()
    }
}

/// The `for` tag: `primary` (required) is the array, `as` and `index` the names bound.
pub fn for_loop() -> Schema {
    let transform: TransformFn = Arc::new(transform_for);
    let string = || SchemaAttribute {
        attr_type: Some(crate::next::schema::TypeSpec::Builtin(
            crate::next::schema::BuiltinType::String,
        )),
        ..SchemaAttribute::default()
    };
    Schema {
        attributes: Some(IndexMap::from([
            attribute(
                "primary",
                SchemaAttribute {
                    required: true,
                    ..SchemaAttribute::default()
                },
            ),
            attribute("as", string()),
            attribute("index", string()),
        ])),
        transform: Some(transform),
        description: Some("Repeat the body once per element of an array".into()),
        ..Schema::default()
    }
}

/// `switch`, `case` and `for`, for `config.tags`.
pub fn schemas() -> IndexMap<String, Option<Schema>> {
    IndexMap::from([
        ("switch".to_string(), Some(switch())),
        ("case".to_string(), Some(case())),
        ("for".to_string(), Some(for_loop())),
    ])
}

/// The layer's name in a schema bundle's data (`layer = "control_flow"` in `tags.toml`).
pub const LAYER: &str = "control_flow";

/// Puts the schemas `bundle` declares for this layer's tags (their descriptions, attributes
/// and validation; [`Bundle::config`] leaves them out) into `config`, and completes them with
/// [`apply`]. A tag `config` hides (`None`) stays hidden.
pub fn apply_bundle(bundle: &Bundle, config: &mut Config) {
    for (name, schema) in bundle.layer_schemas(LAYER) {
        if !matches!(config.tags.get(&name), Some(None)) {
            config.tags.insert(name, Some(schema));
        }
    }
    apply(config);
}

/// Puts [`schemas`] in `config.tags`. A schema the config already has for one of the names
/// (a schema bundle's, with its descriptions and validation) keeps what it says and gets the
/// `transform`, and the attributes it does not declare; an entry that hides the name
/// (`None`) is left alone.
pub fn apply(config: &mut Config) {
    for (name, schema) in schemas() {
        let schema = schema.expect("schemas() has only schemas");
        match config.tags.get_mut(&name) {
            Some(Some(existing)) => {
                existing.transform = schema.transform;
                let attributes = existing.attributes.get_or_insert_with(IndexMap::new);
                for (key, attribute) in schema.attributes.into_iter().flatten() {
                    attributes.entry(key).or_insert(attribute);
                }
            }
            Some(None) => {}
            None => {
                config.tags.insert(name, Some(schema));
            }
        }
    }
}

// ---------------------------------------------------------------------------------------
// At transform time
// ---------------------------------------------------------------------------------------

/// The `case` nodes of a switch: its case children, and the cases inside the non-tag nodes
/// among them (paragraphs, inlines), in document order, never inside another tag.
fn cases_of<'n, 'a>(switch: &'n ResolvedNode<'a>) -> Vec<&'n ResolvedNode<'a>> {
    let mut found = Vec::new();
    let mut stack: Vec<&ResolvedNode<'_>> = switch.children.iter().rev().collect();
    while let Some(node) = stack.pop() {
        if is_tag(node.node, "case") {
            found.push(node);
        } else if node.node.node_type != NodeType::Tag {
            stack.extend(node.children.iter().rev());
        }
    }
    found
}

fn transform_switch(node: &ResolvedNode<'_>, config: &Config) -> Result<Value> {
    // `node.attributes.primary`, read as upstream reads attributes (through their prototype).
    let subject = node.attribute("primary")?;
    let mut chosen = None;
    for case in cases_of(node) {
        // A case without a primary matches anything.
        let matched = match &case.attributes {
            Value::Object(o) if !o.contains_key("primary") && o.proto().is_object_prototype() => {
                true
            }
            _ => {
                let value = case.attribute("primary")?;
                value.is_undefined() || strict_equals(&subject, &value)
            }
        };
        if matched {
            chosen = Some(case);
            break;
        }
    }
    let mut out = Vec::new();
    for child in chosen.map(|case| &case.children[..]).unwrap_or_default() {
        flat_push(&mut out, child.transform(config)?);
    }
    Ok(Value::array(out))
}

/// What a value is, for messages.
fn kind_of(value: &Value) -> &'static str {
    match value {
        Value::Undefined => "undefined",
        Value::Null => "null",
        Value::Boolean(_) => "a boolean",
        Value::Number(_) => "a number",
        Value::String(_) => "a string",
        Value::Array(_) => "an array",
        Value::Object(_) => "an object",
        Value::Tag(_) => "a tag",
        // A built-in object or function a document reached (`$x.constructor`).
        Value::Builtin(_) => "a built-in",
    }
}

/// The name a `for` binds for `key` (`as` or `index`), `fallback` when it is not given: a
/// literal, non-empty string written in the document.
fn binding_name(node: &Node, key: &str, fallback: &str) -> Result<String> {
    match node.attributes.get(key) {
        None => Ok(fallback.to_string()),
        Some(Scalar::String(s)) if !s.is_empty() => Ok(s.clone()),
        Some(Scalar::String(_)) => Err(error(format!(
            "a `for` tag's `{key}` is empty, and a variable needs a name."
        ))),
        Some(Scalar::Variable(_) | Scalar::Function(_)) => Err(error(format!(
            "a `for` tag's `{key}` is the *name* of the variable the loop binds, so it must \
             be a literal string, not an expression, which would make the name depend on the \
             data it is naming."
        ))),
        Some(other) => Err(error(format!(
            "a `for` tag's `{key}` must be a string naming a variable, not {}.",
            kind_of(&Value::from_scalar(other))
        ))),
    }
}

/// The elements a `for` iterates: none for `undefined` and `null`, an `Err` for anything
/// but an array.
fn elements(subject: &Value) -> Result<Vec<Value>> {
    match subject {
        Value::Undefined | Value::Null => Ok(Vec::new()),
        // Every index, a hole as `undefined`.
        Value::Array(items) => Ok((0..items.len())
            .map(|i| items.get(i).cloned().unwrap_or(Value::Undefined))
            .collect()),
        other => Err(error(format!(
            "a `for` tag needs an array to iterate and was given {}. An unset variable is \
             undefined and renders nothing; this is a value of the wrong kind.",
            kind_of(other)
        ))),
    }
}

/// The config one iteration runs under: the config's variables with the two names bound.
fn scoped(config: &Config, names: &[String; 2], item: Value, index: usize) -> Config {
    let bound = [item, Value::Number(index as f64)];
    let variables = match &config.variables {
        Some(Variables::Fn(outer)) => {
            let (outer, names, bound) = (outer.clone(), names.clone(), bound.clone());
            Variables::Fn(Arc::new(move |path: &[PathSegment]| {
                let own = match path.first() {
                    // The later name wins when both are the same, as in an object.
                    Some(PathSegment::String(head)) => names.iter().rposition(|n| n == head),
                    _ => None,
                };
                let Some(at) = own else {
                    return outer(path);
                };
                let mut value = bound[at].clone();
                for step in &path[1..] {
                    if value.is_undefined() {
                        value = Value::empty_object();
                    }
                    value = value::get_property(&value, &value::property_key(step))
                        .unwrap_or(Value::Undefined);
                }
                value
            }))
        }
        other => {
            let mut object = JsObject::new();
            if let Some(Variables::Value(outer)) = other {
                object.extend(value::own_entries(outer));
            }
            for (name, value) in names.iter().zip(bound) {
                object.insert(name.clone(), value);
            }
            Variables::Value(Value::object(object))
        }
    };
    Config {
        variables: Some(variables),
        ..config.clone()
    }
}

/// Is the body one whose seams join lists: it begins and ends with lists of the same kind?
fn joins_lists(body: &[Node]) -> bool {
    match (body.first(), body.last()) {
        (Some(first), Some(last)) => {
            first.node_type == NodeType::List
                && last.node_type == NodeType::List
                && first.attributes.get("ordered") == last.attributes.get("ordered")
        }
        _ => false,
    }
}

fn transform_for(node: &ResolvedNode<'_>, config: &Config) -> Result<Value> {
    let names = [
        binding_name(node.node, "as", "item")?,
        binding_name(node.node, "index", "index")?,
    ];
    let items = elements(&node.attribute("primary")?)?;
    let body = &node.node.children;
    let joins = joins_lists(body);
    let mut out: Vec<Value> = Vec::new();
    for (index, item) in items.into_iter().enumerate() {
        let config = scoped(config, &names, item, index);
        let mut iteration = Vec::new();
        for child in body {
            let resolved = resolve_node(child, &config)?;
            flat_push(&mut iteration, resolved.transform(&config)?);
        }
        let mut iteration = iteration.into_iter();
        let Some(first) = iteration.next() else {
            continue;
        };
        match (out.last_mut(), first) {
            (Some(Value::Tag(previous)), Value::Tag(next))
                if joins && strict_equals(&previous.name, &next.name) =>
            {
                Arc::make_mut(previous)
                    .children
                    .extend(next.children.iter().cloned());
            }
            (_, first) => out.push(first),
        }
        out.extend(iteration);
    }
    Ok(Value::array(out))
}

// ---------------------------------------------------------------------------------------
// On the AST
// ---------------------------------------------------------------------------------------

/// `node` with every `if`, `switch` and `for` replaced by what it renders: the children of
/// the branch, the case, or one copy of the body per element (each copy with the loop's
/// variables replaced by their values, wherever an attribute or a text's content reads them,
/// in function calls, arrays and objects too; every other variable is left for `transform`),
/// with lists joined at the seams as the `for` tag joins them. Conditions and subjects are
/// resolved with `config` (its variables, and its functions over upstream's six) and decided
/// with upstream's truthiness. When `node` itself is one of these tags and renders to other
/// than one node, the result is a document holding them.
///
/// `Err` as the `for` tag's, and past [`MAX_LOOP_NESTING`] loops directly inside each
/// other; any other depth (no recursion).
pub fn evaluate_conditionals(node: &Node, config: &Config) -> Result<Node> {
    let config = merge_config(config);
    let mut top = splice(vec![node.clone()], &config, 0)?;
    let mut root = if top.len() == 1 {
        top.pop().expect("one node")
    } else {
        Node::new(NodeType::Document, std::iter::empty(), top, None)
    };
    let mut stack: Vec<&mut Node> = vec![&mut root];
    while let Some(node) = stack.pop() {
        let children = std::mem::take(&mut node.children);
        node.children = splice(children, &config, 0)?;
        let Node {
            children, slots, ..
        } = node;
        stack.extend(slots.values_mut());
        stack.extend(children.iter_mut());
    }
    Ok(root)
}

/// `nodes` with every `if`, `switch` and `for` among them (not inside them) replaced by what
/// it renders, until none is left among them.
fn splice(nodes: Vec<Node>, config: &Config, loops: usize) -> Result<Vec<Node>> {
    let mut out = Vec::with_capacity(nodes.len());
    let mut queue = nodes;
    queue.reverse();
    while let Some(node) = queue.pop() {
        if is_tag(&node, "if") {
            queue.extend(chosen_branch(&node, config)?.into_iter().rev());
        } else if is_tag(&node, "switch") {
            queue.extend(chosen_case(&node, config)?.into_iter().rev());
        } else if is_tag(&node, "for") {
            if loops >= MAX_LOOP_NESTING {
                return Err(error(format!(
                    "`for` loops nested more than {MAX_LOOP_NESTING} deep"
                )));
            }
            let joins = joins_lists(&node.children);
            let mut iterations = Vec::new();
            for iteration in iterations_of(&node, config)? {
                append_iteration(
                    &mut iterations,
                    splice(iteration, config, loops + 1)?,
                    joins,
                );
            }
            out.extend(iterations);
        } else {
            out.push(node);
        }
    }
    Ok(out)
}

fn primary(node: &Node, config: &Config) -> Result<Value> {
    match node.attributes.get("primary") {
        Some(value) => resolve(value, config),
        None => Ok(Value::Undefined),
    }
}

/// Upstream's `if`: the children up to the first `else` when its condition is truthy, else
/// those after the first `else` whose condition is (true without one).
fn chosen_branch(node: &Node, config: &Config) -> Result<Vec<Node>> {
    let mut taken = truthy(&primary(node, config)?);
    let mut out = Vec::new();
    for child in &node.children {
        if is_tag(child, "else") {
            if taken {
                break;
            }
            taken = match child.attributes.get("primary") {
                Some(value) => truthy(&resolve(value, config)?),
                None => true,
            };
        } else if taken {
            out.push(child.clone());
        }
    }
    Ok(out)
}

fn chosen_case(node: &Node, config: &Config) -> Result<Vec<Node>> {
    let subject = primary(node, config)?;
    let mut stack: Vec<&Node> = node.children.iter().rev().collect();
    while let Some(child) = stack.pop() {
        if is_tag(child, "case") {
            let matched = match child.attributes.get("primary") {
                None => true,
                Some(value) => strict_equals(&subject, &resolve(value, config)?),
            };
            if matched {
                return Ok(child.children.clone());
            }
        } else if child.node_type != NodeType::Tag {
            stack.extend(child.children.iter().rev());
        }
    }
    Ok(Vec::new())
}

/// A `for`'s body once per element, the loop's variables bound in each copy.
fn iterations_of(node: &Node, config: &Config) -> Result<Vec<Vec<Node>>> {
    let names = [
        binding_name(node, "as", "item")?,
        binding_name(node, "index", "index")?,
    ];
    let items = elements(&primary(node, config)?)?;
    let mut out = Vec::with_capacity(items.len());
    for (index, item) in items.into_iter().enumerate() {
        // Only the loop's own names: what else a variable reads is left for `transform`.
        let bindings = scoped(&Config::default(), &names, item, index);
        let mut body = node.children.clone();
        bind(&mut body, &names, &bindings)?;
        out.push(body);
    }
    Ok(out)
}

/// Replaces, in `body`, every variable that reads one of `names` with its value under
/// `bindings`; a nested `for` that binds a name again owns it below its own attributes.
fn bind(body: &mut [Node], names: &[String; 2], bindings: &Config) -> Result<()> {
    let mut stack: Vec<(&mut Node, [bool; 2])> =
        body.iter_mut().map(|node| (node, [true, true])).collect();
    while let Some((node, active)) = stack.pop() {
        for value in node.attributes.values_mut() {
            substitute(value, names, active, bindings)?;
        }
        let mut inner = active;
        if is_tag(node, "for") {
            let own = [
                binding_name(node, "as", "item")?,
                binding_name(node, "index", "index")?,
            ];
            for (k, name) in names.iter().enumerate() {
                if own.contains(name) {
                    inner[k] = false;
                }
            }
        }
        if inner == [false, false] {
            continue;
        }
        let Node {
            children, slots, ..
        } = node;
        stack.extend(slots.values_mut().map(|slot| (slot, inner)));
        stack.extend(children.iter_mut().map(|child| (child, inner)));
    }
    Ok(())
}

/// [`bind`] in one value (a value nests no deeper than the tag grammar's limit).
fn substitute(
    value: &mut Scalar,
    names: &[String; 2],
    active: [bool; 2],
    bindings: &Config,
) -> Result<()> {
    match value {
        Scalar::Variable(variable) if reads(variable, names, active) => {
            *value = match resolve_variable(variable, bindings)? {
                Value::Undefined => Scalar::Undefined,
                resolved => resolved.to_scalar().map_err(error)?,
            };
        }
        Scalar::Function(function) => {
            for parameter in function.parameters.values_mut() {
                substitute(parameter, names, active, bindings)?;
            }
        }
        Scalar::Array(items) => {
            for item in items {
                substitute(item, names, active, bindings)?;
            }
        }
        Scalar::Object(object) => {
            for item in object.values_mut() {
                substitute(item, names, active, bindings)?;
            }
        }
        _ => {}
    }
    Ok(())
}

fn reads(variable: &Variable, names: &[String; 2], active: [bool; 2]) -> bool {
    match variable.path.first() {
        Some(PathSegment::String(head)) => names
            .iter()
            .zip(active)
            .any(|(name, on)| on && name == head),
        _ => false,
    }
}

/// One iteration's nodes after the others: the list it begins with continues the list the
/// last one ended with, when the body joins lists and both are lists of the same kind.
fn append_iteration(out: &mut Vec<Node>, iteration: Vec<Node>, joins: bool) {
    let mut iteration = iteration.into_iter();
    let Some(mut first) = iteration.next() else {
        return;
    };
    match out.last_mut() {
        Some(previous) if joins && continues_a_list(previous, &first) => {
            previous.children.append(&mut first.children);
        }
        _ => out.push(first),
    }
    out.extend(iteration);
}

fn continues_a_list(previous: &Node, next: &Node) -> bool {
    previous.node_type == NodeType::List
        && next.node_type == NodeType::List
        && previous.attributes.get("ordered") == next.attributes.get("ordered")
}

#[cfg(test)]
mod tests;