Keep context implementations with their document models

This commit is contained in:
2026-11-30 18:56:28 +08:00
parent 455a3580d6
commit 66b6910eb1
9 changed files with 110 additions and 106 deletions
+25
View File
@@ -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()
}
}