Skip to content

Commit fc05c6c

Browse files
committedOct 23, 2022
fix: fix the type raw also works.
1 parent 132c54f commit fc05c6c

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed
 

‎src/utils.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export const prevChild = (data: Literal[] = [], index: number): Comment | undefi
2424

2525
export const nextChild = (data: RootContent[] | ElementContent[] = [], index: number, tagName?: string, codeBlockParames?: boolean): ElementContent | undefined => {
2626
let i = index;
27-
2827
while (i < data.length) {
2928
i++;
3029
if (tagName) {
@@ -36,13 +35,17 @@ export const nextChild = (data: RootContent[] | ElementContent[] = [], index: nu
3635
const element = data[i] as ElementContent & Literal;
3736
if (!element || element.type === 'element') return;
3837
if (element.type === 'text' && element.value.replace(/(\n|\s)/g, '') !== '') return;
39-
if (element?.type === 'comment') {
40-
if (!/^rehype:/.test(element.value as string)) return;
38+
if (/^(comment|raw)$/ig.test(element?.type)) {
39+
if (!/^rehype:/.test(element.value?.replace(/^(\s+)?<!--(.*?)-->/, '$2') || '')) {
40+
return
41+
};
4142
if (codeBlockParames) {
4243
const nextNode = nextChild(data, i, 'pre', codeBlockParames)
4344
if (nextNode) return;
45+
element.value = element.value?.replace(/^(\n|\s)+/, '')
4446
return element;
4547
} else {
48+
element.value = element.value?.replace(/^(\n|\s)+/, '')
4649
return element;
4750
}
4851
}
@@ -58,7 +61,7 @@ export const nextChild = (data: RootContent[] | ElementContent[] = [], index: nu
5861
* @returns 返回 当前参数数据 Object,`{}`
5962
*/
6063
export const getCommentObject = ({ value = '' }: Comment): Properties => {
61-
const param = getURLParameters(value.replace(/^rehype:/, ''));
64+
const param = getURLParameters(value.replace(/^<!--(.*?)-->/, '$1').replace(/^rehype:/, ''));
6265
Object.keys(param).forEach((keyName: string) => {
6366
if (param[keyName] === 'true') {
6467
param[keyName] = true;

‎test/index.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,35 @@ import * as utils from '../src/utils';
1212

1313
const mrkStr = "<!--rehype:title=Rehype Attrs-->\n```js\nconsole.log('')\n```"
1414

15+
describe('rehype-attr type raw test case', () => {
16+
[
17+
{
18+
title: 'options="attr" - Header <h1> `#`',
19+
markdown: '# This is a title\n <!--rehype:style=color:pink;-->',
20+
expected: '<h1 style="color:pink;">This is a title</h1>\n&#x3C;!--rehype:style=color:pink;-->',
21+
},
22+
{
23+
title: 'options="attr" - Code - not config 7',
24+
markdown: '<!--rehype:-->\n```js\nconsole.log("")\n```',
25+
expected: '&#x3C;!--rehype:-->\n<pre><code class="language-js">console.log("")\n</code></pre>',
26+
},
27+
].forEach((data, idx) => {
28+
it(data.title, async () => {
29+
const htmlStr = unified()
30+
.use(remarkParse)
31+
.use(remark2rehype, { allowDangerousHtml: true })
32+
// _____⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️________
33+
// .use(rehypeRaw)
34+
.use(rehypeAttrs, { properties: 'attr' })
35+
.use(stringify)
36+
.processSync(data.markdown)
37+
.toString()
38+
expect(htmlStr).toEqual(data.expected);
39+
});
40+
});
41+
42+
})
43+
1544
describe('rehype-attr function test case', () => {
1645
it('visit', async () => {
1746
const node: NodeData<Parent> = {

0 commit comments

Comments
 (0)
Please sign in to comment.