This directory contains pre-commit hooks that automatically optimize images before they are committed to the repository.
- Automatically detect staged image files (
.png,.jpeg,.jpg,.gif,.svg) - Run
image_optimto compress and optimize raster images (.png,.jpeg,.jpg,.gif) - Re-stage optimized images automatically
- Ensure all committed raster images are properly optimized
- Check staged SVGs against a size limit and abort the commit if an oversized SVG is referenced from any file in
help/(otherwise just warn)
- Reduced repository size
- Faster page loads for documentation
- Consistent image quality across all contributors
- No manual optimization required
- Ruby 3.0 or higher
- Bundler
- Git
.githooks/setup-hooks.shgit config core.hooksPath .githooks
chmod +x .githooks/*-
Clone the repository:
git clone <repository-url> cd commerce-operations.en
-
Enable pre-commit hooks:
.githooks/setup-hooks.sh
-
Install Jekyll dependencies:
cd _jekyll bundle install
- Add an image file to your repository
- Stage it:
git add <image-file> - Try to commit:
git commit -m 'test' - The hook should automatically optimize the image
Found 1 staged image(s). Running optimization...
Checking images ...
path/to/your/image.png 100.00%
Pre-commit image checks complete!The hook's SVG link-detection logic (which decides whether an oversized SVG is referenced from help/) is covered by unit tests that need only Ruby's bundled minitest — no gems or _jekyll setup:
ruby .githooks/test/svg_link_checker_test.rb- PNG: Use for screenshots and UI elements (will be optimized automatically)
- JPEG: Use for photographs (will be optimized automatically)
- GIF: Use for animations (will be optimized automatically)
- SVG: Use for icons and simple graphics (not optimized, but checked against a size limit; commit fails only if the oversized SVG is linked from
help/)
The pre-commit hooks will automatically optimize .png, .jpeg/.jpg, and .gif images on commit, and will check staged SVGs against a size limit (140 KB).
If a staged SVG exceeds the limit and is referenced from a file in help/, the commit is aborted. If the oversized SVG isn't referenced anywhere in help/, the hook only prints a warning and the commit proceeds. Convert oversized SVGs to PNG instead:
cd _jekyll
bundle exec rake images:svg_to_png path=../help/assets/image.svgThe path is relative to _jekyll, so images under help/ are referenced as ../help/....
For manual image optimization:
cd _jekyll
bundle exec rake images:optimize path=../path/to/imagesThe hooks use the configuration file _jekyll/.image_optim.yml to customize optimization settings:
- PNG: Uses
advpng,optipng, andpngquant - JPEG: Uses
jhead,jpegoptim, andjpegtran - GIF: Uses
gifsicle - SVG: Not optimized (excluded from
image_optimto preserve vector graphics and animations), but checked against a 140 KB size limit
- Check hook configuration:
git config core.hooksPath - Ensure the hook file is executable:
chmod +x .githooks/pre-commit - Verify you're in the correct repository with
_jekylldirectory
- Verify
bundle installhas been run in the_jekylldirectory - Check that the
adobe-comdox-exl-rake-tasksgem is installed (provides theimages:optimize,images:check_size, andimages:svg_to_pngrake tasks the hook runs) - Review the
.image_optim.ymlconfiguration file
- The commit is aborted if a staged SVG exceeds 140 KB and is referenced from a file in
help/(otherwise the hook only warns and the commit proceeds) - Convert the SVG to PNG:
cd _jekyll && bundle exec rake images:svg_to_png path=../help/assets/image.svg(the path is relative to_jekyll, so images underhelp/are referenced as../help/...) - Then stage the PNG in place of the SVG and commit again
- Adjust thread count in
_jekyll/.image_optim.yml - Set
DEBUG=1environment variable for detailed error information
- Pre-commit trigger: When you run
git commit, the hook automatically executes - Image detection: Scans staged files for image extensions
- Optimization: Runs
image_optimon each staged PNG, JPEG, or GIF - Re-staging: Automatically adds optimized images back to the staging area
- SVG size check: Checks each staged SVG against the 140 KB size limit
- Commit proceeds: If optimization succeeds and no oversized SVG is referenced from
help/, the commit continues normally; otherwise the commit is aborted (an oversized SVG not referenced fromhelp/only triggers a warning)
- PNG (
.png) - Lossless and lossy compression - JPEG (
.jpg,.jpeg) - Lossy compression with metadata cleanup - GIF (
.gif) - Animation and static optimization - SVG (
.svg) - Not optimized (commit as-is to preserve quality), but checked against a 140 KB size limit; the commit is aborted if the limit is exceeded and the SVG is referenced fromhelp/(otherwise the hook only warns)
- Test the hook: Try committing a small image first to ensure it works
- Review changes: Check the git diff to see optimization results
- Monitor performance: Large images may take time to optimize
- Version control: Hooks are stored in this
.githooks/directory
For issues with the pre-commit hooks:
- Check the hook output for error messages
- Verify your
image_optimsetup is working - Test with the manual rake tasks first
- Review the hook logs and configuration
- Check the hook configuration:
git config core.hooksPath