From 102d6750ca62975bd77bf864f195c8b9d0b631e8 Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Mon, 31 May 2021 01:08:30 +0800 Subject: [PATCH] fix: Fix the property setting of the code. --- __tests__/index.test.ts | 38 ++++++++++++++++++++++++++++++++++++-- src/index.ts | 3 ++- src/utils.ts | 5 +++-- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index dce7793..bcb4a7f 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -9,12 +9,41 @@ const remarkParse = require('remark-parse') const gfm = require('remark-gfm') const rehypeAttrs = require('../lib') const utils = require('../lib/utils') +const visit = require('../lib/visit') const mrkStr = "\n```js\nconsole.log('')\n```" describe('rehype-attr function test case', () => { - it('getURLParameters', async () => { - expect(utils.getURLParameters('title=1&b=2')).toEqual({ title: "1", b: "2" }); + it('visit', async () => { + const node = { + "type": "root", + "children": [ + { + "type": "element", + "tagName": "p", + "properties": {}, + "children": [ + { "type": "text", "value": "This is a " }, + { "type": "element", "tagName": "del", "properties": {}, "children": [ { "type": "text", "value": "title" } ] }, + { "type": "comment", "value": "rehype:style=color:pink;" } + ] + } + ], + "data": { "quirksMode": false } + } + visit(node, 'element', (node, index, parent) => { + expect(/(del|p)/.test(node.tagName)).toBeTruthy() + expect(typeof node).toEqual('object') + expect(typeof index).toEqual('number') + expect(typeof parent).toEqual('object') + }) + expect(visit(node)).toBeUndefined() + expect(visit(node, 'element')).toBeUndefined() + expect(visit(node, 'element', () => {})).toBeUndefined() + expect(visit()).toBeUndefined() + expect(visit(undefined)).toBeUndefined() + expect(visit(undefined, undefined)).toBeUndefined() + expect(visit(undefined, undefined, undefined)).toBeUndefined() }); it('getCommentObject', async () => { expect(utils.getCommentObject({})).toEqual({ }); @@ -165,6 +194,11 @@ describe('rehype-attr test case', () => { markdown: '\n```js\nconsole.log("")\n```', expected: '\n
console.log("")\n
', }, + { + title: 'options="attr" - Code', + markdown: '```js\nconsole.log("")\n```\n', + expected: '
console.log("")\n
\n', + }, { title: 'options="attr" - Emphasis ', markdown: 'Npm stand for *node* packet manager.', diff --git a/src/index.ts b/src/index.ts index 42af251..75de896 100644 --- a/src/index.ts +++ b/src/index.ts @@ -66,7 +66,8 @@ const rehypeAttrs: Plugin<[RehypeAttrsOptions?]> = (options): MdastTransformer = codeNode.properties = propertiesHandle(codeNode.properties, attr, opts.properties) } } - } else if (/^(em|strong|b|a|i|p|blockquote|h(1|2|3|4|5|6)|code|img|del|ul|ol)$/.test(node.tagName as string)) { + } + if (/^(em|strong|b|a|i|p|pre|blockquote|h(1|2|3|4|5|6)|code|img|del|ul|ol)$/.test(node.tagName as string)) { const child = nextChild(parent.children, index) if (child) { const attr = getCommentObject(child) diff --git a/src/utils.ts b/src/utils.ts index 1735590..ae10d56 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -19,12 +19,13 @@ export const prevChild = (data: Content[] = [], index: number): CommentData | un let i = index; while (i > -1) { i--; - if ((data[i].value && (data[i].value as string).replace(/(\n|\s)/g, '') !== '') || data[i].type !== 'text') { + if (!data[i]) return + if ((data[i] && data[i].value && ((data[i].value || '') as string).replace(/(\n|\s)/g, '') !== '') || data[i].type !== 'text') { if (!/rehype:/.test(data[i].value as string) && (data[i].type as string) !== 'comment') return; return data[i] as unknown as CommentData; } } - return {} as CommentData; + return; } export const nextChild = (data: Content[] = [], index: number, tagName?: string): CommentData | undefined => {