> ## 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`)에 속합니다. 스키마는 `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`). |

이 세 라우트는 사이트 관리자와 `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`으로 템플릿 하나를 찾습니다. `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` 태그가 붙어 있어, `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.