rehype-github
November 27, 2024 ยท View on GitHub
rehype plugins that match how GitHub transforms markdown on their site.
๐ Note: work in progress. some things are not yet finished.
Contents
- What is this?
- When should I use this?
- Install
- Packages
- Examples
- Types
- Compatibility
- Security
- Related
- Contribute
- Notice
- License
What is this?
This project is a monorepo that contains many packages that can be used together or on their own, with defaults to match GitHub or with configuration to match your site, to process user content for safe use on the web.
When should I use this?
You can use these tools when you need to:
- match GitHub in how it transforms markdown to HTML
- build similar pipelines to bring user content to the masses
Install
Currently, install and use each package manually.
Packages
remark-github-breakโ turn normal line endings into hard breaks (comments)remark-github-yaml-metadataโ show frontmatter as a table (files)rehype-github-alertโ enhance alerts (everywhere)rehype-github-colorโ enhance code for colors (comments)rehype-github-dirโ adddir=autoto elements (everywhere)rehype-github-emojiโ enhance emoji and gemoji (everywhere)rehype-github-headingโ enhance headings (files)rehype-github-highlightโ perform syntax highlighting on code (to do, meanwhile userehype-starry-night)rehype-github-imageโ enhance images (everywhere)rehype-github-linkโ enhance links (everywhere)rehype-github-mentionโ enhance mentions (to do)rehype-github-notranslateโ enhance raw text withnotranslate(comments)rehype-github-referenceโ enhance references (to do)rehype-github-sanitizeโ clean dangerous HTML (to do, meanwhile userehype-sanitize)
GitHub additionally includes client side code to enhance certain code blocks by evaluating their contents. This behavior is performed by:
viewscreen-geojsonโ viewscreen component for geojson and topojsonviewscreen-mermaidโ viewscreen component for mermaidviewscreen-stlโ viewscreen component for STL (3D geometry)
See example/viewscreen/ on how to use them together.
To do:
- investigate task lists
- investigate mathjax
- investigate port of treelights
- investigate linking to labels, issues, pulls, files, lines in files
Examples
Example: comments
A pipeline that gets close to how GitHub transforms comments:
import rehypeGithubAlert from 'rehype-github-alert'
import rehypeGithubColor from 'rehype-github-color'
import rehypeGithubDir from 'rehype-github-dir'
import rehypeGithubEmoji from 'rehype-github-emoji'
import rehypeGithubImage from 'rehype-github-image'
import rehypeGithubLink from 'rehype-github-link'
import rehypeGithubNoTranslate from 'rehype-github-notranslate'
import rehypeRaw from 'rehype-raw'
import rehypeSanitize from 'rehype-sanitize'
import rehypeStringify from 'rehype-stringify'
import remarkGithubBreak from 'remark-github-break'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import {unified} from 'unified'
const file = await unified()
.use(remarkParse)
.use(remarkGithubBreak)
.use(remarkRehype, {allowDangerousHtml: true})
.use(rehypeRaw)
.use(rehypeGithubAlert)
.use(rehypeGithubColor)
.use(rehypeGithubDir)
.use(rehypeGithubEmoji)
.use(rehypeGithubImage)
.use(rehypeGithubLink)
.use(rehypeGithubNoTranslate)
.use(rehypeSanitize)
.use(rehypeStringify)
.process('hi!')
console.log(String(file))
Yields:
<p dir="auto">hi!</p>
Example: files
A pipeline that gets close to how GitHub transforms files:
import rehypeGithubAlert from 'rehype-github-alert'
import rehypeGithubDir from 'rehype-github-dir'
import rehypeGithubEmoji from 'rehype-github-emoji'
import rehypeGithubHeading from 'rehype-github-heading'
import rehypeGithubImage from 'rehype-github-image'
import rehypeGithubLink from 'rehype-github-link'
import rehypeRaw from 'rehype-raw'
import rehypeSanitize from 'rehype-sanitize'
import rehypeStringify from 'rehype-stringify'
import remarkGithubYamlMetadata from 'remark-github-yaml-metadata'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import {unified} from 'unified'
const file = await unified()
.use(remarkParse)
.use(remarkGithubYamlMetadata)
.use(remarkRehype, {allowDangerousHtml: true})
.use(rehypeRaw)
.use(rehypeGithubAlert)
.use(rehypeGithubDir)
.use(rehypeGithubEmoji)
.use(rehypeGithubHeading)
.use(rehypeGithubImage)
.use(rehypeGithubLink)
.use(rehypeSanitize)
.use(rehypeStringify)
.process('hi!')
console.log(String(file))
Yields:
<p dir="auto">hi!</p>
Types
These packages are fully typed with TypeScript.
Compatibility
Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 16+. Our projects sometimes work with older versions, but this is not guaranteed.
Security
Be wary of user input and use rehype-sanitize.
Related
rehype-sanitizeโ sanitize HTMLrehype-starry-nightโ apply syntax highlighting to code withstarry-night
Contribute
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.
Notice
This project is not affiliated with GitHub.
License
MIT ยฉ Titus Wormer