markupwright 0.1.3

Template-driven workspace documentation generator
Documentation
use crate::markdown;
use std::path::Path;

#[must_use]
pub(crate) fn build(workspace: &crate::WorkspaceIndex, dir: &Path, file: &str) -> String {
    let path = dir.join(file);

    let Some(contents) = workspace.contents(&path) else {
        return String::new();
    };

    let trimmed = contents.trim_end();

    markdown::code_block("mermaid", trimmed)
}

#[cfg(test)]
mod tests {
    use googletest::prelude::*;
    use std::path::Path;

    use crate::{WorkspaceFile, WorkspaceIndex};

    fn build(file: &str, contents: Option<&str>) -> String {
        let root = Path::new("/ws");
        let files = contents.into_iter().map(|contents| WorkspaceFile {
            path: root.join(file),
            contents: Some(Box::from(contents)),
        });
        let workspace = WorkspaceIndex::new(root.to_path_buf(), files.collect());

        super::build(&workspace, root, file)
    }

    #[gtest]
    fn build_wraps_in_mermaid_block() {
        let result = build("flow.mermaid", Some("graph TD\n    A --> B\n    B --> C\n"));

        assert_eq!(
            result,
            "```mermaid\ngraph TD\n    A --> B\n    B --> C\n```"
        );
    }

    #[gtest]
    fn build_trims_trailing_whitespace() {
        let result = build("dia.mermaid", Some("graph LR\n    X --> Y  \n\n\n"));

        assert_eq!(result, "```mermaid\ngraph LR\n    X --> Y\n```");
    }

    #[gtest]
    fn build_empty_for_missing_file() -> Result<()> {
        let result = build("nonexistent.mermaid", None);

        verify_eq!(result, "")
    }

    #[gtest]
    fn build_empty_content_file() -> Result<()> {
        let result = build("empty.mermaid", Some(""));

        verify_eq!(result, "```mermaid\n\n```")
    }
}