@tiptap/extension-code-block-lowlight 2.0.0-beta.47 → 2.0.0-beta.51

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.47",
4
+ "version": "2.0.0-beta.51",
5
5
  "homepage": "https://tiptap.dev",
6
6
  "keywords": [
7
7
  "tiptap",
@@ -24,17 +24,17 @@
24
24
  "@tiptap/core": "^2.0.0-beta.1"
25
25
  },
26
26
  "dependencies": {
27
- "@tiptap/extension-code-block": "^2.0.0-beta.24",
27
+ "@tiptap/extension-code-block": "^2.0.0-beta.26",
28
28
  "@types/lowlight": "^0.0.3",
29
29
  "lowlight": "^1.20.0",
30
- "prosemirror-model": "^1.14.3",
30
+ "prosemirror-model": "^1.15.0",
31
31
  "prosemirror-state": "^1.3.4",
32
- "prosemirror-view": "^1.20.3"
32
+ "prosemirror-view": "^1.21.0"
33
33
  },
34
34
  "repository": {
35
35
  "type": "git",
36
36
  "url": "https://github.com/ueberdosis/tiptap",
37
37
  "directory": "packages/extension-code-block-lowlight"
38
38
  },
39
- "gitHead": "9948e2499a3aa6ca72b677ef2ca96de1db1cb6b5"
39
+ "gitHead": "3c32e66c02efb30d7e083e06cdf81bceb3582a33"
40
40
  }
@@ -4,12 +4,16 @@ import { LowlightPlugin } from './lowlight-plugin'
4
4
 
5
5
  export interface CodeBlockLowlightOptions extends CodeBlockOptions {
6
6
  lowlight: any,
7
+ defaultLanguage: string | null | undefined,
7
8
  }
8
9
 
9
10
  export const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({
10
- defaultOptions: {
11
- ...CodeBlock.options,
12
- lowlight,
11
+ addOptions() {
12
+ return {
13
+ ...this.parent?.(),
14
+ lowlight,
15
+ defaultLanguage: null,
16
+ }
13
17
  },
14
18
 
15
19
  addProseMirrorPlugins() {
@@ -18,6 +22,7 @@ export const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({
18
22
  LowlightPlugin({
19
23
  name: this.name,
20
24
  lowlight: this.options.lowlight,
25
+ defaultLanguage: this.options.defaultLanguage,
21
26
  }),
22
27
  ]
23
28
  },
@@ -30,13 +30,19 @@ function getHighlightNodes(result: any) {
30
30
  return result.value || result.children || []
31
31
  }
32
32
 
33
- function getDecorations({ doc, name, lowlight }: { doc: ProsemirrorNode, name: string, lowlight: any }) {
33
+ function getDecorations({
34
+ doc,
35
+ name,
36
+ lowlight,
37
+ defaultLanguage,
38
+ }: { doc: ProsemirrorNode, name: string, lowlight: any, defaultLanguage: string | null | undefined }) {
34
39
  const decorations: Decoration[] = []
35
40
 
36
41
  findChildren(doc, node => node.type.name === name)
37
42
  .forEach(block => {
38
43
  let from = block.pos + 1
39
- const { language } = block.node.attrs
44
+ const language = block.node.attrs.language || defaultLanguage
45
+ console.log({ language, defaultLanguage })
40
46
  const languages = lowlight.listLanguages()
41
47
  const nodes = language && languages.includes(language)
42
48
  ? getHighlightNodes(lowlight.highlight(language, block.node.textContent))
@@ -60,12 +66,17 @@ function getDecorations({ doc, name, lowlight }: { doc: ProsemirrorNode, name: s
60
66
  return DecorationSet.create(doc, decorations)
61
67
  }
62
68
 
63
- export function LowlightPlugin({ name, lowlight }: { name: string, lowlight: any }) {
69
+ export function LowlightPlugin({ name, lowlight, defaultLanguage }: { name: string, lowlight: any, defaultLanguage: string | null | undefined }) {
64
70
  return new Plugin({
65
71
  key: new PluginKey('lowlight'),
66
72
 
67
73
  state: {
68
- init: (_, { doc }) => getDecorations({ doc, name, lowlight }),
74
+ init: (_, { doc }) => getDecorations({
75
+ doc,
76
+ name,
77
+ lowlight,
78
+ defaultLanguage,
79
+ }),
69
80
  apply: (transaction, decorationSet, oldState, newState) => {
70
81
  const oldNodeName = oldState.selection.$head.parent.type.name
71
82
  const newNodeName = newState.selection.$head.parent.type.name
@@ -97,7 +108,12 @@ export function LowlightPlugin({ name, lowlight }: { name: string, lowlight: any
97
108
  })
98
109
  )
99
110
  ) {
100
- return getDecorations({ doc: transaction.doc, name, lowlight })
111
+ return getDecorations({
112
+ doc: transaction.doc,
113
+ name,
114
+ lowlight,
115
+ defaultLanguage,
116
+ })
101
117
  }
102
118
 
103
119
  return decorationSet.map(transaction.mapping, transaction.doc)