Index and summarize a directory into an index.md file. Works for both document folders and code directories. Use when the user wants to create an overview of files, says "indekser", "indeksere", "index this folder", "lag en oversikt", or wants a summary index of a folder.
Create and maintain an index.md that summarizes all files in a directory. Works for two modes:
The mode is inferred automatically, or the user can specify it.
If the user did not specify a directory, ask.
Detect mode by scanning the file extensions in the target directory:
.md, .pdf, .docx, .txt, .pptx → Document mode.ts, .tsx, .js, .jsx, .py, .go, .rs, , etc. → .swiftindex.md exists → enter Re-index mode (Step 8)Use Glob to find all files in the target directory (recursive). Exclude:
index.md itself.DS_Store, .gitkeep, hidden files (starting with .)node_modules/, __pycache__/, .git/dist/, build/, .next/, *.pycEvery file in every subdirectory gets its own entry. Do NOT collapse a subdirectory into a single line. Group files under their folder heading:
## Root files
## components/
## utils/
Use stat to get created and modified dates for each file:
stat -f "%Sm %SB" -t "%Y-%m-%d %H:%M" "filepath"
In document mode, sort by last modified date, newest first within each group.
In code mode, sort alphabetically (dates are less important for code).
Write this immediately so the user sees progress.
# Index: [directory name]
> Auto-generated content index. Last indexed: [YYYY-MM-DD]
> Root path: `[absolute path]`
## Key Facts
| | |
|---|---|
| **Total files** | [N] |
| **Oldest file** | [YYYY-MM-DD] ([filename]) |
| **Newest file** | [YYYY-MM-DD] ([filename]) |
| **Date span** | [X days/weeks/months] |
| **Last indexed** | [YYYY-MM-DD] |
> **Note on relevance:** In active planning and documentation work, files can be superseded within hours — not weeks. When multiple files appear to cover the same topic, the newest one is *likely* the most current truth, but this is not certain. Always check the "Modified" date relative to related files. The **Status** field in each description indicates the AI's best assessment: `Current`, `Superseded`, `Draft`, or `Historical` — but treat these as suggestions, not facts. When in doubt, ask the user.
## Overview
_Being generated..._
## Table of Contents
| # | File | Description | Modified | Created |
|---|------|-------------|----------|---------|
| 1 | [filename](relative-path) | _To be indexed..._ | YYYY-MM-DD | YYYY-MM-DD |
# Index: [directory name]
> Auto-generated code index. Last indexed: [YYYY-MM-DD]
> Root path: `[absolute path]`
## Key Facts
| | |
|---|---|
| **Total files** | [N] |
| **Languages** | [e.g. TypeScript, Python] |
| **Last indexed** | [YYYY-MM-DD] |
## Overview
_Being generated..._
## Table of Contents
| # | File | Purpose | Exports / Key symbols |
|---|------|---------|-----------------------|
| 1 | [filename](relative-path) | _To be indexed..._ | — |
For each file:
index.md after each file — write to disk immediately, do not batch---
### N. [filename](relative-path)
**Modified**: YYYY-MM-DD | **Created**: YYYY-MM-DD
[2–5 sentences: purpose, key decisions, conclusions]
**Key points:**
- ...
**Related to:** [other files if applicable]
**Status:** current / superseded by X / draft / historical
---
### N. [filename](relative-path)
[1–3 sentences: what this module does, its role in the codebase]
**Exports:** `functionA`, `ClassB`, `TYPE_C`
**Dependencies:** imports from `../utils`, `react`, etc.
**Notes:** [anything notable — side effects, global state, gotchas]
Keep code descriptions technical and precise. Focus on what the module does, not how it works internally.
After all files are indexed:
For document mode: if multiple files appear to cover the same topic, flag this explicitly in the Overview and in each affected file's Related to field. Note which file is newest, but do not assert it is correct — suggest the user verify.
When index.md already exists:
index.mdOnly re-read files that are new or changed.
| Type | Handling |
|---|---|
.md | Read directly, summarize |
.pdf | Use Read tool (supports PDF), summarize |
.txt, .csv, .json, .yaml | Read and summarize |
.docx | Use docx skill if available, else note as "Binary — manual review needed" |
.pptx | Use pptx skill if available, else note as "Presentation — manual review needed" |
.png, .jpg, .svg | Note as image, describe by filename context |
Source code (.ts, .py, etc.) | Read and summarize exports and purpose |
| Binary/unknown | Note file exists, type, size — don't attempt to read |
The final index.md serves two audiences:
Humans — Someone unfamiliar with the directory should be able to read the index and understand what each file does, which ones are most relevant, and — in document mode — which ones may be outdated.
AI agents — This is equally important. An AI agent searching for information will typically list a directory, see index.md, read it, and use it to decide which files to open — without reading the entire codebase or document collection. The index must therefore be optimized for this use case:
Think of index.md as a routing layer: it should allow an AI agent to confidently say "I need files A and C, I can skip B and D" — without opening any of them first.