use crate::{NargoValue, Span};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct Document {
pub meta: DocumentMeta,
pub frontmatter: FrontMatter,
pub content: String,
pub rendered_content: Option<String>,
pub span: Span,
}
impl Document {
pub fn new() -> Self {
Self::default()
}
pub fn with_path(mut self, path: String) -> Self {
self.meta.path = path;
self
}
pub fn with_frontmatter(mut self, frontmatter: FrontMatter) -> Self {
self.frontmatter = frontmatter;
self
}
pub fn with_content(mut self, content: String) -> Self {
self.content = content;
self
}
pub fn title(&self) -> Option<&str> {
self.frontmatter.title.as_deref().or_else(|| self.meta.title.as_deref())
}
pub fn description(&self) -> Option<&str> {
self.frontmatter.description.as_deref()
}
pub fn tags(&self) -> &[String] {
&self.frontmatter.tags
}
pub fn to_json(&self) -> serde_json::Result<String> {
serde_json::to_string(self)
}
pub fn to_json_pretty(&self) -> serde_json::Result<String> {
serde_json::to_string_pretty(self)
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct DocumentMeta {
pub path: String,
pub title: Option<String>,
pub lang: Option<String>,
pub last_updated: Option<i64>,
pub extra: HashMap<String, NargoValue>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct FrontMatter {
pub title: Option<String>,
pub description: Option<String>,
pub layout: Option<String>,
pub tags: Vec<String>,
pub sidebar: Option<bool>,
pub sidebar_order: Option<i32>,
pub custom: HashMap<String, NargoValue>,
}
impl FrontMatter {
pub fn new() -> Self {
Self::default()
}
pub fn with_title(mut self, title: String) -> Self {
self.title = Some(title);
self
}
pub fn with_description(mut self, description: String) -> Self {
self.description = Some(description);
self
}
pub fn with_layout(mut self, layout: String) -> Self {
self.layout = Some(layout);
self
}
pub fn add_tag(mut self, tag: String) -> Self {
self.tags.push(tag);
self
}
}