#![doc = include_str!("../README.md")]
#![deny(missing_docs)]
mod layout;
mod mermaid;
mod text;
mod widget;
pub use layout::{
CellRange, CodePolicy, DocumentRow, HeadingPosition, Layout, LayoutError, LayoutOptions,
LineLimit, LineLimitError, LinkPosition, TablePolicy, Theme,
};
pub use markdown_model::{Document, HeadingId, LinkId, ParseError};
use std::sync::Arc;
pub use text::sanitize_terminal_text;
pub use widget::{MarkdownWidget, ViewState};
#[derive(Debug)]
pub struct MarkdownView {
document: Arc<Document>,
options: LayoutOptions,
cached: Option<Layout>,
}
impl MarkdownView {
pub fn new(source: &str) -> Result<Self, ParseError> {
Ok(Self::from_document(Arc::new(Document::parse(source)?)))
}
pub fn from_document(document: Arc<Document>) -> Self {
Self {
document,
options: LayoutOptions::default(),
cached: None,
}
}
pub fn document(&self) -> &Document {
&self.document
}
pub const fn shared_document(&self) -> &Arc<Document> {
&self.document
}
pub const fn options(&self) -> &LayoutOptions {
&self.options
}
pub fn set_document(&mut self, document: Arc<Document>) {
self.document = document;
self.cached = None;
}
pub fn set_options(&mut self, options: LayoutOptions) {
if self.options != options {
self.options = options;
self.cached = None;
}
}
pub fn prepare(&mut self, width: u16) -> Result<&Layout, LayoutError> {
if self
.cached
.as_ref()
.is_none_or(|layout| layout.width() != width)
{
self.cached = Some(Layout::new(&self.document, width, &self.options)?);
}
Ok(self.cached.as_ref().expect("layout was prepared above"))
}
}