In Github you can specify the theme an image is displayed to
by appending #gh-light-mode-only
or #gh-dark-mode-only
to
the end of an image URL, in Markdown. See Specifying the
theme an image is shown to.
However, other platforms currently do not support this syntax and will display both versions of the image. So you might want to delete one of the images before uploading your documents to other platforms like, for example, Packagist (PHP) or PyPI (Python).
These npm package, CLI and Github Action are for you. They strip all the image theme links about one of the Github theme versions from your files. Perfect for running it before your packaging step in your release pipelines.
npm install strip-gh-theme-links
import stripGhThemeLinks from "strip-gh-theme-links";
const content = `
<p align="center>
<img src="https://raw.githubusercontent.com/user/repo/assets/readme/logo-black.svg#gh-light-mode-only" alt="logo"> <img src="https://raw.githubusercontent.com/user/repo/assets/readme/logo-white.svg#gh-dark-mode-only" alt="logo">
</p>
`
console.log(stripGhThemeLinks(content, 'light'))
/* OUTPUT:
<p align="center>
<img src="https://raw.githubusercontent.com/user/repo/assets/readme/logo-black.svg" alt="logo">
</p>
*/
# stripGhThemeLinks(content: string, keep: 'light' | 'dark'): string
- # content ⇒ Content for which the Github theme image links will be stripped.
- #
keep (default:
'light'
) ⇒ Theme variant links to keep in the content.
$ strip-gh-theme-links --help
name: Release
on:
workflow_dispatch:
jobs:
release-pr:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Strip Github theme image links
uses: mondeja/strip-gh-theme-links@v2
with:
files: |
README.md
CONTRIBUTING.md
⚠️ It is recomended to run the CLI withstrip-gh-theme-links --diff file.md
to check that your files are correctly stripped before configure this action in your release pipeline.If you experiments errors, please report them.
- # files ⇒ (required) ⇒ Path to files or globs to strip, separated by newlines.
- # keep
(default:
'light'
) ⇒ Theme variant links to keep in the content of the files. - # strict
(default:
false
) ⇒ Treat warnings as errors and exit with code 1. Warnings are raised when a file specified infiles
input is not found or when any image links are stripped from a file.