rehype plugin to enhance code for colors.
- What is this?
- When should I use this?
- Install
- Use
- API
- Bugs
- Authoring
- HTML
- CSS
- Syntax
- Types
- Compatibility
- Security
- Related
- Contribute
- License
This plugin enhances code that contains a color (such as #00eaff
).
By default it appends markup for a little bullet that, with some CSS, can be
displayed as a preview of colors.
These “color chips” are markup specific to github.com that only work in comments, not in files.
This plugin is part of a monorepo rehype-github
.
See its readme for more info.
You can use this plugin when you want to match how github.com works or when you want to build similar pipelines that enhance color.
This package is ESM only. In Node.js (version 16.0+), install with npm:
npm install rehype-github-color
In Deno with esm.sh
:
import rehypeGithubColor from 'https://esm.sh/rehype-github-color@0'
In browsers with esm.sh
:
<script type="module">
import rehypeGithubColor from 'https://esm.sh/rehype-github-color@0?bundle'
</script>
Say our module example.js
looks as follows:
import rehypeGithubColor from 'rehype-github-color'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeGithubColor)
.use(rehypeStringify)
.process('<code>#00eaff</code>')
console.log(String(file))
…now running node example.js
yields:
<code>#00eaff<span class="ml-1 d-inline-block border circle color-border-subtle" style="background-color: #00eaff; height: 8px; width: 8px;"></span></code>
This package exports the identifiers
defaultBuild
and
defaultExpression
.
The default export is
rehypeGithubColor
.
The default builder to turn a color into rich content.
value
(string
) — color matched by expression
The markup for a color chip that is appended to the code by github.com
(ElementContent
).
The default expression that GitHub uses to match colors (RegExp
).
Supports some formats of hex (#00eaff
), RGB (rgb(1, 2, 3)
), RGBA
(rgba(1, 2, 3, 0.4)
), and HSL (hsl(0, 1%, 2%)
) colors.
See § Syntax for more info.
Plugin to enhance code for colors.
options
(Options
, optional) — configuration
What to do with the result from the builder (TypeScript type).
You can either append to the code element or replace it.
type Behavior = 'append' | 'replace'
Make rich content from a color (TypeScript type).
value
(string
) — color matched by expression
Rich content (ElementContent
or Array<ElementContent>
).
Configuration (TypeScript type).
behavior
(Behavior
, default:'append'
) — what to do with the existing code and the built contentbuild
(Build
, default:defaultBuild
) — make rich content from a colorexpression
(RegExp
, default:defaultExpression
) — match colors
GitHub supports a very limited set of colors. It does not support any of the modern features. See § Syntax for more info.
Just be careful that the syntax on GitHub is very limited and only works in comments.
See § Writing on GitHub for more info.
The markup for color chips on github.com is:
<span class="ml-1 d-inline-block border circle color-border-subtle" style="background-color: xxx; height: 8px; width: 8px;"></span>
…where xxx
is the color as it was authored.
The following CSS is needed to make color chips look like GitHub.
/* Default dark */
:root {
--color-border-default: #30363d;
--color-border-subtle: rgba(240, 246, 252, 0.1);
}
.d-inline-block {
display: inline-block !important;
}
.ml-1 {
margin-left: 4px !important;
}
.color-border-subtle {
border-color: var(--color-border-subtle) !important;
}
.circle {
border-radius: 50% !important;
}
.border {
border: 1px solid var(--color-border-default) !important;
}
Color chips form with, roughly, the following BNF:
color ::= color_hex | color_rgb | color_rgba | color_hsl
color_hex ::= '#' 6*digit_hex
color_rgb ::= 'r' 'g' 'b' '(' ws number_like ws 2( ',' ws number_like ws ) ')'
color_rgba ::= 'r' 'g' 'b' 'a' '(' ws number_like ws 3( ',' ws number_like ws ) ')'
color_hsl ::= 'h' 's' 'l' '(' ws number_like ws 2( ',' ws number_like '%' ws ) ',' ws number_like ws ')'
ws ::= 0*' '
number_like ::= 1*digit | ( 0*digit '.' 0*digit )
hex ::= digit | digit_hex_lower | digit_hex_upper
digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
digit_hex_lower ::= 'a' | 'b' | 'c' | 'd' | 'e' | 'f'
digit_hex_upper ::= 'A' | 'B' | 'C' | 'D' | 'E' | 'F'
This package is fully typed with TypeScript.
It exports the additional types Behavior
,
Build
, and Options
.
Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 16.0+. Our projects sometimes work with older versions, but this is not guaranteed.
This plugin works with rehype-parse
version 3+, rehype-stringify
version
3+, rehype
version 5+, and unified
version 6+.
This package is safe.
The build
option is unsafe when used with user content as it allows for
arbitrary HTML.
remark-gfm
— support GFM in remark
See contributing.md
in rehypejs/.github
for ways to get
started.
See support.md
for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.
This project is not affiliated with GitHub.