Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
svenluijten committed Jul 10, 2023
0 parents commit 2300709
Show file tree
Hide file tree
Showing 36 changed files with 881 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[composer.json]
indent_size = 4
indent_style = space
10 changes: 10 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
* text=auto

/tests export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/CONTRIBUTING.md export-ignore
/phpunit.xml.dist export-ignore

/tests/**/__snapshots__/** binary
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: weekly
30 changes: 30 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Tests
on: [push, pull_request]

jobs:
test:
name: PHP ${{ matrix.php }} on ${{ matrix.os }} (${{ matrix.dependency-version }})
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
php: [8.1, 8.2]
dependency-version: [highest, lowest]
steps:
- uses: actions/[email protected]

- name: Configure PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom
coverage: none

- name: Install dependencies
uses: ramsey/composer-install@v2
with:
dependency-versions: ${{ matrix.dependency-version }}
composer-options: "--prefer-dist"

- name: Execute tests
run: vendor/bin/phpunit
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/composer.phar
/composer.lock
/vendor
93 changes: 93 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# How to contribute
Thank you for considering to contribute to this repository! This file will walk you through all the steps to ensure both
you and I have a good time submitting and reviewing your contribution. First off, some basic rules and reading material:

- Submit your work in a new branch and make the PR to the master branch.
- [Write a short & descriptive commit message](http://chris.beams.io/posts/git-commit/).
- Rebase before committing the final change.
- Stick to [PSR-2](http://www.php-fig.org/psr/psr-2/).
- Add tests if necessary and ensure all the tests are green on the final commit.
- Make sure the CI tools used by the repository are all in order. If one fails, you should make it pass.

## Contributing
Here are the steps to follow to contribute to this repository:

- [Fork this repository on GitHub](#fork-this-repository).
- [Clone your fork to your local machine](#clone-your-fork).
- [Create a feature branch](#create-a-branch).
- [Add an 'upstream' remote](#add-a-remote).
- [Regularly pull & rebase from the upstream remote](#pull-and-rebase).
- [Work on feature branch](#working-on-branch).
- [Make tests pass](#make-tests-pass)
- [Submit a pull request to the master branch](#submitting-pull-request).

### Fork this repository
Fork the repository right here on GitHub. To learn more about forking a repository, visit
[GitHub's help article](https://help.github.com/articles/fork-a-repo/).

### Clone your fork
Clone your repository on your local machine to start work on your pull request.

```bash
$ git clone [email protected]:<USERNAME>/<REPOSITORY>.git
# Or if you prefer HTTPS:
$ git clone https://github.com/<USERNAME>/<REPOSITORY>.git

$ cd <REPOSITORY>
```

### Create a branch
Make your own feature branch in order not to clutter up master.

```bash
# Think of a good name for your branch:
# fix/typo-in-readme
# feature/some-feature
# bug/some-bug-you-are-fixing
$ git checkout -b <BRANCH_NAME>
```

### Add a remote
Add an 'upstream' remote to pull from and to stay up to date with the work being done in there.

```bash
# List all current remotes
$ git remote -v
origin [email protected]/<USERNAME>/<REPOSITORY>.git (fetch)
origin [email protected]/<USERNAME>/<REPOSITORY>.git (push)

# Add a new remote called 'upstream'
$ git remote add upstream [email protected]:svenluijten/<REPOSITORY>.git
# Or if you prefer HTTPS:
$ git remote add upstream https://github.com/svenluijten/<REPOSITORY>.git

# The new remote should now be in the list
$ git remote -v
origin [email protected]/<USERNAME>/<REPOSITORY>.git (fetch)
origin [email protected]/<USERNAME>/<REPOSITORY>.git (push)
upstream [email protected]/svenluijten/<REPOSITORY>.git (fetch)
upstream [email protected]/svenluijten/<REPOSITORY>.git (push)
```

### Pull and rebase
Pull from upstream to stay up to date with what others might be doing in this repository. Any merge conflicts that arise
are your responsibility to resolve.

```bash
$ git pull --rebase upstream master
```

### Working on branch
Do your magic and make your fix. I can't help you with this 😉. Once you're happy with the result and all tests pass,
go ahead and push to your feature branch.

```bash
$ git push origin <BRANCH_NAME>
```

### Make tests pass
You can run `composer check` to see if the tests & static analysis pass. Feel free to only run the static analyses at the
very end, this could take a while on bigger projects. To only run the tests, run `composer test`.

### Submitting pull request
Now, let's head back over to this repository on GitHub and submit the pull request!
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# The MIT License (MIT)

Copyright (c) Sven Luijten

> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in
> all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> THE SOFTWARE.
120 changes: 120 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# CommonMark Dark Mode Images Extension

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Total Downloads][ico-downloads]][link-downloads]
[![Software License][ico-license]](LICENSE.md)
[![Build Status][ico-build]][link-build]

This [CommonMark](https://commonmark.thephpleague.com) extension allows you to determine what images to show in dark- or
light mode in Markdown converted using CommonMark.

## Installation
Via [composer](http://getcomposer.org):

```bash
composer require sven/commonmark-dark-mode-images
```

## Usage
To enable the extension, first make sure [the `Attributes` extension](https://commonmark.thephpleague.com/2.4/extensions/attributes/)
that ships with CommonMark is enabled. Then, add the extension to the CommonMark environment:

```php
use Sven\CommonMark\DarkModeImages\DarkModeImagesExtension;

$environment->addExtension(new DarkModeImagesExtension());
```

You'll have to apply some classes to your Markdown images to indicate to this extension that they should be converted:

```md
![Screenshot of a settings dialog](/images/settings-light.jpg){.light-image}
![dark](/images/settings-dark.jpg){.dark-image}
```

These `{.light-image}` and `{.dark-image}` tags are part of [the `Attributes` extension](https://commonmark.thephpleague.com/2.4/extensions/attributes/).
They apply classes to the converted `<img>` elements, which this extension uses to do its magic. The resulting HTML will
look something like this:

```html
<picture class="dark-mode-images-picture">
<source srcset="/images/settings-dark.jpg" media="(prefers-color-scheme: dark)" />
<img src="/images/settings-light.jpg" alt="Screenshot of a settings dialog"/>
</picture>
```

## Configuration

### Fallback or original
This option allows you to select either `'light'` or `'dark'`as the "original" form of the picture you're embedding.
This determines which picture will be used if the browser doesn't support the `<picture>` element.

```php
use League\CommonMark\Environment\Environment;
use Sven\CommonMark\DarkModeImages\DarkModeImagesExtension;

$environment = new Environment([
'dark_mode_images' => [
'fallback' => 'dark', // Default: 'light'.
],
]);

$environment->addExtension(new DarkModeImagesExtension());
```

### Class on `<picture>` element
To change the class that is eventually applied to the rendered `<picture>` element, use the `picture_class`
configuration option:

```php
use League\CommonMark\Environment\Environment;
use Sven\CommonMark\DarkModeImages\DarkModeImagesExtension;

$environment = new Environment([
'dark_mode_images' => [
'picture_class' => '<your class here>', // Default: 'dark-mode-images-picture'.
],
]);

$environment->addExtension(new DarkModeImagesExtension());
```

### Light and dark classes
Use the `dark_image_class` and `light_image_class` configuration options to determine what classes to apply to your
Markdown images to indicate which is for dark mode, and which is for light mode.

```php
use League\CommonMark\Environment\Environment;
use Sven\CommonMark\DarkModeImages\DarkModeImagesExtension;

$environment = new Environment([
'dark_mode_images' => [
'light_image_class' => 'l', // Default: 'light-image'.
'dark_image_class' => 'd', // Default: 'dark-image'.
],
]);

$environment->addExtension(new DarkModeImagesExtension());
```

### Note
It is _not_ recommended to use this extension when converting to Markdown on-the-fly on every request. This is best
suited for use in a static site generator like [Jigsaw](https://jigsaw.tighten.co/).

## Contributing
All contributions (pull requests, issues and feature requests) are welcome. Make sure to read through the
[CONTRIBUTING.md](CONTRIBUTING.md) first, though. See the [contributors page](../../graphs/contributors) for all
contributors.

## License
`sven/commonmark-dark-mode-images` is licensed under the MIT License (MIT). Please see [the license file](LICENSE.md)
for more information.

[ico-version]: https://img.shields.io/packagist/v/sven/commonmark-dark-mode-images.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-green.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/sven/commonmark-dark-mode-images.svg?style=flat-square
[ico-build]: https://img.shields.io/github/workflow/status/svenluijten/commonmark-dark-mode-images/Tests?style=flat-square

[link-packagist]: https://packagist.org/packages/sven/commonmark-dark-mode-images
[link-downloads]: https://packagist.org/packages/sven/commonmark-dark-mode-images
[link-build]: https://github.com/svenluijten/commonmark-dark-mode-images/actions/workflows/run-tests.yml
34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "sven/commonmark-dark-mode-images",
"description": "",
"keywords": [],
"license": "MIT",
"authors": [
{
"name": "Sven Luijten",
"email": "[email protected]",
"homepage": "https://svenluijten.com"
}
],
"require": {
"league/commonmark": "^2.3"
},
"require-dev": {
"ext-dom": "*",
"phpunit/phpunit": "^10.0",
"spatie/phpunit-snapshot-assertions": "^5.0.1"
},
"autoload": {
"psr-4": {
"Sven\\CommonMark\\DarkModeImages\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Sven\\CommonMark\\DarkModeImages\\Tests\\": "tests/"
}
},
"config": {
"sort-packages": true
}
}
17 changes: 17 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
backupGlobals="false"
bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<testsuites>
<testsuite name="CommonMark Dark Mode Images Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
Loading

0 comments on commit 2300709

Please sign in to comment.