Skip to content

Commit a06400b

Browse files
jupblbampcode-com
andcommitted
Add pre-commit pandoc hook with configurable formatting options
- Wraps pandoc for formatting Markdown files - Configurable column width (default: 80) - Optional reference-style links (default: enabled) - Configurable input/output formats (default: gfm) - MIT licensed for open source use Co-authored-by: Amp <[email protected]> Amp-Thread-ID: https://ampcode.com/threads/T-a4b3b491-de29-4cf7-a459-f324a9c0c53b
1 parent 5b40aad commit a06400b

File tree

5 files changed

+246
-0
lines changed

5 files changed

+246
-0
lines changed

.pre-commit-config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Example configuration for testing
2+
repos:
3+
- repo: .
4+
rev: HEAD
5+
hooks:
6+
- id: pandoc
7+
# Optional: customize formatting options
8+
# args: [--columns=100, --no-reference-links, --from=markdown, --to=markdown]

.pre-commit-hooks.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
- id: pandoc
2+
name: Format markdown files with pandoc
3+
description: Format markdown files using pandoc with standardized settings
4+
entry: pandoc-format
5+
language: script
6+
types: [markdown]
7+
minimum_pre_commit_version: 2.17.0

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Michal Kielbowicz
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# pre-commit-pandoc
2+
3+
A [pre-commit] hook for formatting Markdown files using [pandoc].
4+
5+
## Features
6+
7+
This hook formats Markdown files with pandoc using the following settings:
8+
9+
- 80 column width for better readability
10+
- Reference-style links for cleaner markdown
11+
- GitHub Flavored Markdown (GFM) input and output
12+
- Standalone document mode
13+
14+
## Prerequisites
15+
16+
- [pre-commit] installed
17+
- [pandoc][1] installed
18+
19+
## Installation
20+
21+
Add this to your `.pre-commit-config.yaml`:
22+
23+
``` yaml
24+
repos:
25+
- repo: https://github.com/jupblb/pre-commit-pandoc
26+
rev: v1.0.0 # Use the latest version
27+
hooks:
28+
- id: pandoc
29+
```
30+
31+
Then run:
32+
33+
``` bash
34+
pre-commit install
35+
```
36+
37+
## Usage
38+
39+
The hook will automatically format your Markdown files when you commit. You can
40+
also run it manually:
41+
42+
``` bash
43+
# Run on all files
44+
pre-commit run pandoc --all-files
45+
46+
# Run on specific files
47+
pre-commit run pandoc --files README.md CONTRIBUTING.md
48+
```
49+
50+
## Configuration
51+
52+
The pandoc hook uses the following default settings:
53+
54+
- `--columns=80` - Wrap text at 80 columns
55+
- `--reference-links` - Use reference-style links
56+
- `-s` - Standalone document (always enabled)
57+
- `-f gfm` - From GitHub Flavored Markdown
58+
- `-t gfm` - To GitHub Flavored Markdown
59+
60+
You can customize these settings using args in your `.pre-commit-config.yaml`:
61+
62+
``` yaml
63+
repos:
64+
- repo: https://github.com/jupblb/pre-commit-pandoc
65+
rev: v1.0.0
66+
hooks:
67+
- id: pandoc
68+
args:
69+
- --columns=100 # Set column width to 100
70+
- --no-reference-links # Disable reference-style links
71+
- --from=markdown # Change input format
72+
- --to=markdown # Change output format
73+
```
74+
75+
### Available Arguments
76+
77+
- `--columns <number>` - Set the column width (default: 80)
78+
- `--no-reference-links` - Disable reference-style links (default: enabled)
79+
- `--from <format>` - Set input format (default: gfm)
80+
- `--to <format>` - Set output format (default: gfm)
81+
82+
Common format values include: `gfm` (GitHub Flavored Markdown), `markdown`,
83+
`markdown_strict`, `commonmark`. See [pandoc documentation][2] for all
84+
supported formats.
85+
86+
## How it Works
87+
88+
The hook runs pandoc on each Markdown file with the specified flags. If the file
89+
content changes after formatting, the hook will:
90+
91+
1. Update the file with the formatted content
92+
2. Exit with a non-zero status to prevent the commit
93+
3. Allow you to review and stage the changes
94+
95+
## Contributing
96+
97+
Contributions are welcome! Please feel free to submit a Pull Request.
98+
99+
## License
100+
101+
MIT License - see LICENSE file for details.
102+
103+
[pre-commit]: https://pre-commit.com
104+
[pandoc]: https://pandoc.org/
105+
[1]: https://pandoc.org/installing.html
106+
[2]: https://pandoc.org/MANUAL.html#general-options

pandoc-format

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Wrapper script for pandoc to format markdown files
4+
# Used as a pre-commit hook
5+
6+
set -euo pipefail
7+
8+
# Check if pandoc is installed
9+
if ! command -v pandoc &> /dev/null; then
10+
echo "Error: pandoc is not installed. Please install pandoc before using this hook."
11+
echo "Visit https://pandoc.org/installing.html for installation instructions."
12+
exit 1
13+
fi
14+
15+
# Default values
16+
COLUMNS=80
17+
USE_REFERENCE_LINKS=true
18+
FROM_FORMAT="gfm"
19+
TO_FORMAT="gfm"
20+
21+
# Parse arguments
22+
FILES=()
23+
while [[ $# -gt 0 ]]; do
24+
case "$1" in
25+
--columns)
26+
if [[ $# -lt 2 ]]; then
27+
echo "Error: --columns requires an argument"
28+
exit 1
29+
fi
30+
COLUMNS="$2"
31+
shift 2
32+
;;
33+
--no-reference-links)
34+
USE_REFERENCE_LINKS=false
35+
shift
36+
;;
37+
--from)
38+
if [[ $# -lt 2 ]]; then
39+
echo "Error: --from requires an argument"
40+
exit 1
41+
fi
42+
FROM_FORMAT="$2"
43+
shift 2
44+
;;
45+
--to)
46+
if [[ $# -lt 2 ]]; then
47+
echo "Error: --to requires an argument"
48+
exit 1
49+
fi
50+
TO_FORMAT="$2"
51+
shift 2
52+
;;
53+
*)
54+
FILES+=("$1")
55+
shift
56+
;;
57+
esac
58+
done
59+
60+
# Check if any files were provided
61+
if [ ${#FILES[@]} -eq 0 ]; then
62+
echo "Error: No files provided"
63+
exit 1
64+
fi
65+
66+
# Build pandoc arguments
67+
PANDOC_ARGS=(
68+
"--columns=$COLUMNS"
69+
"-s"
70+
"-f" "$FROM_FORMAT"
71+
"-t" "$TO_FORMAT"
72+
)
73+
74+
if [ "$USE_REFERENCE_LINKS" = true ]; then
75+
PANDOC_ARGS+=("--reference-links")
76+
fi
77+
78+
# Process each file
79+
exit_code=0
80+
81+
for file in "${FILES[@]}"; do
82+
# Create a temporary file for output
83+
tmpfile=$(mktemp)
84+
85+
# Run pandoc with the specified flags
86+
if pandoc "${PANDOC_ARGS[@]}" --output "$tmpfile" "$file" 2>/dev/null; then
87+
88+
# Check if the file changed
89+
if ! cmp -s "$file" "$tmpfile"; then
90+
# Replace the original file with the formatted version
91+
mv "$tmpfile" "$file"
92+
echo "Formatted: $file"
93+
exit_code=1
94+
else
95+
rm "$tmpfile"
96+
fi
97+
else
98+
echo "Error: Failed to format $file"
99+
rm -f "$tmpfile"
100+
exit_code=1
101+
fi
102+
done
103+
104+
exit $exit_code

0 commit comments

Comments
 (0)