prosemirror-highlight 0.5.0 → 0.7.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 +43 -95
- package/dist/index.d.ts +1 -1
- package/dist/lowlight.d.ts +1 -1
- package/dist/refractor.d.ts +1 -1
- package/dist/shiki.d.ts +1 -1
- package/dist/sugar-high.d.ts +7 -0
- package/dist/sugar-high.js +36 -0
- package/package.json +23 -24
- package/dist/shikiji.d.ts +0 -8
- package/dist/shikiji.js +0 -26
- package/src/index.ts +0 -3
- /package/dist/{types-hA0ujWQ1.d.ts → types-D9kxOI8-.d.ts} +0 -0
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
|
|
5
|
+
Highlight your [ProseMirror] code blocks with any syntax highlighter you like!
|
|
6
6
|
|
|
7
7
|
## Usage
|
|
8
8
|
|
|
@@ -12,12 +12,12 @@ Highlight your code blocks in [ProseMirror], with any syntax highlighter you lik
|
|
|
12
12
|
<summary>Static loading of a fixed set of languages</summary>
|
|
13
13
|
|
|
14
14
|
```ts
|
|
15
|
-
import {
|
|
15
|
+
import { getSingletonHighlighter } from 'shiki'
|
|
16
16
|
|
|
17
17
|
import { createHighlightPlugin } from 'prosemirror-highlight'
|
|
18
18
|
import { createParser } from 'prosemirror-highlight/shiki'
|
|
19
19
|
|
|
20
|
-
const highlighter = await
|
|
20
|
+
const highlighter = await getSingletonHighlighter({
|
|
21
21
|
themes: ['github-light'],
|
|
22
22
|
langs: ['javascript', 'typescript', 'python'],
|
|
23
23
|
})
|
|
@@ -31,15 +31,17 @@ export const shikiPlugin = createHighlightPlugin({ parser })
|
|
|
31
31
|
<summary>Dynamic loading of arbitrary languages</summary>
|
|
32
32
|
|
|
33
33
|
```ts
|
|
34
|
-
import {
|
|
34
|
+
import {
|
|
35
|
+
getSingletonHighlighter,
|
|
36
|
+
type BuiltinLanguage,
|
|
37
|
+
type Highlighter,
|
|
38
|
+
} from 'shiki'
|
|
35
39
|
|
|
36
40
|
import { createHighlightPlugin } from 'prosemirror-highlight'
|
|
37
41
|
import { createParser, type Parser } from 'prosemirror-highlight/shiki'
|
|
38
42
|
|
|
39
|
-
let highlighterPromise: Promise<void> | undefined
|
|
40
43
|
let highlighter: Highlighter | undefined
|
|
41
44
|
let parser: Parser | undefined
|
|
42
|
-
const loadedLanguages = new Set<string>()
|
|
43
45
|
|
|
44
46
|
/**
|
|
45
47
|
* Lazy load highlighter and highlighter languages.
|
|
@@ -49,25 +51,18 @@ const loadedLanguages = new Set<string>()
|
|
|
49
51
|
* Otherwise, it returns an array of decorations.
|
|
50
52
|
*/
|
|
51
53
|
const lazyParser: Parser = (options) => {
|
|
52
|
-
if (!
|
|
53
|
-
|
|
54
|
+
if (!highlighter) {
|
|
55
|
+
return getSingletonHighlighter({
|
|
54
56
|
themes: ['github-light'],
|
|
55
57
|
langs: [],
|
|
56
58
|
}).then((h) => {
|
|
57
59
|
highlighter = h
|
|
58
60
|
})
|
|
59
|
-
return highlighterPromise
|
|
60
61
|
}
|
|
61
62
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const language = options.language
|
|
67
|
-
if (language && !loadedLanguages.has(language)) {
|
|
68
|
-
return highlighter.loadLanguage(language as BuiltinLanguage).then(() => {
|
|
69
|
-
loadedLanguages.add(language)
|
|
70
|
-
})
|
|
63
|
+
const language = options.language as BuiltinLanguage
|
|
64
|
+
if (language && !highlighter.getLoadedLanguages().includes(language)) {
|
|
65
|
+
return highlighter.loadLanguage(language)
|
|
71
66
|
}
|
|
72
67
|
|
|
73
68
|
if (!parser) {
|
|
@@ -82,82 +77,6 @@ export const shikiLazyPlugin = createHighlightPlugin({ parser: lazyParser })
|
|
|
82
77
|
|
|
83
78
|
</details>
|
|
84
79
|
|
|
85
|
-
### With [Shikiji]
|
|
86
|
-
|
|
87
|
-
<details>
|
|
88
|
-
<summary>Static loading of a fixed set of languages</summary>
|
|
89
|
-
|
|
90
|
-
```ts
|
|
91
|
-
import { getHighlighter } from 'shikiji'
|
|
92
|
-
|
|
93
|
-
import { createHighlightPlugin } from 'prosemirror-highlight'
|
|
94
|
-
import { createParser } from 'prosemirror-highlight/shikiji'
|
|
95
|
-
|
|
96
|
-
const highlighter = await getHighlighter({
|
|
97
|
-
themes: ['vitesse-light'],
|
|
98
|
-
langs: ['javascript', 'typescript', 'python'],
|
|
99
|
-
})
|
|
100
|
-
const parser = createParser(highlighter)
|
|
101
|
-
export const shikijiPlugin = createHighlightPlugin({ parser })
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
</details>
|
|
105
|
-
|
|
106
|
-
<details>
|
|
107
|
-
<summary>Dynamic loading of arbitrary languages</summary>
|
|
108
|
-
|
|
109
|
-
```ts
|
|
110
|
-
import { getHighlighter, type Highlighter, type BuiltinLanguage } from 'shikiji'
|
|
111
|
-
|
|
112
|
-
import { createHighlightPlugin, type Parser } from 'prosemirror-highlight'
|
|
113
|
-
import { createParser } from 'prosemirror-highlight/shikiji'
|
|
114
|
-
|
|
115
|
-
let highlighterPromise: Promise<void> | undefined
|
|
116
|
-
let highlighter: Highlighter | undefined
|
|
117
|
-
let parser: Parser | undefined
|
|
118
|
-
const loadedLanguages = new Set<string>()
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Lazy load highlighter and highlighter languages.
|
|
122
|
-
*
|
|
123
|
-
* When the highlighter or the required language is not loaded, it returns a
|
|
124
|
-
* promise that resolves when the highlighter or the language is loaded.
|
|
125
|
-
* Otherwise, it returns an array of decorations.
|
|
126
|
-
*/
|
|
127
|
-
const lazyParser: Parser = (options) => {
|
|
128
|
-
if (!highlighterPromise) {
|
|
129
|
-
highlighterPromise = getHighlighter({
|
|
130
|
-
themes: ['vitesse-light'],
|
|
131
|
-
langs: [],
|
|
132
|
-
}).then((h) => {
|
|
133
|
-
highlighter = h
|
|
134
|
-
})
|
|
135
|
-
return highlighterPromise
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
if (!highlighter) {
|
|
139
|
-
return highlighterPromise
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
const language = options.language
|
|
143
|
-
if (language && !loadedLanguages.has(language)) {
|
|
144
|
-
return highlighter.loadLanguage(language as BuiltinLanguage).then(() => {
|
|
145
|
-
loadedLanguages.add(language)
|
|
146
|
-
})
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (!parser) {
|
|
150
|
-
parser = createParser(highlighter)
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return parser(options)
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
export const shikijiLazyPlugin = createHighlightPlugin({ parser: lazyParser })
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
</details>
|
|
160
|
-
|
|
161
80
|
### With [lowlight] (based on [Highlight.js])
|
|
162
81
|
|
|
163
82
|
<details>
|
|
@@ -195,6 +114,35 @@ export const refractorPlugin = createHighlightPlugin({ parser })
|
|
|
195
114
|
|
|
196
115
|
</details>
|
|
197
116
|
|
|
117
|
+
### With [Sugar high]
|
|
118
|
+
|
|
119
|
+
<details>
|
|
120
|
+
<summary>Highlight with CSS</summary>
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
import { createHighlightPlugin } from 'prosemirror-highlight'
|
|
124
|
+
import { createParser } from 'prosemirror-highlight/sugar-high'
|
|
125
|
+
|
|
126
|
+
const parser = createParser()
|
|
127
|
+
export const sugarHighPlugin = createHighlightPlugin({ parser })
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
```css
|
|
131
|
+
:root {
|
|
132
|
+
--sh-class: #2d5e9d;
|
|
133
|
+
--sh-identifier: #354150;
|
|
134
|
+
--sh-sign: #8996a3;
|
|
135
|
+
--sh-property: #0550ae;
|
|
136
|
+
--sh-entity: #249a97;
|
|
137
|
+
--sh-jsxliterals: #6266d1;
|
|
138
|
+
--sh-string: #00a99a;
|
|
139
|
+
--sh-keyword: #f47067;
|
|
140
|
+
--sh-comment: #a19595;
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
</details>
|
|
145
|
+
|
|
198
146
|
## Online demo
|
|
199
147
|
|
|
200
148
|
[](https://stackblitz.com/github/ocavue/prosemirror-highlight?file=playground%2Fmain.ts)
|
|
@@ -212,6 +160,6 @@ MIT
|
|
|
212
160
|
[lowlight]: https://github.com/wooorm/lowlight
|
|
213
161
|
[Highlight.js]: https://github.com/highlightjs/highlight.js
|
|
214
162
|
[Shiki]: https://github.com/shikijs/shiki
|
|
215
|
-
[Shikiji]: https://github.com/antfu/shikiji
|
|
216
163
|
[refractor]: https://github.com/wooorm/refractor
|
|
217
164
|
[Prism]: https://github.com/PrismJS/prism
|
|
165
|
+
[Sugar high]: https://github.com/huozhi/sugar-high
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Node } from 'prosemirror-model';
|
|
2
2
|
import { Transaction, Plugin } from 'prosemirror-state';
|
|
3
3
|
import { Decoration, DecorationSet } from 'prosemirror-view';
|
|
4
|
-
import { P as Parser, L as LanguageExtractor } from './types-
|
|
4
|
+
import { P as Parser, L as LanguageExtractor } from './types-D9kxOI8-.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Represents a cache of doc positions to the node and decorations at that position
|
package/dist/lowlight.d.ts
CHANGED
package/dist/refractor.d.ts
CHANGED
package/dist/shiki.d.ts
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// src/sugar-high.ts
|
|
2
|
+
import { Decoration } from "prosemirror-view";
|
|
3
|
+
import { tokenize } from "sugar-high";
|
|
4
|
+
var types = [
|
|
5
|
+
"identifier",
|
|
6
|
+
"keyword",
|
|
7
|
+
"string",
|
|
8
|
+
"class",
|
|
9
|
+
"property",
|
|
10
|
+
"entity",
|
|
11
|
+
"jsxliterals",
|
|
12
|
+
"sign",
|
|
13
|
+
"comment",
|
|
14
|
+
"break",
|
|
15
|
+
"space"
|
|
16
|
+
];
|
|
17
|
+
function createParser() {
|
|
18
|
+
return function parser({ content, pos }) {
|
|
19
|
+
const decorations = [];
|
|
20
|
+
const tokens = tokenize(content);
|
|
21
|
+
let from = pos + 1;
|
|
22
|
+
for (const [type, content2] of tokens) {
|
|
23
|
+
const to = from + content2.length;
|
|
24
|
+
const decoration = Decoration.inline(from, to, {
|
|
25
|
+
class: `sh__token--${types[type]}`,
|
|
26
|
+
style: `color: var(--sh-${types[type]})`
|
|
27
|
+
});
|
|
28
|
+
decorations.push(decoration);
|
|
29
|
+
from = to;
|
|
30
|
+
}
|
|
31
|
+
return decorations;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export {
|
|
35
|
+
createParser
|
|
36
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prosemirror-highlight",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"packageManager": "pnpm@8.13.1",
|
|
4
|
+
"version": "0.7.0",
|
|
6
5
|
"description": "A ProseMirror plugin to highlight code blocks",
|
|
7
6
|
"author": "ocavue <ocavue@gmail.com>",
|
|
8
7
|
"license": "MIT",
|
|
@@ -18,7 +17,6 @@
|
|
|
18
17
|
"editor",
|
|
19
18
|
"highlight.js",
|
|
20
19
|
"shiki",
|
|
21
|
-
"shikiji",
|
|
22
20
|
"refractor",
|
|
23
21
|
"lowlight",
|
|
24
22
|
"prism"
|
|
@@ -44,9 +42,9 @@
|
|
|
44
42
|
"types": "./dist/shiki.d.ts",
|
|
45
43
|
"default": "./dist/shiki.js"
|
|
46
44
|
},
|
|
47
|
-
"./
|
|
48
|
-
"types": "./dist/
|
|
49
|
-
"default": "./dist/
|
|
45
|
+
"./sugar-high": {
|
|
46
|
+
"types": "./dist/sugar-high.d.ts",
|
|
47
|
+
"default": "./dist/sugar-high.js"
|
|
50
48
|
}
|
|
51
49
|
},
|
|
52
50
|
"files": [
|
|
@@ -61,8 +59,8 @@
|
|
|
61
59
|
"prosemirror-transform": "^1.8.0",
|
|
62
60
|
"prosemirror-view": "^1.32.4",
|
|
63
61
|
"refractor": "^4.8.1",
|
|
64
|
-
"shiki": "^1.
|
|
65
|
-
"
|
|
62
|
+
"shiki": "^1.9.0",
|
|
63
|
+
"sugar-high": "^0.6.1"
|
|
66
64
|
},
|
|
67
65
|
"peerDependenciesMeta": {
|
|
68
66
|
"@types/hast": {
|
|
@@ -92,33 +90,33 @@
|
|
|
92
90
|
"shiki": {
|
|
93
91
|
"optional": true
|
|
94
92
|
},
|
|
95
|
-
"
|
|
93
|
+
"sugar-high": {
|
|
96
94
|
"optional": true
|
|
97
95
|
}
|
|
98
96
|
},
|
|
99
97
|
"devDependencies": {
|
|
100
98
|
"@antfu/ni": "^0.21.12",
|
|
101
|
-
"@ocavue/eslint-config": "^1.
|
|
102
|
-
"@types/hast": "^3.0.
|
|
103
|
-
"@types/node": "^20.
|
|
104
|
-
"eslint": "^8.
|
|
99
|
+
"@ocavue/eslint-config": "^1.6.0",
|
|
100
|
+
"@types/hast": "^3.0.4",
|
|
101
|
+
"@types/node": "^20.14.7",
|
|
102
|
+
"eslint": "^8.57.0",
|
|
105
103
|
"highlight.js": "^11.9.0",
|
|
106
|
-
"jsdom": "^
|
|
104
|
+
"jsdom": "^24.1.0",
|
|
107
105
|
"lowlight": "^3.1.0",
|
|
108
|
-
"prettier": "^3.
|
|
106
|
+
"prettier": "^3.3.2",
|
|
109
107
|
"prosemirror-example-setup": "^1.2.2",
|
|
110
|
-
"prosemirror-model": "^1.
|
|
108
|
+
"prosemirror-model": "^1.21.1",
|
|
111
109
|
"prosemirror-schema-basic": "^1.2.2",
|
|
112
110
|
"prosemirror-state": "^1.4.3",
|
|
113
|
-
"prosemirror-transform": "^1.
|
|
114
|
-
"prosemirror-view": "^1.
|
|
111
|
+
"prosemirror-transform": "^1.9.0",
|
|
112
|
+
"prosemirror-view": "^1.33.8",
|
|
115
113
|
"refractor": "^4.8.1",
|
|
116
|
-
"shiki": "^1.
|
|
117
|
-
"
|
|
118
|
-
"tsup": "^8.0
|
|
119
|
-
"typescript": "^5.
|
|
120
|
-
"vite": "^5.
|
|
121
|
-
"vitest": "^1.
|
|
114
|
+
"shiki": "^1.9.0",
|
|
115
|
+
"sugar-high": "^0.7.0",
|
|
116
|
+
"tsup": "^8.1.0",
|
|
117
|
+
"typescript": "^5.5.2",
|
|
118
|
+
"vite": "^5.3.1",
|
|
119
|
+
"vitest": "^1.6.0"
|
|
122
120
|
},
|
|
123
121
|
"renovate": {
|
|
124
122
|
"dependencyDashboard": true,
|
|
@@ -129,6 +127,7 @@
|
|
|
129
127
|
"scripts": {
|
|
130
128
|
"dev": "vite",
|
|
131
129
|
"build": "tsup",
|
|
130
|
+
"build:playground": "vite build",
|
|
132
131
|
"lint": "eslint .",
|
|
133
132
|
"fix": "eslint --fix . && prettier --write .",
|
|
134
133
|
"start": "esno src/index.ts",
|
package/dist/shikiji.d.ts
DELETED
package/dist/shikiji.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
// src/shikiji.ts
|
|
2
|
-
import { Decoration } from "prosemirror-view";
|
|
3
|
-
function createParser(highlighter) {
|
|
4
|
-
return function parser({ content, language, pos }) {
|
|
5
|
-
const decorations = [];
|
|
6
|
-
const tokens = highlighter.codeToThemedTokens(content, {
|
|
7
|
-
lang: language
|
|
8
|
-
});
|
|
9
|
-
let from = pos + 1;
|
|
10
|
-
for (const line of tokens) {
|
|
11
|
-
for (const token of line) {
|
|
12
|
-
const to = from + token.content.length;
|
|
13
|
-
const decoration = Decoration.inline(from, to, {
|
|
14
|
-
style: `color: ${token.color}`
|
|
15
|
-
});
|
|
16
|
-
decorations.push(decoration);
|
|
17
|
-
from = to;
|
|
18
|
-
}
|
|
19
|
-
from += 1;
|
|
20
|
-
}
|
|
21
|
-
return decorations;
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
export {
|
|
25
|
-
createParser
|
|
26
|
-
};
|
package/src/index.ts
DELETED
|
File without changes
|