@thomd/rehype-textmarker 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/index.js +21 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -61,7 +61,7 @@ then running `node example.js` yields:
|
|
|
61
61
|
|
|
62
62
|
## Test
|
|
63
63
|
|
|
64
|
-
|
|
64
|
+
pnpm run test
|
|
65
65
|
|
|
66
66
|
## API
|
|
67
67
|
|
|
@@ -90,9 +90,9 @@ The following options are available:
|
|
|
90
90
|
|
|
91
91
|
- `className` (`string`, optional) — style class to be added to the html tag. Default is no style class.
|
|
92
92
|
|
|
93
|
-
- `tags` (`array` of `string`, optional) — list of tags within whose text is highlighted. It is also possible to define tags with class names, e.g. `code.language-js`
|
|
93
|
+
- `tags` (`array` of `string`, optional) — list of tags within whose text is highlighted. It is also possible to define tags with class names, e.g. `code.language-js` which will only highlight wihtin Javascript code blocks . Default is `p` tag.
|
|
94
94
|
|
|
95
|
-
- `ignore` (`array` of `string`, optional) — list of tags to ignore. Default is `[]`.
|
|
95
|
+
- `ignore` (`array` of `string`, optional) — list of tags or tags with class names to ignore. Default is `[]`.
|
|
96
96
|
|
|
97
97
|
[rehype]: https://github.com/rehypejs/rehype
|
|
98
98
|
[build-badge]: https://github.com/thomd/rehype-textmarker/workflows/plugin-test/badge.svg
|
package/index.js
CHANGED
|
@@ -8,29 +8,38 @@ const rehypeTextmarker = (options) => {
|
|
|
8
8
|
}
|
|
9
9
|
return (tree) => {
|
|
10
10
|
for (let option of options) {
|
|
11
|
+
const ignoreSelectors = option.ignore ?? []
|
|
12
|
+
const ignoreFn = (node) => {
|
|
13
|
+
return ignoreSelectors.some((selector) => {
|
|
14
|
+
if (selector.includes('.')) {
|
|
15
|
+
const [tag, cls] = selector.split('.')
|
|
16
|
+
return node.tagName === tag && Array.isArray(node.properties?.className) && node.properties.className.includes(cls)
|
|
17
|
+
}
|
|
18
|
+
return node.tagName === selector
|
|
19
|
+
})
|
|
20
|
+
}
|
|
11
21
|
visit(
|
|
12
22
|
tree,
|
|
13
23
|
(node) => {
|
|
14
24
|
const className = node.properties?.className
|
|
15
25
|
const tags = className && Array.isArray(className) ? className.map((cls) => `${node.tagName}.${cls}`) : []
|
|
16
|
-
return option.tags ? option.tags.some((tag) => [node.tagName, ...tags].includes(tag)) : node.tagName
|
|
26
|
+
return option.tags ? option.tags.some((tag) => [node.tagName, ...tags].includes(tag)) : node.tagName === 'p'
|
|
17
27
|
},
|
|
18
28
|
(node) => {
|
|
19
29
|
findAndReplace(
|
|
20
30
|
node,
|
|
21
31
|
[
|
|
22
32
|
option.textPattern,
|
|
23
|
-
(value, capture
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
return markNode
|
|
31
|
-
},
|
|
33
|
+
(value, capture) => ({
|
|
34
|
+
type: 'element',
|
|
35
|
+
tagName: option.htmlTag ?? 'mark',
|
|
36
|
+
properties: option.className ? { className: [option.className] } : {},
|
|
37
|
+
children: [{ type: 'text', value: capture }],
|
|
38
|
+
}),
|
|
32
39
|
],
|
|
33
|
-
{
|
|
40
|
+
{
|
|
41
|
+
ignore: [...defaultIgnore, ignoreFn],
|
|
42
|
+
}
|
|
34
43
|
)
|
|
35
44
|
}
|
|
36
45
|
)
|
|
@@ -38,4 +47,5 @@ const rehypeTextmarker = (options) => {
|
|
|
38
47
|
}
|
|
39
48
|
}
|
|
40
49
|
}
|
|
50
|
+
|
|
41
51
|
export default rehypeTextmarker
|