Skip to content

Commit

Permalink
misc: migrate remark-code-dataset to here
Browse files Browse the repository at this point in the history
  • Loading branch information
y-nk committed Jul 14, 2023
1 parent 5358eb9 commit 0dac9aa
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
27 changes: 27 additions & 0 deletions remark-code-dataset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# remark-code-dataset

## What

It's a remark plugin which adds all metadata passed to a code block as data attributes for exploitation in rehype or client-side.

## Demo

```jsx group=demo file=index.jsx
export default function App() {
return <div>hello world</div>;
}
```


```html
<code class="language-jsx" data-group="demo" data-file="index.jsx">
<!-- some html -->
</code>
```

## How

1. Install the plugin with `npm i remark-code-dataset`
2. Import the plugin with `import remarkCodeDataset from 'remark-code-dataset'
3. Add to your remark plugin stack `[remarkCodeDataset]`
33 changes: 33 additions & 0 deletions remark-code-dataset/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { visit } from "unist-util-visit";
import json5 from 'json5';

export default function remarkCodeDataset(options) {
return function transform(tree, file) {
visit(tree, 'code', function(node) {
if (!node.meta) return

try {
const attrs = json5.parse(node.meta);

const {
exclude = [],
include = Object.keys(attrs)
} = options ?? {}

node.data ??= {}

node.data.hProperties = Object.fromEntries(
Object.entries(attrs)
.filter(([k]) => !exclude.includes(k))
.filter(([k]) => include.includes(k))
.map(([k, v]) => [`data-${k}`, v])
)
} catch {
if (process.env.NODE_ENV !== 'production') {
const [filename] = file.history
console.error(`[remark-code-dataset] Invalid JSON5 structure in ${filename}: ${node.meta}; ignoring.`)
}
}
})
}
}
19 changes: 19 additions & 0 deletions remark-code-dataset/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "remark-code-dataset",
"type": "module",
"version": "2.1.0",
"description": "adds codeblock meta as html dataset",
"homepage": "https://github.com/y-nk/nonorepo/tree/main/remark-code-dataset",
"private": false,
"keywords": ["unified", "remark", "code"],
"author": "Julien Barbay <[email protected]>",
"license": "MIT",
"main": "index.js",
"scripts": {
"deploy": "npm publish --access public"
},
"dependencies": {
"json5": "^2.2.3",
"unist-util-visit": "^4.1.1"
}
}
35 changes: 35 additions & 0 deletions remark-code-dataset/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@types/unist@^2.0.0":
version "2.0.6"
resolved "https://registry.yarnpkg.com/@types/unist/-/unist-2.0.6.tgz#250a7b16c3b91f672a24552ec64678eeb1d3a08d"
integrity sha512-PBjIUxZHOuj0R15/xuwJYjFi+KZdNFrehocChv4g5hu6aFroHue8m0lBP0POdK2nKzbw0cgV1mws8+V/JAcEkQ==

json5@^2.2.3:
version "2.2.3"
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283"
integrity sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==

unist-util-is@^5.0.0:
version "5.1.1"
resolved "https://registry.yarnpkg.com/unist-util-is/-/unist-util-is-5.1.1.tgz#e8aece0b102fa9bc097b0fef8f870c496d4a6236"
integrity sha512-F5CZ68eYzuSvJjGhCLPL3cYx45IxkqXSetCcRgUXtbcm50X2L9oOWQlfUfDdAf+6Pd27YDblBfdtmsThXmwpbQ==

unist-util-visit-parents@^5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/unist-util-visit-parents/-/unist-util-visit-parents-5.1.1.tgz#868f353e6fce6bf8fa875b251b0f4fec3be709bb"
integrity sha512-gks4baapT/kNRaWxuGkl5BIhoanZo7sC/cUT/JToSRNL1dYoXRFl75d++NkjYk4TAu2uv2Px+l8guMajogeuiw==
dependencies:
"@types/unist" "^2.0.0"
unist-util-is "^5.0.0"

unist-util-visit@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/unist-util-visit/-/unist-util-visit-4.1.1.tgz#1c4842d70bd3df6cc545276f5164f933390a9aad"
integrity sha512-n9KN3WV9k4h1DxYR1LoajgN93wpEi/7ZplVe02IoB4gH5ctI1AaF2670BLHQYbwj+pY83gFtyeySFiyMHJklrg==
dependencies:
"@types/unist" "^2.0.0"
unist-util-is "^5.0.0"
unist-util-visit-parents "^5.1.1"

0 comments on commit 0dac9aa

Please sign in to comment.