|
| 1 | +# HTML to Markdown |
| 2 | + |
| 3 | +A simple, original HTML-to-Markdown converter for webpages, local `.html` files, and pasted HTML. |
| 4 | +Run it with no arguments, paste a URL, and it writes a clean `.md` file for you. |
| 5 | + |
| 6 | +## What it does |
| 7 | + |
| 8 | +- Converts headings, paragraphs, links, images, lists, blockquotes, inline code, code blocks, and tables. |
| 9 | +- Uses `--content-mode auto` by default to keep likely article content and skip obvious site chrome. |
| 10 | +- Supports `--content-mode full` when you want the whole page body. |
| 11 | +- Supports `--plain-text` when maximum text retention matters more than Markdown formatting. |
| 12 | +- Supports simple selectors such as `--include-selector article` and `--exclude-selector nav,footer,.ad`. |
| 13 | +- Supports batch conversion with glob input and an output directory. |
| 14 | +- Accepts a URL, a local file path, or stdin. |
| 15 | +- Writes Markdown to stdout or a `.md` file. |
| 16 | +- Uses only the Python standard library. |
| 17 | + |
| 18 | +## Quick Start |
| 19 | + |
| 20 | +From the plugin root: |
| 21 | + |
| 22 | +```bash |
| 23 | +python html2md.py |
| 24 | +``` |
| 25 | + |
| 26 | +Then paste the webpage URL or local HTML file path. The Markdown file will be generated in the |
| 27 | +current directory. |
| 28 | + |
| 29 | +On Windows, if `python` points to Python 2, use: |
| 30 | + |
| 31 | +```powershell |
| 32 | +html2md.cmd |
| 33 | +``` |
| 34 | + |
| 35 | +You can also run the script directly: |
| 36 | + |
| 37 | +```bash |
| 38 | +python scripts/html_to_markdown.py "https://example.com" --output example.md |
| 39 | +``` |
| 40 | + |
| 41 | +Or install it locally and use the `html2md` command: |
| 42 | + |
| 43 | +```bash |
| 44 | +python -m pip install -e . |
| 45 | +html2md |
| 46 | +``` |
| 47 | + |
| 48 | +## Useful Commands |
| 49 | + |
| 50 | +Convert a local file: |
| 51 | + |
| 52 | +```bash |
| 53 | +python html2md.py examples/sample.html --output sample.md |
| 54 | +``` |
| 55 | + |
| 56 | +Keep the full page body instead of automatic article extraction: |
| 57 | + |
| 58 | +```bash |
| 59 | +python html2md.py "https://en.wikipedia.org/wiki/GitHub" --output github-full.md --content-mode full |
| 60 | +``` |
| 61 | + |
| 62 | +Keep more visible text with simpler formatting: |
| 63 | + |
| 64 | +```bash |
| 65 | +python html2md.py "https://en.wikipedia.org/wiki/GitHub" --output github.txt.md --plain-text |
| 66 | +``` |
| 67 | + |
| 68 | +Convert only a selected part of the page: |
| 69 | + |
| 70 | +```bash |
| 71 | +python html2md.py page.html --output article.md --include-selector article --exclude-selector nav,footer,.ad |
| 72 | +``` |
| 73 | + |
| 74 | +Batch convert local HTML files: |
| 75 | + |
| 76 | +```bash |
| 77 | +python html2md.py --input "pages/*.html" --output-dir md |
| 78 | +``` |
| 79 | + |
| 80 | +If a remote webpage times out, increase the timeout and retry count: |
| 81 | + |
| 82 | +```bash |
| 83 | +python html2md.py "https://en.wikipedia.org/wiki/GitHub" --output github.md --timeout 180 --retries 5 |
| 84 | +``` |
| 85 | + |
| 86 | +Convert pasted HTML: |
| 87 | + |
| 88 | +```bash |
| 89 | +Get-Content page.html | py -3 scripts/html_to_markdown.py - --output page.md |
| 90 | +``` |
| 91 | + |
| 92 | +## Quality And Speed |
| 93 | + |
| 94 | +Run tests: |
| 95 | + |
| 96 | +```bash |
| 97 | +py -3 -m unittest discover -s tests |
| 98 | +``` |
| 99 | + |
| 100 | +Run the local benchmark: |
| 101 | + |
| 102 | +```bash |
| 103 | +py -3 benchmarks/bench_converter.py |
| 104 | +``` |
| 105 | + |
| 106 | +## Project Structure |
| 107 | + |
| 108 | +```text |
| 109 | +html-2-markdown/ |
| 110 | + html2md.py # Friendly Python entrypoint |
| 111 | + html2md.cmd # Windows launcher |
| 112 | + html2md # Unix-like launcher |
| 113 | + scripts/html_to_markdown.py # Core converter |
| 114 | + tests/ # Unit tests |
| 115 | + benchmarks/ # Local benchmark |
| 116 | + examples/ # Sample input and output |
| 117 | + .github/workflows/ # GitHub Actions tests |
| 118 | + .codex-plugin/ # Optional Codex plugin metadata |
| 119 | +``` |
| 120 | + |
| 121 | +## Installing as a Codex plugin |
| 122 | + |
| 123 | +This repository is already shaped as a Codex plugin. After cloning it, install or load it through your Codex plugin workflow. |
| 124 | + |
| 125 | +## Author |
| 126 | + |
| 127 | +Created and maintained by [Ryan Zhang](https://github.com/riainzhang). |
| 128 | + |
| 129 | +## Open access and legal notes |
| 130 | + |
| 131 | +Open-sourcing this plugin is okay. For the safest public release: |
| 132 | + |
| 133 | +- Use an open-source license such as MIT. |
| 134 | +- Do not include copied website content in the repository unless you have permission. |
| 135 | +- Respect website terms, robots policies, copyright, paywalls, and login requirements when converting remote pages. |
| 136 | +- Preserve attribution when sharing converted content. |
| 137 | + |
| 138 | +## License |
| 139 | + |
| 140 | +MIT |
0 commit comments