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;
4use crate::models::options::ExtractToc;
5
6impl Plugin for ExtractToc {
7    fn attach(&self, context: &mut Attach) -> Result<(), Error> {
8        context.add_transform(Box::new(self.clone()));
9        Ok(())
10    }
11}
12
13impl Transform for ExtractToc {
14    fn input(&self) -> TreeKind {
15        TreeKind::Hast
16    }
17
18    fn output(&self) -> TreeKind {
19        TreeKind::Hast
20    }
21
22    fn run<'a>(
23        &self,
24        tree: Tree<'a>,
25        file: &mut File,
26        _alloc: &'a Allocator,
27    ) -> Result<Tree<'a>, Error> {
28        match tree {
29            Tree::Hast(node) => {
30                file.data
31                    .insert("toc".into(), extract_toc(&node, &self.skip));
32                Ok(Tree::Hast(node))
33            }
34            _ => Err(Error::new("invalid-stage", "`html.extractToc` takes hast")),
35        }
36    }
37}