> ## Documentation Index
> Fetch the complete documentation index at: https://ayakaleaf-pro.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Templates 集合

`templates` 中模板库里的每个模板一个文档。模板的文件（项目 zip 和 PDF）不在这里：它们保存在 filestore 的 `template/<_id>/v/<version>/` 下。该集合属于 web 模块 `template-gallery`（`services/web/modules/template-gallery`）。schema 是 `services/web/modules/template-gallery/app/src/models/Template.mjs` 中的 Mongoose 模型 `Template`；逻辑位于 `services/web/modules/template-gallery/app/src/TemplateGalleryManager.mjs` 以及同目录下的 `TemplateGalleryHelper.mjs`。

#### 字段

| 字段 | 类型 | 含义 | 来源 |
| - | - | - | - |
| `_id` | ObjectId | 由 Mongoose 设置。它也是 filestore 中模板文件所在的文件夹。 | `TemplateGalleryHelper.mjs:148-150` |
| `name` | String | 标题。必填，最多 150 个字符。发布时按名称查找模板，编辑时会拒绝其他模板已使用的名称，因此即使没有索引，名称也保持唯一。 | `Template.mjs:7`, `TemplateGalleryHelper.mjs:50-52`, `TemplateGalleryManager.mjs:30-38,55` |
| `category` | String | 分类链接 `/templates/<key>`，其中 `<key>` 是 `OVERLEAF_TEMPLATE_CATEGORIES` 中的一个值或 `all`。必填。 | `Template.mjs:8`, `services/web/modules/template-gallery/index.mjs:17-31`, `TemplateGalleryManager.mjs:206` |
| `descriptionMD` | String | 输入的原始描述，Markdown 格式。最多 4096 个字符。模板库会搜索它。 | `Template.mjs:10`, `TemplateGalleryHelper.mjs:53-55`, `TemplateGalleryManager.mjs:207-212` |
| `description` | String | 由 `descriptionMD` 渲染并清理后的 HTML。每次设置 `descriptionMD` 时都会设置。 | `Template.mjs:9`, `TemplateGalleryHelper.mjs:224-228` |
| `authorMD` | String | 输入的原始作者，Markdown 格式。最多 512 个字符。模板库会搜索它。 | `Template.mjs:12`, `TemplateGalleryHelper.mjs:56-58` |
| `author` | String | 由 `authorMD` 渲染的 HTML，只保留链接。每次设置 `authorMD` 时都会设置。 | `Template.mjs:11`, `TemplateGalleryHelper.mjs:229-232` |
| `license` | String | 在表单中选择的许可证文本。必填，最多 512 个字符。 | `Template.mjs:13`, `TemplateGalleryHelper.mjs:59-61` |
| `mainFile` | String | 源项目根 doc 的路径，不带开头的斜杠。必填：没有根 doc 的项目无法发布。 | `Template.mjs:14`, `TemplateGalleryHelper.mjs:112-114` |
| `compiler` | String | 源项目的编译器。必填。 | `Template.mjs:15`, `TemplateGalleryHelper.mjs:122` |
| `imageName` | String | 源项目的 TeX Live 镜像。模板页面只显示其最后一部分。 | `Template.mjs:16`, `TemplateGalleryHelper.mjs:123`, `TemplateGalleryManager.mjs:249-253` |
| `language` | String | 源项目的拼写检查语言。可在编辑表单中修改。 | `Template.mjs:17`, `TemplateGalleryHelper.mjs:124` |
| `version` | Number | 从 1 开始，每次以相同名称重新发布模板时加一。每个版本的文件位于 `v/<version>/` 下。必填。 | `Template.mjs:18`, `TemplateGalleryManager.mjs:74-79` |
| `owner` | ObjectId | 首次发布该模板的用户。覆盖重新发布时保留原所有者。 | `Template.mjs:19`, `TemplateGalleryManager.mjs:70-79` |
| `project_id` | ObjectId | 最近一次发布所用的项目。每次发布时更新。 | `Template.mjs:20`, `TemplateGalleryManager.mjs:81-84` |
| `lastUpdated` | Date | 最近一次发布或编辑的时间。必填，默认为当前时间。 | `Template.mjs:21-27`, `TemplateGalleryHelper.mjs:125`, `TemplateGalleryManager.mjs:41` |
| `__v` | Number | Mongoose 的版本键。schema 没有关闭它。 | Mongoose 默认 |

#### 写入方

| 代码 | 作用 |
| - | - |
| `TemplateGalleryManager.mjs:53-97`（`createTemplateFromProject`） | `POST /template/new/:Project_id`。复制项目的设置（`TemplateGalleryHelper.mjs:94-134`），把 zip 和 PDF 上传到 filestore 并保存。如果名称已被占用，就返回冲突，除非客户端要求覆盖：此时更新该模板并增加其 `version`，然后删除上一个版本的文件。 |
| `TemplateGalleryManager.mjs:21-47`（`editTemplate`） | `POST /template/:template_id/edit`。把提交的字段赋给文档，重新渲染 `description` 和 `author`，并设置 `lastUpdated`。编辑表单会提交 `name`、`license`、`category`、`language`、`authorMD` 和 `descriptionMD` 中被修改的字段（`services/web/modules/template-gallery/frontend/js/pages/template/util/api.ts:22-29`）。 |
| `TemplateGalleryHelper.mjs:186-222`（`deleteTemplateAssets`） | `DELETE /template/:template_id/delete` 删除文档以及指定版本的文件（`TemplateGalleryManager.mjs:49-51`）。 |

只有站点管理员和 `OVERLEAF_TEMPLATES_USER_ID` 中的用户可以调用这三个路由（`services/web/modules/template-gallery/app/src/Router.mjs:55-62,70-84`, `PermissionsMiddleware.mjs:8-19`）。

#### 读取方

| 代码 | 作用 |
| - | - |
| `TemplateGalleryManager.mjs:158-183`（`getTemplate`） | 按 `_id` 或 `name` 查找一个模板，用于模板页面和 `GET /api/template`（`services/web/modules/template-gallery/app/src/TemplateGalleryController.mjs:210-232`）。如果 `project_id` 指向已不存在的项目，响应中会省略它；文档本身不会被修改。 |
| `TemplateGalleryManager.mjs:190-236`（`getCategoryTemplates`） | `GET /api/templates`：一个分类的一页结果，在 `name`、`authorMD` 和 `descriptionMD` 中搜索，按 `lastUpdated` 或 `name`（不区分大小写）排序。 |
| `TemplateGalleryHelper.mjs:64-80`（`canUserOverrideTemplate`） | 读取 `owner`，以便在冲突消息中显示所有者。 |

#### 索引

| 名称 | 键 | 选项 | 迁移 |
| - | - | - | - |
| `_id_` | `_id: 1` | | 默认 |

Mongoose 的 `autoIndex` 已关闭（`services/web/app/src/infrastructure/Mongoose.mjs:7`）。`tools/migrations/20190912145030_create_templates_indexes.mjs`（唯一的 `project_id`、`user_id`、`name`）标记为 `server-pro` 和 `saas`，因此不会在 CE 中运行，CE 只运行 `server-ce` 迁移（`server-ce/init_scripts/900_run_web_migrations.sh`）。

#### 说明

* 上游把 `templates` 视为过时集合，并在自己的站点上删除它（`tools/migrations/20241002180623_drop_unused_collections.mjs`，标记为 `saas`）。在 Ayakaleaf Pro 中，模板库模块会使用它。
* 按名称排序使用不区分大小写的排序规则，`TemplateGalleryManager.mjs:186-188` 处的注释指出 `name` 索引也需要相同的排序规则。CE 没有 `name` 索引，因此 Mongo 在内存中排序。
* 删除源项目或所有者不会删除模板。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.