klieret / jekyll-relative-url-check

A pre-commit hook that enforces that all Jekyll links are relative to site.baseurl

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Jekyll: Enforce URLs relative to site.baseurl 🔗✔️

gh actions pre-commit.ci status Black License PR welcome

A script and pre-commit hook that enforces that all links in a Jekyll project are relative to {{ site.baseurl }}.

The reasoning behind this is that links like [link](/absolute/link) will break if the site is not directly deployed at domain.com, but e.g. at user.github.io/projectname.

For example, the following lines should raise errors:

Markdown:

[link](/absolute/link)
![pic](/absolute/picture.png)

HTML:

href="/absolute/link"
src="/absolute/picture.png"

📦 Installation & usage

🎣 As a pre-commit hook

Test it

Simply run the following (no explicit installation required)

pre-commit try-repo https://github.com/klieret/jekyll-relative-url-check jekyll-relative-url-check-html --verbose --all-files
pre-commit try-repo https://github.com/klieret/jekyll-relative-url-check jekyll-relative-url-check-markdown --verbose --all-files

Install it

Include the following snippet in your pre-commit config

repos:
-   repo: https://github.com/klieret/jekyll-relative-url-check
    rev: main
    hooks:
    -   id: jekyll-relative-url-check-html
    -   id: jekyll-relative-url-check-markdown

Afterwards run pre-commit autoupdate to replace main with the latest release.

🔨 As a script

pip3 install .
jekyll-relative-url-check-html [FILE]...
jekyll-relative-url-check-markdown [FILE]...

⏫ Advanced usage

  • Include JEKYLL_RELATIVE_URL_CHECK_SKIP_FILE in a file to skip checking the entire file.
  • Include JEKYLL_RELATIVE_URL_CHECK_SKIP_LINE in a line to skip checking it

💻 Implementation

Currently this hook only works by checking for several simple regular expressions.

About

A pre-commit hook that enforces that all Jekyll links are relative to site.baseurl

License:MIT License


Languages

Language:Python 96.1%Language:HTML 3.9%