Keep context implementations with their document models
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
use super::contextualize::Contextualize;
|
||||
|
||||
use super::{
|
||||
id::{IdError, validate_id},
|
||||
rich_text::RichText,
|
||||
@@ -70,3 +72,26 @@ pub enum DocumentContent {
|
||||
/// Preserves documents written with the original arbitrary JSON model.
|
||||
Json(Value),
|
||||
}
|
||||
|
||||
impl Contextualize for DocumentContent {
|
||||
fn render_context(&self) -> String {
|
||||
match self {
|
||||
Self::Page { title } => format!("# {title}"),
|
||||
Self::Heading(text) => format!("## {}", text.render_context()),
|
||||
Self::Paragraph(text) => text.render_context(),
|
||||
Self::ListItem(text) => format!("- {}", text.render_context()),
|
||||
Self::Todo { text, checked } => {
|
||||
let mark = if *checked { 'x' } else { ' ' };
|
||||
format!("- [{mark}] {}", text.render_context())
|
||||
}
|
||||
Self::Code { language, source } => format!("Code ({language}):\n{source}"),
|
||||
Self::Json(value) => value.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Contextualize for Document {
|
||||
fn render_context(&self) -> String {
|
||||
self.content.render_context()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user