From e47deddf03faa176fa2bbbd2543d95cb64fb425f Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Fri, 14 May 2021 23:14:14 +0800 Subject: [PATCH] feat: mark code element. --- __tests__/index.test.ts | 8 ++++---- src/index.ts | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 94353e5..d1eb5e2 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -11,7 +11,7 @@ const mrkStr = "\n```js\nconsole.log('')\n```" describe('rehype-attr test case', () => { it('default options="data"', async () => { - const expected = `\n
console.log('')\n
`
+ const expected = `\nconsole.log('')\n
`
const htmlStr = unified()
.use(remarkParse)
.use(remark2rehype, { allowDangerousHtml: true })
@@ -23,7 +23,7 @@ describe('rehype-attr test case', () => {
expect(htmlStr).toEqual(expected);
});
it('options="string"', async () => {
- const expected = `\nconsole.log('')\n
`
+ const expected = `\nconsole.log('')\n
`
const htmlStr = unified()
.use(remarkParse)
.use(remark2rehype, { allowDangerousHtml: true })
@@ -37,7 +37,7 @@ describe('rehype-attr test case', () => {
it('options="string" - Multiple value settings', async () => {
const markdown = "\n```js\nconsole.log('')\n```\n\n```js\nconsole.log('')\n```\n\n```js\nconsole.log('')\n```\n"
- const expected = `\nconsole.log('')\n
\nconsole.log('')\n
\n\nconsole.log('')\n
`
+ const expected = `\nconsole.log('')\n
\nconsole.log('')\n
\n\nconsole.log('')\n
`
const htmlStr = unified()
.use(remarkParse)
.use(remark2rehype, { allowDangerousHtml: true })
@@ -53,7 +53,7 @@ describe('rehype-attr test case', () => {
{
title: 'options="attr" - Code',
markdown: '\n```js\nconsole.log("")\n```',
- expected: '\nconsole.log("")\n
',
+ expected: '\nconsole.log("")\n
',
},
{
title: 'options="attr" - Emphasis ',
diff --git a/src/index.ts b/src/index.ts
index 860a807..5905151 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -60,6 +60,7 @@ const rehypeAttrs: Plugin<[RehypeAttrsOptions?]> = (options): MdastTransformer =
if (node.tagName === 'pre' && codeNode && codeNode.tagName === 'code' && Array.isArray(parent.children) && parent.children.length > 1) {
const attr = getComment(parent.children, index, true)
if (Object.keys(attr).length > 0) {
+ node.properties = { ...(node.properties as any), ...{ 'data-type': 'rehyp' } }
codeNode.properties = propertiesHandle(codeNode.properties, attr, opts.properties)
}
}