Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jaywcjlove/rehype-attr
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.1.0
Choose a base ref
...
head repository: jaywcjlove/rehype-attr
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.1.1
Choose a head ref
  • 2 commits
  • 4 files changed
  • 1 contributor

Commits on Sep 28, 2022

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    17dfaa6 View commit details
  2. released v2.1.1

    jaywcjlove committed Sep 28, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    132c54f View commit details
Showing with 32 additions and 8 deletions.
  1. +1 −1 package.json
  2. +1 −1 src/index.ts
  3. +12 −6 src/utils.ts
  4. +18 −0 test/index.test.ts
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rehype-attr",
"version": "2.1.0",
"version": "2.1.1",
"description": "New syntax to add attributes to Markdown.",
"homepage": "https://jaywcjlove.github.io/rehype-attr",
"author": "Kenny Wong <wowohoo@qq.com>",
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@ const rehypeAttrs: Plugin<[RehypeAttrsOptions?], Root> = (options = {}) => {
}

if (/^(em|strong|b|a|i|p|pre|kbd|blockquote|h(1|2|3|4|5|6)|code|table|img|del|ul|ol)$/.test(node.tagName) && parent && Array.isArray(parent.children) && typeof index === 'number') {
const child = nextChild(parent.children, index)
const child = nextChild(parent.children, index, '', codeBlockParames)
if (child) {
const attr = getCommentObject(child as Comment)
if (Object.keys(attr).length > 0) {
18 changes: 12 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -22,8 +22,9 @@ export const prevChild = (data: Literal[] = [], index: number): Comment | undefi
return;
}

export const nextChild = (data: RootContent[] | ElementContent[] = [], index: number, tagName?: string): ElementContent | undefined => {
export const nextChild = (data: RootContent[] | ElementContent[] = [], index: number, tagName?: string, codeBlockParames?: boolean): ElementContent | undefined => {
let i = index;

while (i < data.length) {
i++;
if (tagName) {
@@ -33,12 +34,17 @@ export const nextChild = (data: RootContent[] | ElementContent[] = [], index: nu
}
} else {
const element = data[i] as ElementContent & Literal;
if (!element || (element.type !== 'text' && (element.type as string) !== 'comment') || (element.type === 'text' && (element.value as string).replace(/(\n|\s)/g, '') !== '')) return;
if ((element.type as string) === 'comment') {
if (!element || element.type === 'element') return;
if (element.type === 'text' && element.value.replace(/(\n|\s)/g, '') !== '') return;
if (element?.type === 'comment') {
if (!/^rehype:/.test(element.value as string)) return;
const nextNode = nextChild(data, i, 'pre')
if (nextNode) return;
return element;
if (codeBlockParames) {
const nextNode = nextChild(data, i, 'pre', codeBlockParames)
if (nextNode) return;
return element;
} else {
return element;
}
}
}
}
18 changes: 18 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -76,6 +76,23 @@ describe('rehype-attr function test case', () => {
})

describe('rehype-attr test case', () => {
it('default codeBlockParames=false', async () => {
const mrkStr2 = "### title\n<!--rehype:title=title3-->\n```js\nconsole.log('')\n```"
const expected = `<h3 title="title3">title</h3>\n<!--rehype:title=title3-->\n<pre><code class="language-js">console.log('')\n</code></pre>`
const htmlStr = unified()
.use(remarkParse)
.use(remark2rehype, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypeAttrs, {
properties: 'attr',
codeBlockParames: false
})
.use(stringify)
.processSync(mrkStr2)
.toString()
expect(htmlStr).toEqual(expected);
});

it('default codeBlockParames=false', async () => {
const expected = `<!--rehype:title=Rehype Attrs-->\n<pre><code class="language-js">console.log('')\n</code></pre>`
const htmlStr = unified()
@@ -88,6 +105,7 @@ describe('rehype-attr test case', () => {
.toString()
expect(htmlStr).toEqual(expected);
});

it('default options="data"', async () => {
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()