Skip to main content

nargo_types/document/
mod.rs

1//! 文档模块
2//! 定义文档的元信息结构
3
4use crate::{NargoValue, Span};
5use serde::{Deserialize, Serialize};
6use std::collections::HashMap;
7
8/// 文档
9#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
10pub struct Document {
11    /// 文档元信息
12    pub meta: DocumentMeta,
13    /// 前置元数据(frontmatter)
14    pub frontmatter: FrontMatter,
15    /// 文档内容(原始文本)
16    pub content: String,
17    /// 解析后的内容(可以是 HTML 或结构化数据)
18    pub rendered_content: Option<String>,
19    /// 位置信息
20    pub span: Span,
21}
22
23impl Document {
24    /// 创建新的文档
25    pub fn new() -> Self {
26        Self::default()
27    }
28
29    /// 设置文档路径
30    pub fn with_path(mut self, path: String) -> Self {
31        self.meta.path = path;
32        self
33    }
34
35    /// 设置前置元数据
36    pub fn with_frontmatter(mut self, frontmatter: FrontMatter) -> Self {
37        self.frontmatter = frontmatter;
38        self
39    }
40
41    /// 设置文档内容
42    pub fn with_content(mut self, content: String) -> Self {
43        self.content = content;
44        self
45    }
46
47    /// 获取文档标题
48    pub fn title(&self) -> Option<&str> {
49        self.frontmatter.title.as_deref().or_else(|| self.meta.title.as_deref())
50    }
51
52    /// 获取文档描述
53    pub fn description(&self) -> Option<&str> {
54        self.frontmatter.description.as_deref()
55    }
56
57    /// 获取文档标签
58    pub fn tags(&self) -> &[String] {
59        &self.frontmatter.tags
60    }
61
62    /// 序列化为 JSON
63    pub fn to_json(&self) -> serde_json::Result<String> {
64        serde_json::to_string(self)
65    }
66
67    /// 序列化为美化的 JSON
68    pub fn to_json_pretty(&self) -> serde_json::Result<String> {
69        serde_json::to_string_pretty(self)
70    }
71}
72
73/// 文档元信息
74#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
75pub struct DocumentMeta {
76    /// 文档路径
77    pub path: String,
78    /// 文档标题
79    pub title: Option<String>,
80    /// 文档语言
81    pub lang: Option<String>,
82    /// 最后更新时间
83    pub last_updated: Option<i64>,
84    /// 其他元数据
85    pub extra: HashMap<String, NargoValue>,
86}
87
88/// 前置元数据(Front Matter)
89#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
90pub struct FrontMatter {
91    /// 页面标题
92    pub title: Option<String>,
93    /// 页面描述
94    pub description: Option<String>,
95    /// 页面布局
96    pub layout: Option<String>,
97    /// 页面标签
98    pub tags: Vec<String>,
99    /// 是否在侧边栏中隐藏
100    pub sidebar: Option<bool>,
101    /// 侧边栏顺序
102    pub sidebar_order: Option<i32>,
103    /// 自定义元数据
104    pub custom: HashMap<String, NargoValue>,
105}
106
107impl FrontMatter {
108    /// 创建新的前置元数据
109    pub fn new() -> Self {
110        Self::default()
111    }
112
113    /// 设置标题
114    pub fn with_title(mut self, title: String) -> Self {
115        self.title = Some(title);
116        self
117    }
118
119    /// 设置描述
120    pub fn with_description(mut self, description: String) -> Self {
121        self.description = Some(description);
122        self
123    }
124
125    /// 设置布局
126    pub fn with_layout(mut self, layout: String) -> Self {
127        self.layout = Some(layout);
128        self
129    }
130
131    /// 添加标签
132    pub fn add_tag(mut self, tag: String) -> Self {
133        self.tags.push(tag);
134        self
135    }
136}