Update Markdown file index
Scan a target Markdown document and a source folder, derive descriptions from discovered files, then replace or add a consistent file index with valid relative links and stable ordering.
When to invoke
- "Update this README with a table of files in
docs/."
- "Refresh the markdown file index section."
- "Create a list of scripts from this folder."
- "Add a contents table for these Markdown files."
- "Regenerate the directory index without changing the rest of the document."
Inputs
Use $ARGUMENTS or the user request to identify the target Markdown file, source folder, and optional glob pattern. If a pattern is absent, infer a safe default from the request such as *.md for documentation indexes or all regular files for generic folder indexes.
Placeholder mapping
Legacy prompt placeholders map directly to runtime inputs: ${file} is the target Markdown file, ${input:folder} is the scanned folder, and ${input:pattern} is the optional glob. The result may be an index/table, table/index, categorized sub-tables, or another best-fit structure using file type/category and other file-related metadata.
Procedure
- Read the target Markdown file to understand heading levels, existing sections, comments, and style.
- List files in the specified folder matching the requested pattern.
- Extract file metadata: name, type, description, optional size, and optional modified date.
- Detect an existing index section by heading, table columns, file-link list, or HTML comment marker.
- Choose the smallest format that matches the document: simple list, detailed table, or categorized sections.
- Replace the existing section or insert a new section at the appropriate heading level.
- Validate Markdown syntax, links, escaping, sort order, and preservation of surrounding content.
Section identification
| Signal |
Match examples |
Action |
| Heading text |
"index", "files", "contents", "directory", "list" |
Treat the heading subtree as the candidate replacement. |
| File table |
Columns such as File, Type, Description, Size, Modified |
Preserve column intent when regenerating rows. |
| File-link list |
Bullets containing [filename.ext](path/to/filename.ext) |
Replace only the contiguous file-list block. |
| HTML marker |
Comments such as <!-- file index --> or generated-section markers |
Prefer marker bounds over heading heuristics. |
File analysis
| Field |
Extraction rule |
Fallback |
| Name |
Use the filename with extension unless the surrounding document omits extensions. |
Preserve the existing convention. |
| Type |
Use extension and category, for example .md, .js, .py, or Extension. |
file when no extension exists. |
| Description |
First Markdown heading, first meaningful comment, shebang purpose, package metadata, or inferred filename purpose. |
Short inferred description in sentence case. |
| Size |
Include only if the existing index includes size or the user asks. |
Omit. |
| Modified |
Include only if the existing index includes dates or the user asks. |
Omit. |
Table structure options
Simple list
## Files in <folder>
- [filename.ext](path/to/filename.ext) - Description
- [filename2.ext](path/to/filename2.ext) - Description
Detailed table
| File | Type | Description |
|------|------|-------------|
| [filename.ext](path/to/filename.ext) | Extension | Description |
| [filename2.ext](path/to/filename2.ext) | Extension | Description |
Categorized sections
Group files by type, feature area, or folder when a flat table is too noisy. Keep each category alphabetized.
Update rules
- Preserve existing Markdown structure, heading levels, frontmatter, comments, prose, and document flow outside the index block.
- Use repository-relative or target-file-relative links consistently with the surrounding document.
- Sort files alphabetically by default; use natural order when filenames contain numbered prefixes.
- Escape
| in table cells and handle spaces, parentheses, and other special characters in filenames.
- Do not include directories unless the user asks for directory entries.
- Do not invent descriptions that claim behavior not visible from file names or contents.
Output template
## Markdown file index result
**Status:** updated | created | blocked
**Target file:** `<file>`
**Source folder:** `<folder>`
**Pattern:** `<pattern>`
**Format:** simple list | detailed table | categorized sections
### Files indexed
| File | Type | Description |
| --- | --- | --- |
| `<relative/path>` | `<extension or category>` | <description> |
### Validation
- Existing section found: yes | no
- Relative links valid: pass | fail
- Markdown table/list syntax valid: pass | fail
Quality gate
1---2name: update-markdown-file-index-23description: Update a Markdown file with an index, list, or table of files from a specified folder, preserving existing document structure and relative links. Use when the user asks to index a folder in Markdown, refresh a file table, list docs or scripts in a README, or maintain a contents section from filesystem files.4---56# Update Markdown file index78Scan a target Markdown document and a source folder, derive descriptions from discovered files, then replace or add a consistent file index with valid relative links and stable ordering.910## When to invoke1112- "Update this README with a table of files in `docs/`."13- "Refresh the markdown file index section."14- "Create a list of scripts from this folder."15- "Add a contents table for these Markdown files."16- "Regenerate the directory index without changing the rest of the document."1718## Inputs1920Use `$ARGUMENTS` or the user request to identify the target Markdown file, source folder, and optional glob pattern. If a pattern is absent, infer a safe default from the request such as `*.md` for documentation indexes or all regular files for generic folder indexes.2122## Placeholder mapping2324Legacy prompt placeholders map directly to runtime inputs: `${file}` is the target Markdown file, `${input:folder}` is the scanned folder, and `${input:pattern}` is the optional glob. The result may be an `index/table`, `table/index`, categorized `sub-tables`, or another `best-fit` structure using file `type/category` and other `file-related` metadata.25## Procedure26271. Read the target Markdown file to understand heading levels, existing sections, comments, and style.282. List files in the specified folder matching the requested pattern.293. Extract file metadata: name, type, description, optional size, and optional modified date.304. Detect an existing index section by heading, table columns, file-link list, or HTML comment marker.315. Choose the smallest format that matches the document: simple list, detailed table, or categorized sections.326. Replace the existing section or insert a new section at the appropriate heading level.337. Validate Markdown syntax, links, escaping, sort order, and preservation of surrounding content.3435## Section identification3637| Signal | Match examples | Action |38| --- | --- | --- |39| Heading text | "index", "files", "contents", "directory", "list" | Treat the heading subtree as the candidate replacement. |40| File table | Columns such as `File`, `Type`, `Description`, `Size`, `Modified` | Preserve column intent when regenerating rows. |41| File-link list | Bullets containing `[filename.ext](path/to/filename.ext)` | Replace only the contiguous file-list block. |42| HTML marker | Comments such as `<!-- file index -->` or generated-section markers | Prefer marker bounds over heading heuristics. |4344## File analysis4546| Field | Extraction rule | Fallback |47| --- | --- | --- |48| Name | Use the filename with extension unless the surrounding document omits extensions. | Preserve the existing convention. |49| Type | Use extension and category, for example `.md`, `.js`, `.py`, or `Extension`. | `file` when no extension exists. |50| Description | First Markdown heading, first meaningful comment, shebang purpose, package metadata, or inferred filename purpose. | Short inferred description in sentence case. |51| Size | Include only if the existing index includes size or the user asks. | Omit. |52| Modified | Include only if the existing index includes dates or the user asks. | Omit. |5354## Table structure options5556### Simple list5758```markdown59## Files in <folder>6061- [filename.ext](path/to/filename.ext) - Description62- [filename2.ext](path/to/filename2.ext) - Description63```6465### Detailed table6667```markdown68| File | Type | Description |69|------|------|-------------|70| [filename.ext](path/to/filename.ext) | Extension | Description |71| [filename2.ext](path/to/filename2.ext) | Extension | Description |72```7374### Categorized sections7576Group files by type, feature area, or folder when a flat table is too noisy. Keep each category alphabetized.7778## Update rules7980- Preserve existing Markdown structure, heading levels, frontmatter, comments, prose, and document flow outside the index block.81- Use repository-relative or target-file-relative links consistently with the surrounding document.82- Sort files alphabetically by default; use natural order when filenames contain numbered prefixes.83- Escape `|` in table cells and handle spaces, parentheses, and other special characters in filenames.84- Do not include directories unless the user asks for directory entries.85- Do not invent descriptions that claim behavior not visible from file names or contents.8687## Output template8889```markdown90## Markdown file index result9192**Status:** updated | created | blocked93**Target file:** `<file>`94**Source folder:** `<folder>`95**Pattern:** `<pattern>`96**Format:** simple list | detailed table | categorized sections9798### Files indexed99| File | Type | Description |100| --- | --- | --- |101| `<relative/path>` | `<extension or category>` | <description> |102103### Validation104- Existing section found: yes | no105- Relative links valid: pass | fail106- Markdown table/list syntax valid: pass | fail107```108109## Quality gate110111- [ ] The target Markdown file was read before editing.112- [ ] The source folder and pattern were resolved from the user request or `$ARGUMENTS`.113- [ ] Existing index markers, headings, tables, or file-link lists were reused when present.114- [ ] File links are relative, valid, and consistently formatted.115- [ ] Files are sorted alphabetically or by justified natural order.116- [ ] Surrounding Markdown content outside the index block was preserved.117- [ ] Generated descriptions are grounded in file content, comments, headers, or conservative filename inference.