Skip to main content

textlane_html/plugins/slug/
plugin.rs

1use textlane_core::{Allocator, Attach, Error, File, Plugin, Transform, Tree, TreeKind};
2
3use crate::actions::hast::autolink::link_headings;
4use crate::actions::hast::slug::slug_hast;
5use crate::models::options::{Autolink, AutolinkOptions, Slug};
6
7impl Plugin for Slug {
8    fn attach(&self, context: &mut Attach) -> Result<(), Error> {
9        context.add_transform(Box::new(self.clone()));
10        Ok(())
11    }
12}
13
14impl Transform for Slug {
15    fn input(&self) -> TreeKind {
16        TreeKind::Hast
17    }
18
19    fn output(&self) -> TreeKind {
20        TreeKind::Hast
21    }
22
23    fn run<'a>(
24        &self,
25        tree: Tree<'a>,
26        _file: &mut File,
27        alloc: &'a Allocator,
28    ) -> Result<Tree<'a>, Error> {
29        match tree {
30            Tree::Hast(mut node) => {
31                slug_hast(&mut node, self, alloc);
32                match &self.autolink {
33                    Some(Autolink::Enabled(true)) => {
34                        link_headings(&mut node, &AutolinkOptions::default(), alloc)?;
35                    }
36                    Some(Autolink::Options(options)) => {
37                        link_headings(&mut node, options, alloc)?
38                    }
39                    Some(Autolink::Enabled(false)) | None => {}
40                }
41                Ok(Tree::Hast(node))
42            }
43            _ => Err(Error::new("invalid-stage", "`html.slug` takes hast")),
44        }
45    }
46}