remark-code-title
Version:
remark plugin to add titles to code blocks
58 lines (40 loc) • 2.07 kB
Markdown
<img alt="preview badge" src="https://img.shields.io/npm/dm/remark-code-title?color=informational&logo=npm">
Remark plugin to add a title to code blocks.
Inspired by:
- [gatsby-remark-code-titles](https://github.com/DSchau/gatsby-remark-code-titles)
- [remark-code-titles](https://github.com/mottox2/remark-code-titles) [currently not maintained]
| Astro | Next.js |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| [  ](https://stackblitz.com/edit/astro-remark-code-title?file=README.md) | [  ](https://stackblitz.com/edit/github-sf5q11?file=README.md) |
```bash
npm install remark-code-title
```
Given this markdown file `example.md`:
````markdown
```js title="example.js"
console.log("Hello World");
```
````
And this script, `example.js`, using `remark-code-title`:
```js
import codeTitle from "remark-code-title";
import html from "remark-html";
import { read } from "to-vfile";
import { remark } from "remark";
const processor = remark().use(codeTitle).use(html, { sanitize: false });
const markdown = await read("example.md");
const result = await processor.process(markdown);
```
Running `node example.js` yields:
```html
<h1>Example</h1>
<div data-remark-code-title data-language="js">example.js</div>
<pre>
<code>console.log("Hello World");</code>
</pre>
```