Skip to content

Commit

Permalink
feat: mark code element.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed May 14, 2021
1 parent a617fc5 commit e47dedd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
8 changes: 4 additions & 4 deletions __tests__/index.test.ts
Expand Up @@ -11,7 +11,7 @@ const mrkStr = "<!--rehype:title=Rehype Attrs-->\n```js\nconsole.log('')\n```"

describe('rehype-attr test case', () => {
it('default options="data"', async () => {
const expected = `<!--rehype:title=Rehype Attrs-->\n<pre><code class="language-js" data-config="[object Object]">console.log('')\n</code></pre>`
const expected = `<!--rehype:title=Rehype Attrs-->\n<pre data-type="rehyp"><code class="language-js" data-config="[object Object]">console.log('')\n</code></pre>`
const htmlStr = unified()
.use(remarkParse)
.use(remark2rehype, { allowDangerousHtml: true })
Expand All @@ -23,7 +23,7 @@ describe('rehype-attr test case', () => {
expect(htmlStr).toEqual(expected);
});
it('options="string"', async () => {
const expected = `<!--rehype:title=Rehype Attrs-->\n<pre><code class="language-js" data-config="{&#x22;title&#x22;:&#x22;Rehype Attrs&#x22;,&#x22;rehyp&#x22;:true}">console.log('')\n</code></pre>`
const expected = `<!--rehype:title=Rehype Attrs-->\n<pre data-type="rehyp"><code class="language-js" data-config="{&#x22;title&#x22;:&#x22;Rehype Attrs&#x22;,&#x22;rehyp&#x22;:true}">console.log('')\n</code></pre>`
const htmlStr = unified()
.use(remarkParse)
.use(remark2rehype, { allowDangerousHtml: true })
Expand All @@ -37,7 +37,7 @@ describe('rehype-attr test case', () => {

it('options="string" - Multiple value settings', async () => {
const markdown = "<!--rehype:title=Rehype Attrs-->\n```js\nconsole.log('')\n```\n\n```js\nconsole.log('')\n```\n<!--rehype:title=Rehype Attrs Sub-->\n```js\nconsole.log('')\n```\n"
const expected = `<!--rehype:title=Rehype Attrs-->\n<pre><code class="language-js" data-config="{&#x22;title&#x22;:&#x22;Rehype Attrs&#x22;,&#x22;rehyp&#x22;:true}">console.log('')\n</code></pre>\n<pre><code class="language-js">console.log('')\n</code></pre>\n<!--rehype:title=Rehype Attrs Sub-->\n<pre><code class="language-js" data-config="{&#x22;title&#x22;:&#x22;Rehype Attrs Sub&#x22;,&#x22;rehyp&#x22;:true}">console.log('')\n</code></pre>`
const expected = `<!--rehype:title=Rehype Attrs-->\n<pre data-type="rehyp"><code class="language-js" data-config="{&#x22;title&#x22;:&#x22;Rehype Attrs&#x22;,&#x22;rehyp&#x22;:true}">console.log('')\n</code></pre>\n<pre><code class="language-js">console.log('')\n</code></pre>\n<!--rehype:title=Rehype Attrs Sub-->\n<pre data-type="rehyp"><code class="language-js" data-config="{&#x22;title&#x22;:&#x22;Rehype Attrs Sub&#x22;,&#x22;rehyp&#x22;:true}">console.log('')\n</code></pre>`
const htmlStr = unified()
.use(remarkParse)
.use(remark2rehype, { allowDangerousHtml: true })
Expand All @@ -53,7 +53,7 @@ describe('rehype-attr test case', () => {
{
title: 'options="attr" - Code',
markdown: '<!--rehype:title=Rehype Attrs-->\n```js\nconsole.log("")\n```',
expected: '<!--rehype:title=Rehype Attrs-->\n<pre><code class="language-js" title="Rehype Attrs">console.log("")\n</code></pre>',
expected: '<!--rehype:title=Rehype Attrs-->\n<pre data-type="rehyp"><code class="language-js" title="Rehype Attrs">console.log("")\n</code></pre>',
},
{
title: 'options="attr" - Emphasis <strong>',
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit e47dedd

Please sign in to comment.