Gathering detailed insights and metrics for rehype-remark
Gathering detailed insights and metrics for rehype-remark
Gathering detailed insights and metrics for rehype-remark
Gathering detailed insights and metrics for rehype-remark
plugin to transform from HTML (rehype) to Markdown (remark)
npm install rehype-remark
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
82 Stars
115 Commits
8 Forks
9 Watching
1 Branches
10 Contributors
Updated on 22 Oct 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-6.9%
13,743
Compared to previous day
Last week
-2.8%
82,122
Compared to previous week
Last month
19.7%
352,038
Compared to previous month
Last year
62.7%
2,736,959
Compared to previous year
rehype plugin that turns HTML into markdown to support remark.
This package is a unified (rehype) plugin that switches from rehype (the
HTML ecosystem) to remark (the markdown ecosystem).
It does this by transforming the current HTML (hast) syntax tree into a markdown
(mdast) syntax tree.
rehype plugins deal with hast and remark plugins deal with mdast, so plugins
used after rehype-remark
have to be remark plugins.
The reason that there are different ecosystems for markdown and HTML is that
turning markdown into HTML is, while frequently needed, not the only purpose of
markdown.
Checking (linting) and formatting markdown are also common use cases for
remark and markdown.
There are several aspects of markdown that do not translate 1-to-1 to HTML.
In some cases markdown contains more information than HTML: for example, there
are several ways to add a link in markdown (as in, autolinks: <https://url>
,
resource links: [label](url)
, and reference links with definitions:
[label][id]
and [id]: url
).
In other cases HTML contains more information than markdown: there are many
tags, which add new meaning (semantics), available in HTML that aren’t available
in markdown.
If there was just one AST, it would be quite hard to perform the tasks that
several remark and rehype plugins currently do.
unified is a project that transforms content with abstract syntax trees (ASTs). remark adds support for markdown to unified. rehype adds support for HTML to unified. mdast is the markdown AST that remark uses. hast is the HTML AST that rehype uses. This is a rehype plugin that transforms hast into mdast to support remark.
This project is useful when you want to turn HTML to markdown.
The remark plugin remark-rehype
does the inverse of this
plugin.
It turns markdown into HTML.
This package is ESM only. In Node.js (version 16+), install with npm:
1npm install rehype-remark
In Deno with esm.sh
:
1import rehypeRemark from 'https://esm.sh/rehype-remark@10'
In browsers with esm.sh
:
1<script type="module"> 2 import rehypeRemark from 'https://esm.sh/rehype-remark@10?bundle' 3</script>
Say we have the following module example.js
:
1import rehypeParse from 'rehype-parse' 2import rehypeRemark from 'rehype-remark' 3import remarkStringify from 'remark-stringify' 4import {fetch} from 'undici' 5import {unified} from 'unified' 6 7const response = await fetch('https://example.com') 8const text = await response.text() 9 10const file = await unified() 11 .use(rehypeParse) 12 .use(rehypeRemark) 13 .use(remarkStringify) 14 .process(text) 15 16console.log(String(file))
Now running node example.js
yields:
1# Example Domain 2 3This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission. 4 5[More information...](https://www.iana.org/domains/example)
This package exports no identifiers.
The default export is rehypeRemark
.
unified().use(rehypeRemark[, destination][, options])
Turn HTML into markdown.
Transform (Transformer
).
rehypeRemark
are remark plugins (mutate mode)👉 Note: It’s highly unlikely that you want to pass a
processor
.
Options
Configuration (TypeScript type).
checked
(string
, default: '[x]'
)
— value to use for a checked checkbox or radio inputdocument
(boolean
, default: true
)
— whether the given tree represents a complete document; when the tree
represents a complete document, then things are wrapped in paragraphs
when needed, and otherwise they’re left as-ishandlers
(Record<string, Handle>
, optional)
— object mapping tag names to functions handling the corresponding
elements; merged into the defaults; see
Handle
in hast-util-to-mdast
newlines
(boolean
, default: false
)
— keep line endings when collapsing whitespace; the default collapses to a
single spacenodeHandlers
(Record<string, NodeHandle>
, optional)
— object mapping node types to functions handling the corresponding nodes;
merged into the defaults; see
NodeHandle
in hast-util-to-mdast
quotes
(Array<string>
, default: ['"']
)
— list of quotes to use; each value can be one or two characters; when two,
the first character determines the opening quote and the second the closing
quote at that level; when one, both the opening and closing quote are that
character; the order in which the preferred quotes appear determines which
quotes to use at which level of nesting; so, to prefer ‘’
at the first
level of nesting, and “”
at the second, pass ['‘’', '“”']
; if <q>
s
are nested deeper than the given amount of quotes, the markers wrap around:
a third level of nesting when using ['«»', '‹›']
should have double
guillemets, a fourth single, a fifth double again, etcunchecked
(string
, default: '[ ]'
)
— value to use for an unchecked checkbox or radio inputIt’s possible to exclude something from within HTML when turning it into
markdown, by wrapping it in an element with a data-mdast
attribute set to
'ignore'
.
For example:
1<p><strong>Importance</strong> and <em data-mdast="ignore">emphasis</em>.</p>
Yields:
1**Importance** and .
It’s also possible to pass a handler to ignore nodes, or create your own plugin that uses more advanced filters.
The goal of this project is to map HTML to plain and readable markdown.
That means that certain elements are ignored (such as <svg>
) or “downgraded”
(such as <video>
to links).
You can change this by passing handlers.
Say we have the following file example.html
:
1<p> 2 Some text with 3 <svg viewBox="0 0 1 1" width="1" height="1"><rect fill="black" x="0" y="0" width="1" height="1" /></svg> 4 a graphic… Wait is that a dead pixel? 5</p>
And our module example.js
looks as follows:
1/** 2 * @typedef {import('mdast').Html} Html 3 */ 4 5import {toHtml} from 'hast-util-to-html' 6import rehypeParse from 'rehype-parse' 7import rehypeRemark from 'rehype-remark' 8import remarkStringify from 'remark-stringify' 9import {read} from 'to-vfile' 10import {unified} from 'unified' 11 12const file = await unified() 13 .use(rehypeParse, {fragment: true}) 14 .use(rehypeRemark, { 15 handlers: { 16 svg(state, node) { 17 /** @type {Html} */ 18 const result = {type: 'html', value: toHtml(node)} 19 state.patch(node, result) 20 return result 21 } 22 } 23 }) 24 .use(remarkStringify) 25 .process(await read('example.html')) 26 27console.log(String(file))
Now running node example.js
yields:
1Some text with <svg viewBox="0 0 1 1" width="1" height="1"><rect fill="black" x="0" y="0" width="1" height="1"></rect></svg> a graphic… Wait is that a dead pixel?
This package is fully typed with TypeScript.
It exports the additional type Options
.
More advanced types are exposed from hast-util-to-mdast
.
Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, rehype-remark@^10
,
compatible with Node.js 16.
This plugin works with unified
version 6+, rehype-parse
version 3+ (used in
rehype
version 5), and remark-stringify
version 3+ (used in remark
version 7).
Use of rehype-remark
is safe by default.
remark-rehype
— remark plugin to turn markdown into HTMLremark-retext
— remark plugin to support retextrehype-retext
— rehype plugin to support retextSee 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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
security policy file detected
Details
Reason
Found 3/30 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More