> ## 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` にはテンプレートギャラリーのテンプレートごとに 1 つのドキュメントがあります。テンプレートのファイル（プロジェクトの zip と PDF）はここにはなく、filestore の `template/<_id>/v/<version>/` に保存されます。このコレクションは web モジュール `template-gallery`（`services/web/modules/template-gallery`）に属します。スキーマは `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 から始まり、同じ名前でテンプレートを再公開するたびに 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 のバージョンキー。スキーマはこれを無効にしていません。 | 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`）。 |

これら 3 つのルートを呼び出せるのは、サイト管理者と `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`） | テンプレートページと `GET /api/template`（`services/web/modules/template-gallery/app/src/TemplateGalleryController.mjs:210-232`）のために、`_id` または `name` でテンプレートを 1 つ検索します。`project_id` が存在しなくなったプロジェクトを指している場合、レスポンスではそれを省略します。ドキュメントは変更されません。 |
| `TemplateGalleryManager.mjs:190-236`（`getCategoryTemplates`） | `GET /api/templates`：カテゴリーの 1 ページ分。`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` のタグが付いているため、`server-ce` のマイグレーションだけを実行する 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.