https://github.com/chrisweb/rehype-github-alerts
rehype plugin to create alerts (admonitions/callouts) ⚠️, mimicking the way alerts get rendered on github.com
https://github.com/chrisweb/rehype-github-alerts
admonitions alerts callouts github hast markdown mdx rehype rehype-plugin typescript
Last synced: 7 days ago
JSON representation
rehype plugin to create alerts (admonitions/callouts) ⚠️, mimicking the way alerts get rendered on github.com
- Host: GitHub
- URL: https://github.com/chrisweb/rehype-github-alerts
- Owner: chrisweb
- License: mit
- Created: 2023-11-01T19:27:46.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-22T12:01:51.000Z (about 1 month ago)
- Last Synced: 2025-04-23T01:16:06.477Z (7 days ago)
- Topics: admonitions, alerts, callouts, github, hast, markdown, mdx, rehype, rehype-plugin, typescript
- Language: TypeScript
- Homepage:
- Size: 756 KB
- Stars: 25
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/rehype-github-alerts)
[](https://github.com/chrisweb/rehype-github-alerts/blob/master/LICENSE)# rehype-github-alerts
## Introduction
rehype plugin to create alerts (admonitions/callouts), mimicking the way alerts get rendered on github.com (based on this [GitHub community "Alerts" discussion](https://github.com/orgs/community/discussions/16925)), currently 5 types of alerts are supported:
> [!NOTE]
> Highlights information that users should take into account, even when skimming.> [!TIP]
> Optional information to help a user be more successful.> [!IMPORTANT]
> Crucial information necessary for users to succeed.> [!WARNING]
> Critical content demanding immediate user attention due to potential risks.> [!CAUTION]
> Negative potential consequences of an action.the markdown syntax for the 5 examples above is as follows:
```md
> [!NOTE]
> Highlights information that users should take into account, even when skimming.> [!TIP]
> Optional information to help a user be more successful.> [!IMPORTANT]
> Crucial information necessary for users to succeed.> [!WARNING]
> Critical content demanding immediate user attention due to potential risks.> [!CAUTION]
> Negative potential consequences of an action.
```this is a zero configuration package as all [options](#options) have defaults, but you can use them if you wish to modify default behavior, like for example by default 5 alerts are defined (with a default icon for each), use `options.alerts` to replace them with your own setup, there is also a default build that will create an HTML output that mimics what GitHub does, but you can change the build to create whatever HTML suits your needs best, check out the ["options" chapter](#options) to learn more about customization
## installation
To install rehype-github-alerts:
```shell
npm i rehype-github-alerts --save-exact
```## examples
### Rehype example
For a simple example have a look at the ["rehype example" README](./examples/simple-rehype-example/README.md) (the source code is located in `examples/simple-rehype-example/`)
### Next.js tutorial
I published a Next.js [Next.js MDX tutorial](https://chris.lu/web_development/tutorials/next-js-static-first-mdx-starterkit) on my blog, the tutorial has a page about [using the **rehype-github-alerts** plugin with **Next.js**](https://chris.lu/web_development/tutorials/next-js-static-first-mdx-starterkit/github-like-alerts-plugin)
### Customized plugin Demo
You can now see a live demo of this plugin on my blog, especially in my web_development [chris.lu/web_development](https://chris.lu/web_development) section, the source code is in the [chris.lu repository](https://github.com/chrisweb/chris.lu), the configuration I used can be found in the next.config.ts file and the styling is in /app/global.css
### How does GitHub render alerts
I created [an issue on github](https://github.com/chrisweb/rehype-github-alerts/issues/1) to check how github is rendering alerts (will add more examples over time, based on feedback)
## styling
If you want to add styling / CSS similar to what GitHub uses, then you can get started by using the stylesheet that is included in the build of this package. The stylesheet is in [dist/styling/css/index.css](/dist/styling/css/index.css).
You can either open the file and copy what you need and paste it into your own CSS file
Or you could import the stylesheet in a Next.js layout file like this:
```js
import '@/node_modules/rehype-github-alerts/dist/styling/css/index.css'
```There is an example in the layout file of the [Next.js MDX starterkit](https://github.com/chrisweb/next-js-static-first-mdx-starterkit_tutorial_chris.lu/blob/92500597d8152910aabec0bf9ea56477dca3e1b0/app/layout.tsx) repository
Another option is to use the [webpack CSS loader](https://www.npmjs.com/package/css-loader) to include the css file into your builds
### icons
In v4 we switched to using the [GitHub Primer Octicons](https://primer.style/foundations/icons/)
If you prefer using other icons like the [twbs icons](https://icons.getbootstrap.com/), then have a look at the [v3 icons build](#v3-icons-build) chapter
## options
`options` (optional)
all options have default values which for most use cases should be enough, meaning there is zero configuration to do, unless you want to customize something
- `alerts` (`IAlert[]`)
- `supportLegacy` (`boolean`, default: false)
- `build` (`DefaultBuildType`)### build option
the build option can be used to customize how alerts get rendered, this can be useful if you want to modify what css classes the elements have
the build option accepts a function that has two parameters:
alertOptions: this is an object of type **IAlert**, meaning it contains the options of the alert that got matched, like the keyword, icon and title
originalChildren: an array of type **DefaultBuildType**, containing the original children (body content of the alert)for example in your configuration file create a rehype-github-alerts **build** option like this:
```mjs
/**
* @typedef {import('rehype-github-alerts').IOptions} IOptions
* @typedef {import('rehype-github-alerts').DefaultBuildType} DefaultBuildType
*//** @type { DefaultBuildType } */
const myGithubAlertBuild = (alertOptions, originalChildren) => {
const alert = {
type: 'element',
tagName: 'div',
properties: {
className: [
`markdown-alert-${alertOptions.keyword.toLowerCase()}`,
],
},
children: [
...originalChildren
],
}return alert
}/** @type { IOptions } */
const rehypeGithubAlertsOptions = {
build: myGithubAlertBuild,
}
```then use the following markdown code (important: there are two spaces after `[!NOTE] ` to create a hard line break, see the ["about soft line breaks" chapter](#about-soft-line-breaks-support) for a more detailed explanation):
```md
> [!NOTE]
> I'm a note (created using a custom build)
```will yield the following HTML output:
```html
I'm a note (created using a custom build)
```#### v3 icons build
If you migrate from a previous version to v4 and want to keep the [twbs icons](https://icons.getbootstrap.com/), then you need to update your build (in the plugin options) like this:
```js
alerts: [
{
keyword: 'NOTE',
// bootstrap icon: info-circle
icon: '',
title: 'Note',
},
{
keyword: 'IMPORTANT',
// bootstrap icon: exclamation-square
icon: '',
title: 'Important',
},
{
keyword: 'WARNING',
// bootstrap icon: exclamation-triangle
icon: '',
title: 'Warning',
},
{
keyword: 'TIP',
// bootstrap icon: lightbulb
icon: '',
title: 'Tip',
},
{
keyword: 'CAUTION',
// bootstrap icon: exclamation-octagon
icon: '',
title: 'Caution',
},
],
```> [!TIP]
> you are not limited to using the [twbs icons](https://icons.getbootstrap.com/), if you prefer you could use the [lucide icons](https://lucide.dev/packages) or the [Material Symbols icons (google font)](https://fonts.google.com/icons) or the [Font Awesome Free icons](https://github.com/FortAwesome/Font-Awesome)## about "soft line breaks" support
> [!IMPORTANT]
> GitHub turns soft line breaks into hard line breaks by default, this plugin does NOT**option 1:** If you are using rehype-github-alerts, then **you need to add two spaces at the end of each line** if you want to have a line break (same as you would do for markdown outside of an alert), which is the [markdown syntax for a hard linebreak](https://daringfireball.net/projects/markdown/syntax#p), like so:
```md
> [!NOTE]
> you MUST add 2 spaces (to all 3 lines of this example, including the first one) to create line breaks
> if you don't want to manually add two spaces after each line, then you need to install the [remark-breaks](https://github.com/remarkjs/remark-breaks) plugin
```**option 2:** If you do NOT want to have to add two spaces manually after each line, then I recommend you install the plugin called [remark-breaks](https://github.com/remarkjs/remark-breaks), **remark-breaks** will mimic the behavior you experience on GitHub, by automatically turning a soft line break (when you hit `Enter` at the end of a line) into hard line breaks
As noted in the readme of the [remark-breaks](https://github.com/remarkjs/remark-breaks) package README, the purpose of the **remark-breaks** is to:
> remark-breaks turns enters into `
`s
> GitHub does this in a few places (comments, issues, PRs, and releases)## paragraphs separation
If in your markdown, you add two spaces at the end of a line, like this:
```md
> [!TIP]
> first paragraph
> second paragraph
```Then the resulting HTML will only have **one paragraph**, in which both parts of your text are separated by a `
` element:```html
(...)
first paragraph
second paragraph
```To create **two separate paragraphs** (no matter if you are using remark-breaks or not) you need to add an empty line into your markdown (same as you would do outside of a blockquote), like so:
```md
> [!TIP]
> first paragraph
>
> second paragraph
```Which will result in the following HTML:
```html
(...)
first paragraph
second paragraph
```## tests
For our tests we use the [test-runner that is built in node.js](https://nodejs.org/api/test.html)
All tests are located in the `/test` directory
### run tests
To run the tests use the following command:
```shell
npm run test
```> [!NOTE]
> this will build the plugin and then run the test coverage command### github token to create new test fixtures
To create new fixtures, we use a the [create-gfm-fixtures](https://github.com/wooorm/create-gfm-fixtures) package, to be able to use this package locally you will need a **personal GitHub access token**, because the tool will create gists on GitHub, insert the markdown from the test into the gist file, then the tool will read the converted HTML from GitHub and finally create a fixture HTML file. The fixture HTML file can then be used to compare the fixture HTML with the HTML output that gets created by this plugin. If the HTML from GitHub and the HTML produced by this plugin match then the test will pass, else it will fail.
To create a new token visit the GitHub ["New fine-grained personal access token"](https://github.com/settings/personal-access-tokens/new) page. There you need to create a new token, then under **Permissions** you need to set the **Gists** permission to read/write, then click on **Generate token** to create your new token.
When you have your token, make a copy of the `.env.example` and rename it to `.env`, then insert your token and save it
> [!TIP]
> if you new to GitHub tokens, then you may want to check out the [GitHub documentation "Creating a fine-grained personal access token"](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token)## types
If you use typescript and intend to edit the options, for example to create custom alerts, then you may want to use the types provided by this library:
```ts
import { rehypeGithubAlerts, IOptions } from 'rehype-github-alerts'const myOptions: IOptions = {
alerts: [
{
keyword: 'MY_ALERT',
icon: '',
title: 'My Alert',
},
],
}
```If your configuration file is written in javascript, then you can use the types likes this:
on top of your file add this jsdoc **typedef** at the beginning of the file:
```js
/**
* @typedef {import('rehype-github-alerts').IOptions} IOptions
*/
```and then in your code use the rehype-github-alerts type by placing a jsdoc @type tag over the options, like so:
```js
/** @type { IOptions } */
const rehypeGithubAlertsOptions = {
supportLegacy: false,
}
```here is a full example of a next.js next.config.mjs configuration file
```mjs
/**
* @typedef {import('rehype-github-alerts').IOptions} IOptions
*/import WithMDX from '@next/mdx'
import remarkBreaks from 'remark-breaks'
import remarkGfm from 'remark-gfm'
import { rehypeGithubAlerts } from 'rehype-github-alerts'const nextConfig = (/*phase*/) => {
// https://github.com/remarkjs/remark-gfm
// If you use remark-gfm, you'll need to use next.config.mjs
// as the package is ESM only
const remarkGfmOptions = {
singleTilde: false,
}// https://github.com/chrisweb/rehype-github-alerts
/** @type { IOptions } */
const rehypeGithubAlertsOptions = {
supportLegacy: false,
}const withMDX = WithMDX({
extension: /\.mdx?$/,
options: {
remarkPlugins: [remarkBreaks, [remarkGfm, remarkGfmOptions]],
rehypePlugins: [[rehypeGithubAlerts, rehypeGithubAlertsOptions]],
},
})/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
// experimental use rust compiler for MDX
// as of now (07.10.2023) there is no support for rehype plugins
// this is why it is currently disabled
mdxRs: false,
},
// configure pageExtensions to include md and mdx
pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'],
}return withMDX(nextConfig)
}
export default nextConfig
```The Next.js configuration example above assumes that you have installed the packages [@next/mdx](https://www.npmjs.com/package/@next/mdx), [@mdx-js/loader](https://www.npmjs.com/package/@mdx-js/loader), [remark-breaks](https://www.npmjs.com/package/remark-breaks), [remark-gfm](https://www.npmjs.com/package/remark-gfm) and [rehype-github-alerts](https://www.npmjs.com/package/rehype-github-alerts)
## legacy syntax
as of 14 November 2023 GitHub has removed support for legacy syntax, the legacy syntax is supported by this plugin but as of now turned off by default
legacy markdown (mdx) syntax:
```md
> **!Note**
> I'm a note :wave:> **!Important**
> I'm important> **!Warning**
> I'm a warning
```you can turn **ON** legacy support via the options like so:
```js
const myRehypeGithubAlertsOptions = {
supportLegacy: true,
}
```## bug reports / issues
if you find a bug, please open an issue in the [rehype-github-alerts issues page on github](https://github.com/chrisweb/rehype-github-alerts/issues), try to describe the bug you encountered as best as you can and if possible add some examples of the markdown / mdx content or code that you used when you found the bug, I or a contributor will try to look into it asap
## feedback / ideas
If you have an idea to improve this project please use the ["NEW Feature Request"](https://github.com/chrisweb/rehype-github-alerts/issues/new/choose) issue template or if you have any feedback about this package you may want to post it in the [rehype discussion about this plugin](https://github.com/orgs/rehypejs/discussions/157)
## contributing
PRs are always welcome 😉
To get started, please check out the [CONTRIBUTING.md](CONTRIBUTING.md) guide of this project
## alternatives
* One alternative to this package if you want to have github like alerts but do it with a **remark plugin** instead of a **rehype plugin** is [remark-github-beta-blockquote-admonitions](https://www.npmjs.com/package/remark-github-beta-blockquote-admonitions)
* Another alternative is to use the [rehype-github-alert](https://www.npmjs.com/package/rehype-github-alert) (the name is similar to this plugin but minus the "s"), it is the official package from the [rehype-github](https://github.com/rehypejs/rehype-github) repository
* A different approach is being used in [rehype-callouts](https://github.com/lin-stephanie/rehype-callouts), which is NOT about mimicking GitHub (even though the blockquote usage is similar) but the package has nice features like nestable callouts and also custom callout titles## additional packages
if you use this package, there are other packages you might want to install too, for example:
- [remark-gfm](https://github.com/remarkjs/remark-gfm), adds support for [GitHub Flavored Markdown (GFM)](https://github.github.com/gfm/) (autolink literals, footnotes, strikethrough, tables, task lists)
- [remark-breaks](https://github.com/remarkjs/remark-breaks), turns soft line endings (enters) into hard breaks (`
`s). GitHub does this in a few places (comments, issues, PRs, and releases)