From 0dac9aadc16630cee7d6802d1c63ddf0b42fe7c3 Mon Sep 17 00:00:00 2001 From: Julien Barbay Date: Fri, 14 Jul 2023 18:28:19 +0700 Subject: [PATCH] misc: migrate remark-code-dataset to here --- remark-code-dataset/README.md | 27 ++++++++++++++++++++++++ remark-code-dataset/index.js | 33 ++++++++++++++++++++++++++++++ remark-code-dataset/package.json | 19 +++++++++++++++++ remark-code-dataset/yarn.lock | 35 ++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 remark-code-dataset/README.md create mode 100644 remark-code-dataset/index.js create mode 100644 remark-code-dataset/package.json create mode 100644 remark-code-dataset/yarn.lock diff --git a/remark-code-dataset/README.md b/remark-code-dataset/README.md new file mode 100644 index 0000000..b48f362 --- /dev/null +++ b/remark-code-dataset/README.md @@ -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
hello world
; +} +``` + +↓ + +```html + + + +``` + +## 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]` diff --git a/remark-code-dataset/index.js b/remark-code-dataset/index.js new file mode 100644 index 0000000..9b58515 --- /dev/null +++ b/remark-code-dataset/index.js @@ -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.`) + } + } + }) + } +} diff --git a/remark-code-dataset/package.json b/remark-code-dataset/package.json new file mode 100644 index 0000000..5335684 --- /dev/null +++ b/remark-code-dataset/package.json @@ -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 ", + "license": "MIT", + "main": "index.js", + "scripts": { + "deploy": "npm publish --access public" + }, + "dependencies": { + "json5": "^2.2.3", + "unist-util-visit": "^4.1.1" + } +} diff --git a/remark-code-dataset/yarn.lock b/remark-code-dataset/yarn.lock new file mode 100644 index 0000000..e66fd81 --- /dev/null +++ b/remark-code-dataset/yarn.lock @@ -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"