Skip to content

Commit 91ca402

Browse files
committedSep 28, 2022
feat: add codeBlockParames option.
1 parent 07bb0ea commit 91ca402

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed
 

‎README.md

+7
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ const htmlStr = rehype()
265265
```
266266
267267
Output:
268+
268269
```html
269270
<h1 style="color:pink;">This is a title</h1>
270271
```
@@ -306,6 +307,12 @@ Output:
306307
> Default Value: `data`
307308
> Value: `data`, `string`, `attr`
308309
310+
### `codeBlockParames`
311+
312+
Code block passing parameters
313+
314+
> Default Value: `true`
315+
309316
## Related
310317

311318
- [`rehype-video`](https://github.com/jaywcjlove/rehype-video) Add improved video syntax: links to `.mp4` and `.mov` turn into videos.

‎src/index.ts

+14-15
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ export type RehypeAttrsOptions = {
1111
* text
1212
* <!--rehype:title=Rehype Attrs&abc=2-->
1313
* ```
14-
*
15-
* ⇣⇣⇣⇣⇣⇣
16-
*
14+
* 👇👇👇👇👇
1715
* ```html
1816
* <p data-config="data-config='[object Object]'">text</p>
1917
* ```
@@ -25,7 +23,7 @@ export type RehypeAttrsOptions = {
2523
* <!--rehype:title=Rehype Attrs-->
2624
* ```
2725
*
28-
* ⇣⇣⇣⇣⇣⇣
26+
* 👇👇👇👇👇
2927
*
3028
* ```html
3129
* <p data-config="{&#x22;title&#x22;:&#x22;Rehype Attrs&#x22;,&#x22;rehyp&#x22;:true}">text</p>
@@ -37,31 +35,32 @@ export type RehypeAttrsOptions = {
3735
* text
3836
* <!--rehype:title=Rehype Attrs-->
3937
* ```
40-
* ⇣⇣⇣⇣⇣⇣
38+
* 👇👇👇👇👇
4139
* ```html
4240
* <p title="Rehype Attrs">text</p>
4341
* ```
42+
* @default `data`
4443
*/
45-
properties: 'data' | 'string' | 'attr';
46-
}
47-
48-
const defaultOptions: RehypeAttrsOptions = {
49-
properties: 'data',
44+
properties?: 'data' | 'string' | 'attr';
45+
/**
46+
* Code block passing parameters
47+
*/
48+
codeBlockParames?: boolean;
5049
}
5150

52-
const rehypeAttrs: Plugin<[RehypeAttrsOptions?], Root> = (options) => {
53-
const opts = { ...defaultOptions, ...options }
51+
const rehypeAttrs: Plugin<[RehypeAttrsOptions?], Root> = (options = {}) => {
52+
const { properties = 'data', codeBlockParames = true } = options;
5453
return (tree) => {
5554
visit(tree, 'element', (node, index, parent) => {
56-
if (node.tagName === 'pre' && node && Array.isArray(node.children) && parent && Array.isArray(parent.children) && parent.children.length > 1) {
55+
if (codeBlockParames && node.tagName === 'pre' && node && Array.isArray(node.children) && parent && Array.isArray(parent.children) && parent.children.length > 1) {
5756
const firstChild = node.children[0] as Element;
5857
if (firstChild && firstChild.tagName === 'code' && typeof index === 'number') {
5958
const child = prevChild(parent.children as Literal[], index);
6059
if (child) {
6160
const attr = getCommentObject(child);
6261
if (Object.keys(attr).length > 0) {
6362
node.properties = { ...node.properties, ...{ 'data-type': 'rehyp' } }
64-
firstChild.properties = propertiesHandle(firstChild.properties, attr, opts.properties) as Properties
63+
firstChild.properties = propertiesHandle(firstChild.properties, attr, properties) as Properties
6564
}
6665
}
6766
}
@@ -72,7 +71,7 @@ const rehypeAttrs: Plugin<[RehypeAttrsOptions?], Root> = (options) => {
7271
if (child) {
7372
const attr = getCommentObject(child as Comment)
7473
if (Object.keys(attr).length > 0) {
75-
node.properties = propertiesHandle(node.properties, attr, opts.properties) as Properties
74+
node.properties = propertiesHandle(node.properties, attr, properties) as Properties
7675
}
7776
}
7877
}

‎test/index.test.ts

+27
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ describe('rehype-attr function test case', () => {
7676
})
7777

7878
describe('rehype-attr test case', () => {
79+
it('default codeBlockParames=false', async () => {
80+
const expected = `<!--rehype:title=Rehype Attrs-->\n<pre><code class="language-js">console.log('')\n</code></pre>`
81+
const htmlStr = unified()
82+
.use(remarkParse)
83+
.use(remark2rehype, { allowDangerousHtml: true })
84+
.use(rehypeRaw)
85+
.use(rehypeAttrs, { codeBlockParames: false })
86+
.use(stringify)
87+
.processSync(mrkStr)
88+
.toString()
89+
expect(htmlStr).toEqual(expected);
90+
});
7991
it('default options="data"', async () => {
8092
const expected = `<!--rehype:title=Rehype Attrs-->\n<pre data-type="rehyp"><code class="language-js" data-config="[object Object]">console.log('')\n</code></pre>`
8193
const htmlStr = unified()
@@ -297,6 +309,11 @@ describe('rehype-attr test case', () => {
297309
markdown: '#### This is a title\n<!--rehype:style=background-color: rgb(235 217 78/var(--bg-opacity));-->',
298310
expected: '<h4 style="background-color: rgb(235 217 78/var(--bg-opacity));">This is a title</h4>\n<!--rehype:style=background-color: rgb(235 217 78/var(--bg-opacity));-->',
299311
},
312+
{
313+
title: 'options="attr" - test identifier',
314+
markdown: '#### This is a title\n<!--rehype:className=test test2-->',
315+
expected: '<h4 class="test test2">This is a title</h4>\n<!--rehype:className=test test2-->',
316+
},
300317
].forEach((data, idx) => {
301318
it(data.title, async () => {
302319
const htmlStr = unified()
@@ -334,6 +351,16 @@ describe('rehype-attr test case', () => {
334351

335352

336353
[
354+
{
355+
title: 'options="attr" - <p>',
356+
markdown: '<p class="hello">text</p><!--rehype:className=text-->',
357+
expected: '<p class="text">text</p><!--rehype:className=text-->',
358+
},
359+
{
360+
title: 'options="attr" - <p> ???????????xxxxxxx',
361+
markdown: '<p class="hello">text</p><!--rehype:class=text-->',
362+
expected: '<p class="hello" class="text">text</p><!--rehype:class=text-->',
363+
},
337364
{
338365
title: 'options="attr" - <p>',
339366
markdown: '<p>text</p><!--rehype:id=text-->',

0 commit comments

Comments
 (0)
Please sign in to comment.