use comrak::nodes::{AstNode, NodeCodeBlock, NodeValue};
use super::context::Context;
use super::inline;
use super::layout;
use crate::document::{Block, Line, Span, Style};
use crate::text;
pub(super) const MERMAID: &str = "mermaid";
pub(super) fn html(literal: &str, context: &Context) -> Vec<Line> {
let muted = Style::fg(context.palette().muted);
literal.trim_end_matches('\n').lines().flat_map(|line| text::wrap_literal(&[Span::new(line, muted)], context.width(), &[])).collect()
}
pub(super) fn flat<'a>(node: &'a AstNode<'a>, context: &Context) -> Vec<Block> {
super::wrapped(&[Span::new(inline::text(node), context.style)], context)
}
pub(super) fn code<'a>(node: &'a AstNode<'a>, block: &NodeCodeBlock, context: &Context) -> Vec<Block> {
let language = language(block);
match language.as_deref() {
Some(MERMAID) => vec![crate::mermaid::render_in_document(&block.literal, &context.settings_at_width(), context.diagrams)],
language => {
let key = start_line(node);
layout::lines(crate::code::render_in_document(&block.literal, language, key, context.highlights, &context.settings_at_width()))
}
}
}
fn start_line<'a>(node: &'a AstNode<'a>) -> usize {
node.data().sourcepos.start.line
}
pub(super) fn highlighted<'a>(root: &'a AstNode<'a>) -> impl Iterator<Item = (usize, crate::code::Highlighting)> + 'a {
root.descendants().filter_map(|node| match &node.data().value {
NodeValue::CodeBlock(block) => match language(block).as_deref() {
Some(MERMAID) => None,
language => Some((start_line(node), crate::code::highlighting(&block.literal, language))),
},
_ => None,
})
}
pub(super) fn diagrams<'a>(root: &'a AstNode<'a>) -> impl Iterator<Item = String> + 'a {
root.descendants().filter_map(|node| match &node.data().value {
NodeValue::CodeBlock(block) if language(block).as_deref() == Some(MERMAID) => Some(block.literal.clone()),
_ => None,
})
}
pub(super) fn language(block: &NodeCodeBlock) -> Option<String> {
let words = block.info.split(|character: char| character.is_whitespace() || character == ',');
words.map(|word| word.trim_matches(['{', '}', '.'])).find(|word| !word.is_empty()).map(str::to_lowercase)
}
#[cfg(test)]
mod tests {
use comrak::nodes::NodeCodeBlock;
use comrak::{Arena, parse_document};
use crate::code::{Highlighting, highlighting};
use crate::markdown::plain_at;
fn language(info: &str) -> Option<String> {
super::language(&NodeCodeBlock { info: info.to_owned(), ..NodeCodeBlock::default() })
}
#[test]
fn every_code_block_but_mermaid_counts_toward_highlighting_by_the_line_it_starts_on() {
let source = "```rust\na\n```\n\n- item\n\n ```js\n bc\n ```\n\n```mermaid\ngraph TD\n```\n\n indented\n";
let arena = Arena::new();
let root = parse_document(&arena, source, &crate::markdown::options());
let expected = [(1, highlighting("a\n", Some("rust"))), (7, highlighting("bc\n", Some("js"))), (15, Highlighting::default())];
assert_eq!(super::highlighted(root).collect::<Vec<_>>(), expected);
}
#[test]
fn the_language_is_the_first_word_of_the_info_string_lowercased() {
assert_eq!(language("rust,ignore").as_deref(), Some("rust"));
assert_eq!(language(" ts title=\"a.ts\"").as_deref(), Some("ts"));
assert_eq!(language("{.python}").as_deref(), Some("python"));
assert_eq!(language("Mermaid").as_deref(), Some("mermaid"));
assert_eq!(language(" "), None);
}
#[test]
fn an_html_block_keeps_its_spacing_and_cuts_where_a_line_is_full() {
assert_eq!(plain_at("<pre>a b</pre>\n", 40), "<pre>a b</pre>\n");
assert_eq!(plain_at("<pre>a bcdefgh</pre>\n", 10), "<pre>a\nbcdefgh</p\nre>\n");
}
#[test]
fn the_code_panel_is_labelled_with_the_language_only() {
assert_eq!(plain_at("```Rust,ignore\nx\n```", 20).lines().next().map(str::trim), Some("rust"));
}
}