notedown_ast 0.14.9

Notedown Abstract Syntax Tree
Documentation
use super::*;

impl TocNode {
    fn last_at_level(&mut self, depth: u8) -> &mut TocNode {
        if depth == 0 || self.children.is_empty() { self } else { self.children.last_mut().unwrap().last_at_level(depth - 1) }
    }
}

impl TableOfContent for ASTNode {
    fn toc_configurable(&self, config: &TocConfig) -> TocNode {
        let mut root = TocNode::default();
        let mut toc_ignore = false;
        if let ASTKind::Statements(terms) = &self.value {
            for term in terms {
                match &term.value {
                    ASTKind::Header(header) => {
                        let level = header.level;
                        if toc_ignore {
                            toc_ignore = false;
                            continue;
                        }
                        if level > config.max_depth {
                            continue;
                        }
                        let parent = root.last_at_level(level - 1);
                        let new =
                            TocNode { level, detail: header.slugify(), range: self.range.to_owned().unwrap_or_default(), children: vec![] };
                        parent.children.push(new);
                    }
                    ASTKind::Command(cmd) => {
                        if cmd.is("toc_ignore") {
                            toc_ignore = true
                        }
                    }
                    _ => (),
                }
            }
        }
        return root;
    }
}

impl From<TocNode> for DocumentSymbol {
    #[allow(deprecated)]
    fn from(node: TocNode) -> Self {
        DocumentSymbol {
            name: "".to_string(),
            detail: Some(node.detail),
            kind: SymbolKind::NAMESPACE,
            tags: None,
            deprecated: None,
            range: Default::default(),
            selection_range: Default::default(),
            children: None,
        }
    }
}