@tiptap/extension-code-block-lowlight 2.0.0-beta.208 → 2.0.0-beta.210

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tiptap/extension-code-block-lowlight",
3
3
  "description": "code block extension for tiptap",
4
- "version": "2.0.0-beta.208",
4
+ "version": "2.0.0-beta.210",
5
5
  "homepage": "https://tiptap.dev",
6
6
  "keywords": [
7
7
  "tiptap",
@@ -15,36 +15,45 @@
15
15
  "type": "module",
16
16
  "exports": {
17
17
  ".": {
18
- "types": "./dist/packages/extension-code-block-lowlight/src/index.d.ts",
19
- "import": "./dist/tiptap-extension-code-block-lowlight.esm.js",
20
- "require": "./dist/tiptap-extension-code-block-lowlight.cjs"
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js",
20
+ "require": "./dist/index.cjs"
21
21
  }
22
22
  },
23
- "main": "dist/tiptap-extension-code-block-lowlight.cjs",
24
- "umd": "dist/tiptap-extension-code-block-lowlight.umd.js",
25
- "module": "dist/tiptap-extension-code-block-lowlight.esm.js",
26
- "types": "dist/packages/extension-code-block-lowlight/src/index.d.ts",
23
+ "main": "dist/index.cjs",
24
+ "module": "dist/index.js",
25
+ "types": "dist/index.d.ts",
27
26
  "files": [
28
27
  "src",
29
28
  "dist"
30
29
  ],
31
30
  "peerDependencies": {
32
- "@tiptap/core": "^2.0.0-beta.193",
33
- "@tiptap/extension-code-block": "^2.0.0-beta.193",
34
- "prosemirror-model": "^1.18.1",
35
- "prosemirror-state": "^1.4.1",
36
- "prosemirror-view": "^1.28.2"
31
+ "@tiptap/core": "^2.0.0-beta.209",
32
+ "@tiptap/extension-code-block": "^2.0.0-beta.209",
33
+ "@tiptap/pm": "^2.0.0-beta.209"
37
34
  },
38
35
  "devDependencies": {
39
- "@tiptap/core": "^2.0.0-beta.208",
40
- "@tiptap/extension-code-block": "^2.0.0-beta.208",
41
- "prosemirror-model": "^1.18.1",
42
- "prosemirror-state": "^1.4.1",
43
- "prosemirror-view": "^1.28.2"
36
+ "@tiptap/core": "^2.0.0-beta.210",
37
+ "@tiptap/extension-code-block": "^2.0.0-beta.210",
38
+ "@tiptap/pm": "^2.0.0-beta.210"
44
39
  },
45
40
  "repository": {
46
41
  "type": "git",
47
42
  "url": "https://github.com/ueberdosis/tiptap",
48
43
  "directory": "packages/extension-code-block-lowlight"
44
+ },
45
+ "scripts": {
46
+ "build": "tsup"
47
+ },
48
+ "tsup": {
49
+ "entry": [
50
+ "src/index.ts"
51
+ ],
52
+ "dts": true,
53
+ "splitting": true,
54
+ "format": [
55
+ "esm",
56
+ "cjs"
57
+ ]
49
58
  }
50
59
  }
@@ -1,19 +1,14 @@
1
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'
2
5
  // @ts-ignore
3
6
  import highlight from 'highlight.js/lib/core'
4
- import { Node as ProsemirrorNode } from 'prosemirror-model'
5
- import { Plugin, PluginKey } from 'prosemirror-state'
6
- import { Decoration, DecorationSet } from 'prosemirror-view'
7
7
 
8
- function parseNodes(nodes: any[], className: string[] = []): { text: string, classes: string[] }[] {
8
+ function parseNodes(nodes: any[], className: string[] = []): { text: string; classes: string[] }[] {
9
9
  return nodes
10
10
  .map(node => {
11
- const classes = [
12
- ...className,
13
- ...node.properties
14
- ? node.properties.className
15
- : [],
16
- ]
11
+ const classes = [...className, ...(node.properties ? node.properties.className : [])]
17
12
 
18
13
  if (node.children) {
19
14
  return parseNodes(node.children, classes)
@@ -41,33 +36,37 @@ function getDecorations({
41
36
  name,
42
37
  lowlight,
43
38
  defaultLanguage,
44
- }: { doc: ProsemirrorNode, name: string, lowlight: any, defaultLanguage: string | null | undefined }) {
39
+ }: {
40
+ doc: ProsemirrorNode
41
+ name: string
42
+ lowlight: any
43
+ defaultLanguage: string | null | undefined
44
+ }) {
45
45
  const decorations: Decoration[] = []
46
46
 
47
- findChildren(doc, node => node.type.name === name)
48
- .forEach(block => {
49
- let from = block.pos + 1
50
- const language = block.node.attrs.language || defaultLanguage
51
- const languages = lowlight.listLanguages()
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()
52
51
 
53
- const nodes = language && (languages.includes(language) || registered(language))
54
- ? getHighlightNodes(lowlight.highlight(language, block.node.textContent))
55
- : getHighlightNodes(lowlight.highlightAuto(block.node.textContent))
52
+ const nodes = language && (languages.includes(language) || registered(language))
53
+ ? getHighlightNodes(lowlight.highlight(language, block.node.textContent))
54
+ : getHighlightNodes(lowlight.highlightAuto(block.node.textContent))
56
55
 
57
- parseNodes(nodes).forEach(node => {
58
- const to = from + node.text.length
56
+ parseNodes(nodes).forEach(node => {
57
+ const to = from + node.text.length
59
58
 
60
- if (node.classes.length) {
61
- const decoration = Decoration.inline(from, to, {
62
- class: node.classes.join(' '),
63
- })
59
+ if (node.classes.length) {
60
+ const decoration = Decoration.inline(from, to, {
61
+ class: node.classes.join(' '),
62
+ })
64
63
 
65
- decorations.push(decoration)
66
- }
64
+ decorations.push(decoration)
65
+ }
67
66
 
68
- from = to
69
- })
67
+ from = to
70
68
  })
69
+ })
71
70
 
72
71
  return DecorationSet.create(doc, decorations)
73
72
  }
@@ -76,9 +75,19 @@ function isFunction(param: Function) {
76
75
  return typeof param === 'function'
77
76
  }
78
77
 
79
- export function LowlightPlugin({ name, lowlight, defaultLanguage }: { name: string, lowlight: any, defaultLanguage: string | null | undefined }) {
78
+ export function LowlightPlugin({
79
+ name,
80
+ lowlight,
81
+ defaultLanguage,
82
+ }: {
83
+ name: string
84
+ lowlight: any
85
+ defaultLanguage: string | null | undefined
86
+ }) {
80
87
  if (!['highlight', 'highlightAuto', 'listLanguages'].every(api => isFunction(lowlight[api]))) {
81
- throw Error('You should provide an instance of lowlight to use the code-block-lowlight extension')
88
+ throw Error(
89
+ 'You should provide an instance of lowlight to use the code-block-lowlight extension',
90
+ )
82
91
  }
83
92
 
84
93
  const lowlightPlugin: Plugin<any> = new Plugin({
@@ -100,9 +109,8 @@ export function LowlightPlugin({ name, lowlight, defaultLanguage }: { name: stri
100
109
  if (
101
110
  transaction.docChanged
102
111
  // Apply decorations if:
103
- && (
104
- // selection includes named node,
105
- [oldNodeName, newNodeName].includes(name)
112
+ // selection includes named node,
113
+ && ([oldNodeName, newNodeName].includes(name)
106
114
  // OR transaction adds/removes named node,
107
115
  || newNodes.length !== oldNodes.length
108
116
  // OR transaction has changes that completely encapsulte a node
@@ -110,17 +118,22 @@ export function LowlightPlugin({ name, lowlight, defaultLanguage }: { name: stri
110
118
  // Such transactions can happen during collab syncing via y-prosemirror, for example.
111
119
  || transaction.steps.some(step => {
112
120
  // @ts-ignore
113
- return step.from !== undefined
121
+ return (
122
+ // @ts-ignore
123
+ step.from !== undefined
114
124
  // @ts-ignore
115
125
  && step.to !== undefined
116
126
  && oldNodes.some(node => {
117
127
  // @ts-ignore
118
- return node.pos >= step.from
128
+ return (
129
+ // @ts-ignore
130
+ node.pos >= step.from
119
131
  // @ts-ignore
120
132
  && node.pos + node.node.nodeSize <= step.to
133
+ )
121
134
  })
122
- })
123
- )
135
+ )
136
+ }))
124
137
  ) {
125
138
  return getDecorations({
126
139
  doc: transaction.doc,
@@ -1,6 +0,0 @@
1
- import { CodeBlockOptions } from '@tiptap/extension-code-block';
2
- export interface CodeBlockLowlightOptions extends CodeBlockOptions {
3
- lowlight: any;
4
- defaultLanguage: string | null | undefined;
5
- }
6
- export declare const CodeBlockLowlight: import("@tiptap/core").Node<CodeBlockLowlightOptions, any>;
@@ -1,3 +0,0 @@
1
- import { CodeBlockLowlight } from './code-block-lowlight';
2
- export * from './code-block-lowlight';
3
- export default CodeBlockLowlight;
@@ -1,6 +0,0 @@
1
- import { Plugin } from 'prosemirror-state';
2
- export declare function LowlightPlugin({ name, lowlight, defaultLanguage }: {
3
- name: string;
4
- lowlight: any;
5
- defaultLanguage: string | null | undefined;
6
- }): Plugin<any>;