Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
@beoe/rehype-code-hook
Advanced tools
Rehype plugin to make it easier to write custom processors for code e.g.:
Block-code:
<pre><code class="language-js">const x = 1;</code></pre>
Inline-code:
<code>x</code>
In markdown it corresponds to:
Block-code:
```js
const x = 1;
```
Inline-code:
`x`
This plugin is usefull if you want to create rehype plugin to:
@shikijs/rehype
, rehype-prism
, rehype-highlight
etc.rehype-mermaid
rehype-color-chips
Basic example looks like this:
import { rehypeCodeHook } from "@beoe/rehype-code-hook";
import { generateSvg } from "./generateSvg.js";
export const rehypeExampleDiagram = (options = {}) => {
return rehypeCodeHook({
...options,
code: ({ code }) => generateSvg(code),
});
};
If you have code like this (it is more clear in Markdown notation, but plugin actually works at rehype level):
```js {1,10}
const x = 1;
```
code
callback would be called with:
{
code: "const x = 1;\n",
inline: false,
language: "js",
meta: "{1,10}"
}
If you have code like this:
`const x = 1;`
code
callback would be called with:
{
code: "const x = 1;",
inline: true,
language: undefined,
meta: undefined
}
Now it is time to render your thing:
code
, inline
, language
, meta
and if this is not the block you are looking for you can return undefined
- and block would be unchangedstring
(for example SVG or HTML), you can return HAST fragment or you can return promise of string or HAST
You can configure your plugin to be called only for specific cases, for example:
example
: rehypeCodeHook({language: "example",..})
rehypeCodeHook({inline: true,..})
To enable caching you need to pass Map
-like storage:
rehypeCodeHook({ code, cache: new Map(), hashTostring: true });
I checked it with @beoe/cache, but it suppose to work with any storage that has Map
like interface. You may pass additional salt
param to reset cache, for example when configuration of your plugin changed.
If you need to parse meta
param you can use, for example:
If you want to use rehype plugin for code
elements in Astro you need to disable built-in highlighter and then make sure it will appear after your rehype plugin
import { rehypeShiki, markdownConfigDefaults } from "@astrojs/markdown-remark";
export default defineConfig({
markdown: {
syntaxHighlight: false,
rehypePlugins: [
// you rehype plugin goes here
// re-enable default Astro code highlighter
[rehypeShiki, markdownConfigDefaults.shikiConfig],
],
},
});
Important use v0.22+
export default defineConfig({
markdown: {
rehypePlugins: [
// you rehype plugin goes here
// after you can insert code highlighter
],
},
});
My main use-case is to inline SVGs, but if you want to produce images instead you can use data URLs. Something like this:
{
code: () =>
`<img src="data:image/png;base64,${dataBuffer.toString("base64")}">`;
}
If you want to convert SVG data URL use mini-svg-data-uri
or similar.
I didn't investigate how to render images aside with vfile.
FAQs
rehype plugin for code fences
The npm package @beoe/rehype-code-hook receives a total of 57 weekly downloads. As such, @beoe/rehype-code-hook popularity was classified as not popular.
We found that @beoe/rehype-code-hook demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.