feat: add DOCX/HTML output, go-tomd reverse converter, and input/output flags

- go-mdtopdf-helper: -format pdf,html,docx; new internal/document writers
  (standalone HTML, pure Go DOCX)
- new go-tomd command converting PDF/HTML/DOCX back to Markdown, skipping
  existing .md files unless -force
- -input/-output on both commands (single file with chosen name, or output
  directory mirroring the scan layout)
- restructure README and build config (Makefile, goreleaser) for both binaries
This commit is contained in:
Hein committed 2026-10-08 17:05:01 +02:00
1 parent 50b7246c21
commit 5bf9c3673d
17 files changed
+1903 -84

No files matched your search

+140 -64
View File
@@ -1,32 +1,79 @@
# go-mdtopdf-helper
A command-line tool that automatically converts Markdown files to PDF. Perfect for maintaining PDF documentation alongside your Markdown files in Git repositories.
Two command-line tools for keeping documentation in sync with your Markdown:
- **`go-mdtopdf-helper`** converts Markdown files to PDF, HTML or DOCX.
- **`go-tomd`** does the reverse, converting PDF, HTML and DOCX documents back to Markdown.
Perfect for maintaining PDF documentation alongside your Markdown files in Git repositories.
## Features
- Converts Markdown files to PDF, HTML or DOCX (one or several at once)
- Choice of PDF rendering engines
- Works out of the box: the default engine is pure Go with no external dependencies
- Markdown to PDF, HTML or DOCX, one or several formats at once
- Documents back to Markdown (`go-tomd`)
- Works out of the box: the default PDF engine, the DOCX writer and the DOCX reader are pure Go with no external dependencies
- Choice of PDF engines (pure Go, headless Chrome, or wkhtmltopdf)
- Convert a single file or scan a directory; choose where the output goes
- Can run as a Git pre-commit hook
- Recursive directory scanning
- Parallel file processing
- Recursive directory scanning and parallel processing
- Cross-platform support (Windows, Linux, macOS)
## Output Formats
## Installation
Select formats with `-format`, for example `-format pdf,html,docx`. Each file is written next to its Markdown source with the matching extension (`README.md` → `README.pdf`, `README.html`, `README.docx`).
Download the archive for your platform from the project's GitHub releases. Each archive contains both `go-mdtopdf-helper` and `go-tomd`.
- **pdf**: rendered by the selected engine (see below).
Or build from source (requires Go and, optionally, `golangci-lint` for `make lint`):
```bash
git clone https://github.com/Warky-Devs/go-mdtopdf-helper.git
cd go-mdtopdf-helper
make build # writes bin/go-mdtopdf-helper and bin/go-tomd
```
## go-mdtopdf-helper: Markdown to PDF, HTML and DOCX
Convert every Markdown file under the current directory to PDF:
```bash
go-mdtopdf-helper
```
### Options
```
go-mdtopdf-helper [options]
Options:
-dir string
Directory to scan for markdown files (default ".")
-input string
Convert only this markdown file (instead of scanning -dir)
-output string
With -input: output file name, extension added per format
Without -input: directory to write results to
-recursive
Scan directories recursively (default true)
-parallel
Convert files in parallel (default true)
-format string
Output formats, comma separated: pdf, html, docx (default "pdf")
-engine string
PDF engine: gopdf, wkhtmltopdf, chromedp (default "gopdf")
-hook
Run as git pre-commit hook
```
### Output formats
Select formats with `-format`, for example `-format pdf,html,docx`. By default each file is written next to its Markdown source with the matching extension (`README.md` becomes `README.pdf`, `README.html` and `README.docx`).
- **pdf**: rendered by the selected engine (see below). A4 with 15mm margins.
- **html**: a standalone page with embedded CSS.
- **docx**: a Word document generated in pure Go (no Word or other tools needed). Supports headings, paragraphs, bold/italic/code, links, bulleted and numbered lists, code blocks, quotes, rules and tables. Images are written as `[image: alt]` text.
In pre-commit hook mode every generated file is staged.
### PDF engines
## Engines
The engine only applies to PDF output.
Select an engine with `-engine`:
The engine only applies to PDF output. Select one with `-engine`:
| Engine | Needs | Notes |
|--------|-------|-------|
@@ -34,45 +81,9 @@ Select an engine with `-engine`:
| `chromedp` | Chrome or Chromium | Best rendering (full HTML/CSS). Runs headless Chrome; the sandbox is disabled automatically when running as root, as in most CI containers. |
| `wkhtmltopdf` | [wkhtmltopdf](https://wkhtmltopdf.org/) | Legacy engine. Looked up in the standard install locations (Windows: `C:\Program Files\wkhtmltopdf\bin`, Linux/macOS: `/usr/local/bin`, `/usr/bin`, Homebrew). |
## Installation
### Git pre-commit hook
```bash
go get github.com/Warky-Devs/go-mdtopdf-helper
```
## Usage
### Basic Usage
Convert Markdown files in the current directory:
```bash
go-mdtopdf-helper
```
### Command Line Options
```bash
go-mdtopdf-helper [options]
Options:
-dir string
Directory to scan for markdown files (default ".")
-recursive
Scan directories recursively (default true)
-parallel
Convert files in parallel (default true)
-hook
Run as git pre-commit hook
-engine string
PDF engine: gopdf, wkhtmltopdf, chromedp (default "gopdf")
-format string
Output formats, comma separated: pdf, html, docx (default "pdf")
```
### Git Pre-commit Hook
To use as a Git pre-commit hook:
To run it as a Git pre-commit hook:
1. Create a file named `pre-commit` in your repository's `.git/hooks/` directory
2. Add the following content:
@@ -91,20 +102,83 @@ chmod +x .git/hooks/pre-commit
When enabled as a pre-commit hook, the tool will:
1. Detect staged Markdown files
2. Ask for confirmation before conversion
3. Convert files to PDF
4. Automatically stage the generated PDFs
3. Convert the files to the formats given by `-format` (PDF by default)
4. Automatically stage every generated file
## PDF Output Configuration
To keep HTML or DOCX copies up to date as well, add the formats to the hook, for example `go-mdtopdf-helper -hook -format pdf,html`. The hook cannot be combined with `-input` or `-output`.
Generated PDFs are A4 with 15mm margins on all sides. The `chromedp` and `wkhtmltopdf` engines render the full Markdown-to-HTML output, so they support everything the Markdown parser's common extensions produce.
## go-tomd: documents to Markdown
## Project Layout
`go-tomd` scans a directory for `.pdf`, `.html`/`.htm` and `.docx` files and writes `<name>.md` next to each one.
```bash
go-tomd
```
### Options
```
cmd/go-mdtopdf-helper/ CLI entry point (flags, hook mode)
go-tomd [options]
Options:
-dir string
Directory to scan for documents (default ".")
-input string
Convert only this document (instead of scanning -dir); -format is ignored
-output string
With -input: Markdown file name to write (.md added if missing)
Without -input: directory to write results to
-recursive
Scan directories recursively (default true)
-parallel
Convert files in parallel (default true)
-format string
Input formats to convert, comma separated: docx, html, pdf (default "docx,html,pdf")
-force
Overwrite Markdown files that already exist
```
An existing `.md` is never overwritten unless you pass `-force`, so converting `doc.html` will not clobber a `doc.md` you wrote by hand. If several documents map to the same Markdown file (for example `doc.docx` and `doc.pdf`), one is chosen in the order docx, html, pdf and a warning is printed.
### How each format is read
| Input | How | Fidelity |
|-------|-----|----------|
| `docx` | Parsed directly (no Word needed). Headings, bold/italic/code, links, nested bulleted and numbered lists, code blocks, quotes, rules and tables. | High |
| `html` | [html-to-markdown](https://github.com/JohannesKaufmann/html-to-markdown), including tables and strikethrough. | High |
| `pdf` | Best effort, pure Go. PDFs have no real structure, so headings are inferred from font size, code from monospaced fonts, and lists from bullet or number prefixes. Tables and images are not recovered, and text in multi-column layouts may interleave. | Good on PDFs made by this tool, rougher on arbitrary ones |
## Input and output names
Both commands take `-input` and `-output`:
```bash
# Convert one file, choosing the output name (the extension is added per format)
go-mdtopdf-helper -input docs/guide.md -output build/manual -format pdf,html
# -> build/manual.pdf, build/manual.html
# Scan a directory but write results elsewhere, mirroring the folder layout
go-mdtopdf-helper -dir docs -output build
# And the reverse
go-tomd -input report.docx -output notes/report # -> notes/report.md
go-tomd -dir exports -output imported
```
- `-input` replaces the directory scan, so it cannot be combined with `-dir` or `-recursive` (or `-hook`). `go-mdtopdf-helper` expects a `.md`/`.markdown` file, and `go-tomd` a `.pdf`, `.html`/`.htm` or `.docx` file.
- With `-input`, `-output` is a file name. For `go-mdtopdf-helper` it is a base name that gets each format's extension; a name that already ends in the extension of the single requested format (`-output out.pdf -format pdf`) is used as is, and a mismatched extension is an error. For `go-tomd`, `.md` is added unless the name already ends in `.md` or `.markdown`.
- Without `-input`, `-output` is a directory, created if needed.
- Parent directories of the output are created automatically.
## Project layout
```
cmd/go-mdtopdf-helper/ Markdown to PDF/HTML/DOCX CLI (flags, hook mode)
cmd/go-tomd/ PDF/HTML/DOCX to Markdown CLI
internal/converter/ File discovery, parallel conversion, git hook
internal/document/ HTML and DOCX writers
internal/engine/ Engine interface and the gopdf, wkhtmltopdf and chromedp backends
internal/tomd/ PDF, HTML and DOCX readers (to Markdown)
```
## Contributing
@@ -117,7 +191,7 @@ Contributions are welcome! Please feel free to submit a Pull Request. Here's how
4. Push to the branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request
### Development Setup
### Development setup
1. Clone the repository
2. Install dependencies:
@@ -125,12 +199,12 @@ Contributions are welcome! Please feel free to submit a Pull Request. Here's how
go mod download
```
3. Make your changes
4. Run tests:
4. Run the checks:
```bash
make test
make all
```
`make` also provides `build`, `lint` (golangci-lint), `format` (gofmt) and `all`. Tests for the `chromedp` and `wkhtmltopdf` engines are skipped when the tool is not installed.
Make targets: `build` (both binaries into `bin/`), `run` and `run-tomd` (pass arguments with `ARGS="..."`), `test`, `lint` (golangci-lint), `format` (gofmt), `clean`, and `all` (format, lint, test, build). Tests for the `chromedp` and `wkhtmltopdf` engines are skipped when the tool is not installed.
## Dependencies
@@ -138,6 +212,8 @@ Contributions are welcome! Please feel free to submit a Pull Request. Here's how
- [chromedp](https://github.com/chromedp/chromedp) - Headless Chrome driver
- [go-wkhtmltopdf](https://github.com/SebastiaanKlippert/go-wkhtmltopdf) - Go wrapper for wkhtmltopdf
- [gomarkdown](https://github.com/gomarkdown/markdown) - Markdown parser and HTML renderer
- [pdf](https://github.com/ledongthuc/pdf) - Pure Go PDF text extraction (go-tomd)
- [html-to-markdown](https://github.com/JohannesKaufmann/html-to-markdown) - HTML to Markdown (go-tomd)
## License
@@ -146,4 +222,4 @@ This project is licensed under the MIT License - see the LICENSE file for detail
## Acknowledgments
- Thanks to [SebastiaanKlippert](https://github.com/SebastiaanKlippert) for the go-wkhtmltopdf library
- Thanks to the gomarkdown team for their Markdown parser
- Thanks to the gomarkdown team for their Markdown parser