|
| 1 | +import os |
| 2 | + |
| 3 | +def create_markdown_from_directory(directory=".", output_file="combined.md"): |
| 4 | + """ |
| 5 | + Recursively traverses the given directory, reads all files (ignoring files/folders in ignore_list), |
| 6 | + and creates a single markdown file containing the contents of each file, prefixed with the |
| 7 | + relative path of the file. |
| 8 | +
|
| 9 | + Args: |
| 10 | + directory (str): The directory to traverse. Defaults to the current directory. |
| 11 | + output_file (str): The name of the output markdown file. Defaults to 'combined.md'. |
| 12 | + """ |
| 13 | + ignore_list = [ |
| 14 | + "node_modules", "__pycache__", ".git", ".DS_Store", "inputs", "indexes", |
| 15 | + "model", "models", ".venv", "temp", ".pytest_cache", ".ruff_cache", |
| 16 | + "extensions", "dir_tree.py", "map.txt", "signal-desktop-keyring.gpg", |
| 17 | + ".husky", ".next", "docs", "index.pkl", "index.faiss", "assets", "fonts", "public", |
| 18 | + "yarn.lock", "package-lock.json", |
| 19 | + ] |
| 20 | + |
| 21 | + with open(output_file, "w", encoding="utf-8") as outfile: |
| 22 | + for root, dirs, files in os.walk(directory): |
| 23 | + # Filter out directories in ignore_list so they won't be traversed |
| 24 | + dirs[:] = [d for d in dirs if d not in ignore_list] |
| 25 | + |
| 26 | + for filename in files: |
| 27 | + if filename in ignore_list: |
| 28 | + continue |
| 29 | + filepath = os.path.join(root, filename) |
| 30 | + |
| 31 | + try: |
| 32 | + with open(filepath, "r", encoding="utf-8") as infile: |
| 33 | + content = infile.read() |
| 34 | + |
| 35 | + # Get a relative path to better indicate file location |
| 36 | + rel_path = os.path.relpath(filepath, directory) |
| 37 | + outfile.write(f"## File: {rel_path}\n\n") |
| 38 | + outfile.write(content) |
| 39 | + outfile.write("\n\n---\n\n") # Separator between files |
| 40 | + |
| 41 | + except Exception as e: |
| 42 | + print(f"Error processing file {filepath}: {e}") |
| 43 | + |
| 44 | + print(f"Successfully created {output_file}") |
| 45 | + |
| 46 | +if __name__ == "__main__": |
| 47 | + create_markdown_from_directory() |
0 commit comments