@thomd/rehype-textmarker 1.0.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/LICENSE +21 -0
- package/README.md +98 -0
- package/index.js +41 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Thomas Dürr
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# rehype-textmarker
|
|
2
|
+
|
|
3
|
+
![Build][build-badge]
|
|
4
|
+
|
|
5
|
+
`rehype-textmarker` is a [rehype][rehype] plugin to highlight text pattern like e.g. `TODO`, `FIXME` or to highlight text surrounded by a defined symbol, e.g. `this is ≈highlighted≈ text` by enclosing the text with a `<mark>` tag.
|
|
6
|
+
|
|
7
|
+
See below example.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
Say we have the following file `example.md`:
|
|
12
|
+
|
|
13
|
+
```markdown
|
|
14
|
+
# Headline
|
|
15
|
+
|
|
16
|
+
Paragraph with ≈highlighted≈ text.
|
|
17
|
+
|
|
18
|
+
Inline code `console.≈log≈();`.
|
|
19
|
+
|
|
20
|
+
TODO things to do later
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
and a module `example.js`:
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
import { remark } from 'remark'
|
|
27
|
+
import remarkRehype from 'remark-rehype'
|
|
28
|
+
import rehypeTextmarker from 'rehype-textmarker'
|
|
29
|
+
import rehypeStringify from 'rehype-stringify'
|
|
30
|
+
import { read } from 'to-vfile'
|
|
31
|
+
|
|
32
|
+
const file = await remark()
|
|
33
|
+
.use(remarkRehype)
|
|
34
|
+
.use(rehypeTextmarker, [
|
|
35
|
+
{
|
|
36
|
+
textPattern: /≈([^≈]+)≈/g,
|
|
37
|
+
className: 'yellow-marker',
|
|
38
|
+
tags: ['p', 'code'],
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
textPattern: /\b(TODO)\b/,
|
|
42
|
+
className: 'red-marker',
|
|
43
|
+
},
|
|
44
|
+
])
|
|
45
|
+
.use(rehypeStringify)
|
|
46
|
+
.process(await read('example.md'))
|
|
47
|
+
|
|
48
|
+
console.log(file.value)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
then running `node example.js` yields:
|
|
52
|
+
|
|
53
|
+
```html
|
|
54
|
+
<h1>Headline</h1>
|
|
55
|
+
<p>Paragraph with <mark class="yellow-marker">highlighted</mark> text.</p>
|
|
56
|
+
<p>
|
|
57
|
+
Inline code <code>console.<mark class="yellow-marker">log</mark>();</code>.
|
|
58
|
+
</p>
|
|
59
|
+
<p><mark class="red-marker">TODO</mark> things to do later</p>
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Test
|
|
63
|
+
|
|
64
|
+
npm run test
|
|
65
|
+
|
|
66
|
+
## API
|
|
67
|
+
|
|
68
|
+
The default export is `rehypeTextmarker`.
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
unified().use(rehypeTextmarker, options)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Options
|
|
75
|
+
|
|
76
|
+
In order to define **multiple** regular expressions, put options into a list.
|
|
77
|
+
|
|
78
|
+
```js
|
|
79
|
+
unified().use(rehypeTextmarker, options)
|
|
80
|
+
unified().use(rehypeTextmarker, [options_1, options_2, ... ])
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
where `options` is an object with at least a `textPattern` property.
|
|
84
|
+
|
|
85
|
+
The following options are available:
|
|
86
|
+
|
|
87
|
+
- `textPattern` (`RegExp`, mandatory) — regular expression which must contain a **capturing group**.
|
|
88
|
+
|
|
89
|
+
- `htmlTag` (`string`, optional) — HTML tag to sourround the captured string. Default is a `mark` tag.
|
|
90
|
+
|
|
91
|
+
- `className` (`string`, optional) — style class to be added to the html tag. Default is no style class.
|
|
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` which will only highlight wihtin Javascript code blocks . Default is `p` tag.
|
|
94
|
+
|
|
95
|
+
- `ignore` (`array` of `string`, optional) — list of tags to ignore. Default is `[]`.
|
|
96
|
+
|
|
97
|
+
[rehype]: https://github.com/rehypejs/rehype
|
|
98
|
+
[build-badge]: https://github.com/thomd/rehype-textmarker/workflows/plugin-test/badge.svg
|
package/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { visit } from 'unist-util-visit'
|
|
2
|
+
import { findAndReplace, defaultIgnore } from 'hast-util-find-and-replace'
|
|
3
|
+
|
|
4
|
+
const rehypeTextmarker = (options) => {
|
|
5
|
+
if (options !== null) {
|
|
6
|
+
if (!Array.isArray(options)) {
|
|
7
|
+
options = [options]
|
|
8
|
+
}
|
|
9
|
+
return (tree) => {
|
|
10
|
+
for (let option of options) {
|
|
11
|
+
visit(
|
|
12
|
+
tree,
|
|
13
|
+
(node) => {
|
|
14
|
+
const className = node.properties?.className
|
|
15
|
+
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 == 'p'
|
|
17
|
+
},
|
|
18
|
+
(node) => {
|
|
19
|
+
findAndReplace(
|
|
20
|
+
node,
|
|
21
|
+
[
|
|
22
|
+
option.textPattern,
|
|
23
|
+
(value, capture, match) => {
|
|
24
|
+
const markNode = {
|
|
25
|
+
type: 'element',
|
|
26
|
+
tagName: option.htmlTag != null ? option.htmlTag : 'mark',
|
|
27
|
+
properties: option.className != null ? { className: [option.className] } : {},
|
|
28
|
+
children: [{ type: 'text', value: capture }],
|
|
29
|
+
}
|
|
30
|
+
return markNode
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
{ ignore: defaultIgnore.concat(option.ignore ? option.ignore : []) }
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export default rehypeTextmarker
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@thomd/rehype-textmarker",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "rehype-plugin to enclode text pattern with a mark-tag for text highlighting",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"rehype-plugin",
|
|
7
|
+
"rehype",
|
|
8
|
+
"html"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"main": "index.js",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"hast-util-find-and-replace": "^5.0.1",
|
|
15
|
+
"unist-util-visit": "^5.0.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"c8": "^10.1.2",
|
|
19
|
+
"prettier": "^3.3.2",
|
|
20
|
+
"rehype-document": "^7.0.3",
|
|
21
|
+
"rehype-format": "^5.0.0",
|
|
22
|
+
"rehype-stringify": "^10.0.0",
|
|
23
|
+
"remark": "^15.0.1",
|
|
24
|
+
"remark-rehype": "^11.1.0",
|
|
25
|
+
"to-vfile": "^8.0.0"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "c8 --100 --reporter lcov node --conditions development test/index.js"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"index.js"
|
|
32
|
+
],
|
|
33
|
+
"author": "Thomas Dürr <thomduerr@gmail.com>",
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/thomd/rehype-textmarker.git"
|
|
37
|
+
},
|
|
38
|
+
"bugs": "https://github.com/thomd/rehype-textmarker/issues"
|
|
39
|
+
}
|