Skip to main content

textlane_html/plugins/extract_toc/
plugin.rs

1use textlane_core::{Allocator, Attach, Error, File, Plugin, Transform, Tree, TreeKind};
2
3use crate::actions::hast::toc::extract_toc;
4
5/// `html.extractToc`: the headings, nested by rank, as the file's `data.toc`, as
6/// @stefanprobst/rehype-extract-toc stores them.
7#[derive(Clone, Debug, Default)]
8pub struct ExtractToc;
9
10impl Plugin for ExtractToc {
11    fn attach(&self, context: &mut Attach) -> Result<(), Error> {
12        context.add_transform(Box::new(ExtractToc));
13        Ok(())
14    }
15}
16
17impl Transform for ExtractToc {
18    fn input(&self) -> TreeKind {
19        TreeKind::Hast
20    }
21
22    fn output(&self) -> TreeKind {
23        TreeKind::Hast
24    }
25
26    fn run<'a>(
27        &self,
28        tree: Tree<'a>,
29        file: &mut File,
30        _alloc: &'a Allocator,
31    ) -> Result<Tree<'a>, Error> {
32        match tree {
33            Tree::Hast(node) => {
34                file.data.insert("toc".into(), extract_toc(&node));
35                Ok(Tree::Hast(node))
36            }
37            _ => Err(Error::new("invalid-stage", "`html.extractToc` takes hast")),
38        }
39    }
40}