@tiptap/extension-code-block-lowlight 2.0.0-beta.22 → 2.0.0-beta.220

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.
@@ -1,17 +1,14 @@
1
- import { Plugin, PluginKey } from 'prosemirror-state'
2
- import { Decoration, DecorationSet } from 'prosemirror-view'
3
- import { Node as ProsemirrorNode } from 'prosemirror-model'
4
1
  import { findChildren } from '@tiptap/core'
2
+ import { Node as ProsemirrorNode } from '@tiptap/pm/model'
3
+ import { Plugin, PluginKey } from '@tiptap/pm/state'
4
+ import { Decoration, DecorationSet } from '@tiptap/pm/view'
5
+ // @ts-ignore
6
+ import highlight from 'highlight.js/lib/core'
5
7
 
6
- function parseNodes(nodes: any[], className: string[] = []): { text: string, classes: string[] }[] {
8
+ function parseNodes(nodes: any[], className: string[] = []): { text: string; classes: string[] }[] {
7
9
  return nodes
8
10
  .map(node => {
9
- const classes = [
10
- ...className,
11
- ...node.properties
12
- ? node.properties.className
13
- : [],
14
- ]
11
+ const classes = [...className, ...(node.properties ? node.properties.className : [])]
15
12
 
16
13
  if (node.children) {
17
14
  return parseNodes(node.children, classes)
@@ -25,42 +22,84 @@ function parseNodes(nodes: any[], className: string[] = []): { text: string, cla
25
22
  .flat()
26
23
  }
27
24
 
28
- function getDecorations({ doc, name, lowlight }: { doc: ProsemirrorNode, name: string, lowlight: any }) {
25
+ function getHighlightNodes(result: any) {
26
+ // `.value` for lowlight v1, `.children` for lowlight v2
27
+ return result.value || result.children || []
28
+ }
29
+
30
+ function registered(aliasOrLanguage: string) {
31
+ return Boolean(highlight.getLanguage(aliasOrLanguage))
32
+ }
33
+
34
+ function getDecorations({
35
+ doc,
36
+ name,
37
+ lowlight,
38
+ defaultLanguage,
39
+ }: {
40
+ doc: ProsemirrorNode
41
+ name: string
42
+ lowlight: any
43
+ defaultLanguage: string | null | undefined
44
+ }) {
29
45
  const decorations: Decoration[] = []
30
46
 
31
- findChildren(doc, node => node.type.name === name)
32
- .forEach(block => {
33
- let from = block.pos + 1
34
- const { language } = block.node.attrs
35
- const languages = lowlight.listLanguages()
36
- const nodes = language && languages.includes(language)
37
- ? lowlight.highlight(language, block.node.textContent).value
38
- : lowlight.highlightAuto(block.node.textContent).value
39
-
40
- parseNodes(nodes).forEach(node => {
41
- const to = from + node.text.length
42
-
43
- if (node.classes.length) {
44
- const decoration = Decoration.inline(from, to, {
45
- class: node.classes.join(' '),
46
- })
47
+ findChildren(doc, node => node.type.name === name).forEach(block => {
48
+ let from = block.pos + 1
49
+ const language = block.node.attrs.language || defaultLanguage
50
+ const languages = lowlight.listLanguages()
47
51
 
48
- decorations.push(decoration)
49
- }
52
+ const nodes = language && (languages.includes(language) || registered(language))
53
+ ? getHighlightNodes(lowlight.highlight(language, block.node.textContent))
54
+ : getHighlightNodes(lowlight.highlightAuto(block.node.textContent))
55
+
56
+ parseNodes(nodes).forEach(node => {
57
+ const to = from + node.text.length
58
+
59
+ if (node.classes.length) {
60
+ const decoration = Decoration.inline(from, to, {
61
+ class: node.classes.join(' '),
62
+ })
63
+
64
+ decorations.push(decoration)
65
+ }
50
66
 
51
- from = to
52
- })
67
+ from = to
53
68
  })
69
+ })
54
70
 
55
71
  return DecorationSet.create(doc, decorations)
56
72
  }
57
73
 
58
- export function LowlightPlugin({ name, lowlight }: { name: string, lowlight: any }) {
59
- return new Plugin({
74
+ function isFunction(param: Function) {
75
+ return typeof param === 'function'
76
+ }
77
+
78
+ export function LowlightPlugin({
79
+ name,
80
+ lowlight,
81
+ defaultLanguage,
82
+ }: {
83
+ name: string
84
+ lowlight: any
85
+ defaultLanguage: string | null | undefined
86
+ }) {
87
+ if (!['highlight', 'highlightAuto', 'listLanguages'].every(api => isFunction(lowlight[api]))) {
88
+ throw Error(
89
+ 'You should provide an instance of lowlight to use the code-block-lowlight extension',
90
+ )
91
+ }
92
+
93
+ const lowlightPlugin: Plugin<any> = new Plugin({
60
94
  key: new PluginKey('lowlight'),
61
95
 
62
96
  state: {
63
- init: (_, { doc }) => getDecorations({ doc, name, lowlight }),
97
+ init: (_, { doc }) => getDecorations({
98
+ doc,
99
+ name,
100
+ lowlight,
101
+ defaultLanguage,
102
+ }),
64
103
  apply: (transaction, decorationSet, oldState, newState) => {
65
104
  const oldNodeName = oldState.selection.$head.parent.type.name
66
105
  const newNodeName = newState.selection.$head.parent.type.name
@@ -70,9 +109,8 @@ export function LowlightPlugin({ name, lowlight }: { name: string, lowlight: any
70
109
  if (
71
110
  transaction.docChanged
72
111
  // Apply decorations if:
73
- && (
74
- // selection includes named node,
75
- [oldNodeName, newNodeName].includes(name)
112
+ // selection includes named node,
113
+ && ([oldNodeName, newNodeName].includes(name)
76
114
  // OR transaction adds/removes named node,
77
115
  || newNodes.length !== oldNodes.length
78
116
  // OR transaction has changes that completely encapsulte a node
@@ -80,19 +118,29 @@ export function LowlightPlugin({ name, lowlight }: { name: string, lowlight: any
80
118
  // Such transactions can happen during collab syncing via y-prosemirror, for example.
81
119
  || transaction.steps.some(step => {
82
120
  // @ts-ignore
83
- return step.from !== undefined
121
+ return (
122
+ // @ts-ignore
123
+ step.from !== undefined
84
124
  // @ts-ignore
85
125
  && step.to !== undefined
86
126
  && oldNodes.some(node => {
87
127
  // @ts-ignore
88
- return node.pos >= step.from
128
+ return (
129
+ // @ts-ignore
130
+ node.pos >= step.from
89
131
  // @ts-ignore
90
132
  && node.pos + node.node.nodeSize <= step.to
133
+ )
91
134
  })
92
- })
93
- )
135
+ )
136
+ }))
94
137
  ) {
95
- return getDecorations({ doc: transaction.doc, name, lowlight })
138
+ return getDecorations({
139
+ doc: transaction.doc,
140
+ name,
141
+ lowlight,
142
+ defaultLanguage,
143
+ })
96
144
  }
97
145
 
98
146
  return decorationSet.map(transaction.mapping, transaction.doc)
@@ -101,8 +149,10 @@ export function LowlightPlugin({ name, lowlight }: { name: string, lowlight: any
101
149
 
102
150
  props: {
103
151
  decorations(state) {
104
- return this.getState(state)
152
+ return lowlightPlugin.getState(state)
105
153
  },
106
154
  },
107
155
  })
156
+
157
+ return lowlightPlugin
108
158
  }
package/CHANGELOG.md DELETED
@@ -1,183 +0,0 @@
1
- # Change Log
2
-
3
- All notable changes to this project will be documented in this file.
4
- See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
-
6
- # [2.0.0-beta.22](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.20...@tiptap/extension-code-block-lowlight@2.0.0-beta.22) (2021-05-13)
7
-
8
- **Note:** Version bump only for package @tiptap/extension-code-block-lowlight
9
-
10
-
11
-
12
-
13
-
14
- # [2.0.0-beta.20](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.19...@tiptap/extension-code-block-lowlight@2.0.0-beta.20) (2021-05-13)
15
-
16
- **Note:** Version bump only for package @tiptap/extension-code-block-lowlight
17
-
18
-
19
-
20
-
21
-
22
- # [2.0.0-beta.20](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.19...@tiptap/extension-code-block-lowlight@2.0.0-beta.20) (2021-05-13)
23
-
24
- **Note:** Version bump only for package @tiptap/extension-code-block-lowlight
25
-
26
-
27
-
28
-
29
-
30
- # [2.0.0-beta.19](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.18...@tiptap/extension-code-block-lowlight@2.0.0-beta.19) (2021-05-13)
31
-
32
- **Note:** Version bump only for package @tiptap/extension-code-block-lowlight
33
-
34
-
35
-
36
-
37
-
38
- # [2.0.0-beta.18](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.17...@tiptap/extension-code-block-lowlight@2.0.0-beta.18) (2021-05-07)
39
-
40
-
41
- ### Bug Fixes
42
-
43
- * revert adding exports ([bc320d0](https://github.com/ueberdosis/tiptap/commit/bc320d0b4b80b0e37a7e47a56e0f6daec6e65d98))
44
-
45
-
46
-
47
-
48
-
49
- # [2.0.0-beta.17](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.16...@tiptap/extension-code-block-lowlight@2.0.0-beta.17) (2021-05-06)
50
-
51
-
52
- ### Bug Fixes
53
-
54
- * revert adding type: module ([f8d6475](https://github.com/ueberdosis/tiptap/commit/f8d6475e2151faea6f96baecdd6bd75880d50d2c))
55
-
56
-
57
-
58
-
59
-
60
- # [2.0.0-beta.16](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.15...@tiptap/extension-code-block-lowlight@2.0.0-beta.16) (2021-05-06)
61
-
62
-
63
- ### Bug Fixes
64
-
65
- * add exports to package.json ([1277fa4](https://github.com/ueberdosis/tiptap/commit/1277fa47151e9c039508cdb219bdd0ffe647f4ee))
66
-
67
-
68
-
69
-
70
-
71
- # [2.0.0-beta.15](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.14...@tiptap/extension-code-block-lowlight@2.0.0-beta.15) (2021-05-06)
72
-
73
- **Note:** Version bump only for package @tiptap/extension-code-block-lowlight
74
-
75
-
76
-
77
-
78
-
79
- # [2.0.0-beta.14](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.13...@tiptap/extension-code-block-lowlight@2.0.0-beta.14) (2021-05-05)
80
-
81
- **Note:** Version bump only for package @tiptap/extension-code-block-lowlight
82
-
83
-
84
-
85
-
86
-
87
- # [2.0.0-beta.13](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.12...@tiptap/extension-code-block-lowlight@2.0.0-beta.13) (2021-05-04)
88
-
89
- **Note:** Version bump only for package @tiptap/extension-code-block-lowlight
90
-
91
-
92
-
93
-
94
-
95
- # [2.0.0-beta.12](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.11...@tiptap/extension-code-block-lowlight@2.0.0-beta.12) (2021-04-27)
96
-
97
- **Note:** Version bump only for package @tiptap/extension-code-block-lowlight
98
-
99
-
100
-
101
-
102
-
103
- # [2.0.0-beta.11](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.10...@tiptap/extension-code-block-lowlight@2.0.0-beta.11) (2021-04-23)
104
-
105
- **Note:** Version bump only for package @tiptap/extension-code-block-lowlight
106
-
107
-
108
-
109
-
110
-
111
- # [2.0.0-beta.10](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.9...@tiptap/extension-code-block-lowlight@2.0.0-beta.10) (2021-04-22)
112
-
113
- **Note:** Version bump only for package @tiptap/extension-code-block-lowlight
114
-
115
-
116
-
117
-
118
-
119
- # [2.0.0-beta.9](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.8...@tiptap/extension-code-block-lowlight@2.0.0-beta.9) (2021-04-21)
120
-
121
- **Note:** Version bump only for package @tiptap/extension-code-block-lowlight
122
-
123
-
124
-
125
-
126
-
127
- # [2.0.0-beta.8](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.7...@tiptap/extension-code-block-lowlight@2.0.0-beta.8) (2021-04-16)
128
-
129
- **Note:** Version bump only for package @tiptap/extension-code-block-lowlight
130
-
131
-
132
-
133
-
134
-
135
- # [2.0.0-beta.7](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.6...@tiptap/extension-code-block-lowlight@2.0.0-beta.7) (2021-04-15)
136
-
137
- **Note:** Version bump only for package @tiptap/extension-code-block-lowlight
138
-
139
-
140
-
141
-
142
-
143
- # [2.0.0-beta.6](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.4...@tiptap/extension-code-block-lowlight@2.0.0-beta.6) (2021-04-14)
144
-
145
- **Note:** Version bump only for package @tiptap/extension-code-block-lowlight
146
-
147
-
148
-
149
-
150
-
151
- # [2.0.0-beta.4](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.3...@tiptap/extension-code-block-lowlight@2.0.0-beta.4) (2021-04-14)
152
-
153
-
154
- ### Bug Fixes
155
-
156
- * fix lowlight decorations for vue 3 ([daa5dc0](https://github.com/ueberdosis/tiptap/commit/daa5dc0fb1ec2db6889565fc9c091f3dbdbbda6d))
157
-
158
-
159
-
160
-
161
-
162
- # [2.0.0-beta.3](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.2...@tiptap/extension-code-block-lowlight@2.0.0-beta.3) (2021-04-12)
163
-
164
-
165
- ### Features
166
-
167
- * add parentConfig to extension context for more extendable extensions, fix [#259](https://github.com/ueberdosis/tiptap/issues/259) ([5e1ec5d](https://github.com/ueberdosis/tiptap/commit/5e1ec5d2a66be164f505d631f97861ab9344ba96))
168
-
169
-
170
-
171
-
172
-
173
- # [2.0.0-beta.2](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-code-block-lowlight@2.0.0-beta.1...@tiptap/extension-code-block-lowlight@2.0.0-beta.2) (2021-04-11)
174
-
175
- **Note:** Version bump only for package @tiptap/extension-code-block-lowlight
176
-
177
-
178
-
179
-
180
-
181
- # 2.0.0-beta.1 (2021-04-08)
182
-
183
- **Note:** Version bump only for package @tiptap/extension-code-block-lowlight
package/LICENSE.md DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2021, überdosis GbR
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.