Back to notes

Build an index for Markdown notes

Use the Python standard library to sort note files and generate an editable index.

Start with the existing directory

This example assumes filenames contain neither spaces nor Markdown punctuation. Print the index to standard output and review it before saving.

from pathlib import Path

for note in sorted(Path("notes").glob("*.md")):
    title = note.stem.replace("-", " ")
    print(f"- [{title}]({note.as_posix()})")

Pick a stable ordering

Sorting by path avoids changes caused by filesystem traversal order. As the index grows, group notes by directory or read titles from their Markdown metadata.