prosemirror-highlight 0.2.0 → 0.3.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 +15 -1
- package/dist/chunk-ZZGBRRBM.js +46 -0
- package/dist/lowlight.js +5 -41
- package/dist/refractor.d.ts +8 -0
- package/dist/refractor.js +19 -0
- package/package.json +23 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/prosemirror-highlight)
|
|
4
4
|
|
|
5
|
-
Highlight your code blocks in [ProseMirror], with any syntax highlighter you
|
|
5
|
+
Highlight your code blocks in [ProseMirror], with any syntax highlighter you like!
|
|
6
6
|
|
|
7
7
|
## Usage
|
|
8
8
|
|
|
@@ -55,6 +55,18 @@ const parser = createParser(lowlight)
|
|
|
55
55
|
export const lowlightPlugin = createHighlightPlugin({ parser })
|
|
56
56
|
```
|
|
57
57
|
|
|
58
|
+
### With [refractor] (based on [Prism])
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { refractor } from 'refractor'
|
|
62
|
+
|
|
63
|
+
import { createHighlightPlugin } from 'prosemirror-highlight'
|
|
64
|
+
import { createParser } from 'prosemirror-highlight/refractor'
|
|
65
|
+
|
|
66
|
+
const parser = createParser(refractor)
|
|
67
|
+
export const refractorPlugin = createHighlightPlugin({ parser })
|
|
68
|
+
```
|
|
69
|
+
|
|
58
70
|
## Online demo
|
|
59
71
|
|
|
60
72
|
[](https://stackblitz.com/github/ocavue/prosemirror-highlight?file=playground%2Fmain.ts)
|
|
@@ -73,3 +85,5 @@ MIT
|
|
|
73
85
|
[Highlight.js]: https://github.com/highlightjs/highlight.js
|
|
74
86
|
[Shiki]: https://github.com/shikijs/shiki
|
|
75
87
|
[Shikiji]: https://github.com/antfu/shikiji
|
|
88
|
+
[refractor]: https://github.com/wooorm/refractor
|
|
89
|
+
[Prism]: https://github.com/PrismJS/prism
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// src/hast.ts
|
|
2
|
+
import { Decoration } from "prosemirror-view";
|
|
3
|
+
function fillFromRoot(decorations, node, from) {
|
|
4
|
+
for (const child of node.children) {
|
|
5
|
+
from = fillFromRootContent(decorations, child, from);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
function fillFromRootContent(decorations, node, from) {
|
|
9
|
+
if (node.type === "element") {
|
|
10
|
+
const to = from + getElementSize(node);
|
|
11
|
+
const { className, ...rest } = node.properties || {};
|
|
12
|
+
decorations.push(
|
|
13
|
+
Decoration.inline(from, to, {
|
|
14
|
+
class: className ? Array.isArray(className) ? className.join(" ") : String(className) : void 0,
|
|
15
|
+
...rest,
|
|
16
|
+
nodeName: node.tagName
|
|
17
|
+
})
|
|
18
|
+
);
|
|
19
|
+
return to;
|
|
20
|
+
} else if (node.type === "text") {
|
|
21
|
+
return from + node.value.length;
|
|
22
|
+
} else {
|
|
23
|
+
return from;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function getElementSize(node) {
|
|
27
|
+
let size = 0;
|
|
28
|
+
for (const child of node.children) {
|
|
29
|
+
size += getElementContentSize(child);
|
|
30
|
+
}
|
|
31
|
+
return size;
|
|
32
|
+
}
|
|
33
|
+
function getElementContentSize(node) {
|
|
34
|
+
switch (node.type) {
|
|
35
|
+
case "element":
|
|
36
|
+
return getElementSize(node);
|
|
37
|
+
case "text":
|
|
38
|
+
return node.value.length;
|
|
39
|
+
default:
|
|
40
|
+
return 0;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export {
|
|
45
|
+
fillFromRoot
|
|
46
|
+
};
|
package/dist/lowlight.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
import {
|
|
2
|
+
fillFromRoot
|
|
3
|
+
} from "./chunk-ZZGBRRBM.js";
|
|
4
|
+
|
|
1
5
|
// src/lowlight.ts
|
|
2
|
-
import
|
|
6
|
+
import "prosemirror-view";
|
|
3
7
|
function createParser(lowlight) {
|
|
4
8
|
return function highlighter({ content, language, pos }) {
|
|
5
9
|
const root = language ? lowlight.highlight(language, content) : lowlight.highlightAuto(content);
|
|
@@ -9,46 +13,6 @@ function createParser(lowlight) {
|
|
|
9
13
|
return decorations;
|
|
10
14
|
};
|
|
11
15
|
}
|
|
12
|
-
function fillFromRoot(decorations, node, from) {
|
|
13
|
-
for (const child of node.children) {
|
|
14
|
-
from = fillFromRootContent(decorations, child, from);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
function fillFromRootContent(decorations, node, from) {
|
|
18
|
-
if (node.type === "element") {
|
|
19
|
-
const to = from + getElementSize(node);
|
|
20
|
-
const { className, ...rest } = node.properties;
|
|
21
|
-
decorations.push(
|
|
22
|
-
Decoration.inline(from, to, {
|
|
23
|
-
class: className ? Array.isArray(className) ? className.join(" ") : String(className) : void 0,
|
|
24
|
-
...rest,
|
|
25
|
-
nodeName: node.tagName
|
|
26
|
-
})
|
|
27
|
-
);
|
|
28
|
-
return to;
|
|
29
|
-
} else if (node.type === "text") {
|
|
30
|
-
return from + node.value.length;
|
|
31
|
-
} else {
|
|
32
|
-
return from;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
function getElementSize(node) {
|
|
36
|
-
let size = 0;
|
|
37
|
-
for (const child of node.children) {
|
|
38
|
-
size += getElementContentSize(child);
|
|
39
|
-
}
|
|
40
|
-
return size;
|
|
41
|
-
}
|
|
42
|
-
function getElementContentSize(node) {
|
|
43
|
-
switch (node.type) {
|
|
44
|
-
case "element":
|
|
45
|
-
return getElementSize(node);
|
|
46
|
-
case "text":
|
|
47
|
-
return node.value.length;
|
|
48
|
-
default:
|
|
49
|
-
return 0;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
16
|
export {
|
|
53
17
|
createParser
|
|
54
18
|
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {
|
|
2
|
+
fillFromRoot
|
|
3
|
+
} from "./chunk-ZZGBRRBM.js";
|
|
4
|
+
|
|
5
|
+
// src/refractor.ts
|
|
6
|
+
import "prosemirror-view";
|
|
7
|
+
function createParser(refractor) {
|
|
8
|
+
return function highlighter({ content, language, pos }) {
|
|
9
|
+
const root = refractor.highlight(content, language || "");
|
|
10
|
+
const decorations = [];
|
|
11
|
+
const from = pos + 1;
|
|
12
|
+
const hastRoot = root;
|
|
13
|
+
fillFromRoot(decorations, hastRoot, from);
|
|
14
|
+
return decorations;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export {
|
|
18
|
+
createParser
|
|
19
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prosemirror-highlight",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"packageManager": "pnpm@8.10.0",
|
|
6
6
|
"description": "A ProseMirror plugin to highlight code blocks",
|
|
7
7
|
"author": "ocavue <ocavue@gmail.com>",
|
|
@@ -13,7 +13,16 @@
|
|
|
13
13
|
"url": "https://github.com/ocavue/prosemirror-highlight.git"
|
|
14
14
|
},
|
|
15
15
|
"bugs": "https://github.com/ocavue/prosemirror-highlight/issues",
|
|
16
|
-
"keywords": [
|
|
16
|
+
"keywords": [
|
|
17
|
+
"prosemirror",
|
|
18
|
+
"editor",
|
|
19
|
+
"highlight.js",
|
|
20
|
+
"shiki",
|
|
21
|
+
"shikiji",
|
|
22
|
+
"refractor",
|
|
23
|
+
"lowlight",
|
|
24
|
+
"prism"
|
|
25
|
+
],
|
|
17
26
|
"sideEffects": false,
|
|
18
27
|
"main": "./src/index.ts",
|
|
19
28
|
"module": "./src/index.ts",
|
|
@@ -25,6 +34,9 @@
|
|
|
25
34
|
"./lowlight": {
|
|
26
35
|
"default": "./src/lowlight.ts"
|
|
27
36
|
},
|
|
37
|
+
"./refractor": {
|
|
38
|
+
"default": "./src/refractor.ts"
|
|
39
|
+
},
|
|
28
40
|
"./shiki": {
|
|
29
41
|
"default": "./src/shiki.ts"
|
|
30
42
|
},
|
|
@@ -53,6 +65,7 @@
|
|
|
53
65
|
"prosemirror-state": "^1.4.3",
|
|
54
66
|
"prosemirror-transform": "^1.8.0",
|
|
55
67
|
"prosemirror-view": "^1.32.4",
|
|
68
|
+
"refractor": "^4.8.1",
|
|
56
69
|
"shiki": "^0.14.6",
|
|
57
70
|
"shikiji": "^0.8.0"
|
|
58
71
|
},
|
|
@@ -78,6 +91,9 @@
|
|
|
78
91
|
"prosemirror-view": {
|
|
79
92
|
"optional": true
|
|
80
93
|
},
|
|
94
|
+
"refractor": {
|
|
95
|
+
"optional": true
|
|
96
|
+
},
|
|
81
97
|
"shiki": {
|
|
82
98
|
"optional": true
|
|
83
99
|
},
|
|
@@ -101,6 +117,7 @@
|
|
|
101
117
|
"prosemirror-state": "^1.4.3",
|
|
102
118
|
"prosemirror-transform": "^1.8.0",
|
|
103
119
|
"prosemirror-view": "^1.32.4",
|
|
120
|
+
"refractor": "^4.8.1",
|
|
104
121
|
"shiki": "^0.14.6",
|
|
105
122
|
"shikiji": "^0.8.0",
|
|
106
123
|
"tsup": "^8.0.1",
|
|
@@ -121,6 +138,10 @@
|
|
|
121
138
|
"types": "./dist/lowlight.d.ts",
|
|
122
139
|
"default": "./dist/lowlight.js"
|
|
123
140
|
},
|
|
141
|
+
"./refractor": {
|
|
142
|
+
"types": "./dist/refractor.d.ts",
|
|
143
|
+
"default": "./dist/refractor.js"
|
|
144
|
+
},
|
|
124
145
|
"./shiki": {
|
|
125
146
|
"types": "./dist/shiki.d.ts",
|
|
126
147
|
"default": "./dist/shiki.js"
|