@tiptap/extension-code-block 2.0.0-beta.209 → 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/dist/index.cjs ADDED
@@ -0,0 +1,189 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/code-block.ts
2
+ var _core = require('@tiptap/core');
3
+ var _state = require('@tiptap/pm/state');
4
+ var backtickInputRegex = /^```([a-z]+)?[\s\n]$/;
5
+ var tildeInputRegex = /^~~~([a-z]+)?[\s\n]$/;
6
+ var CodeBlock = _core.Node.create({
7
+ name: "codeBlock",
8
+ addOptions() {
9
+ return {
10
+ languageClassPrefix: "language-",
11
+ exitOnTripleEnter: true,
12
+ exitOnArrowDown: true,
13
+ HTMLAttributes: {}
14
+ };
15
+ },
16
+ content: "text*",
17
+ marks: "",
18
+ group: "block",
19
+ code: true,
20
+ defining: true,
21
+ addAttributes() {
22
+ return {
23
+ language: {
24
+ default: null,
25
+ parseHTML: (element) => {
26
+ var _a;
27
+ const { languageClassPrefix } = this.options;
28
+ const classNames = [...((_a = element.firstElementChild) == null ? void 0 : _a.classList) || []];
29
+ const languages = classNames.filter((className) => className.startsWith(languageClassPrefix)).map((className) => className.replace(languageClassPrefix, ""));
30
+ const language = languages[0];
31
+ if (!language) {
32
+ return null;
33
+ }
34
+ return language;
35
+ },
36
+ rendered: false
37
+ }
38
+ };
39
+ },
40
+ parseHTML() {
41
+ return [
42
+ {
43
+ tag: "pre",
44
+ preserveWhitespace: "full"
45
+ }
46
+ ];
47
+ },
48
+ renderHTML({ node, HTMLAttributes }) {
49
+ return [
50
+ "pre",
51
+ _core.mergeAttributes.call(void 0, this.options.HTMLAttributes, HTMLAttributes),
52
+ [
53
+ "code",
54
+ {
55
+ class: node.attrs.language ? this.options.languageClassPrefix + node.attrs.language : null
56
+ },
57
+ 0
58
+ ]
59
+ ];
60
+ },
61
+ addCommands() {
62
+ return {
63
+ setCodeBlock: (attributes) => ({ commands }) => {
64
+ return commands.setNode(this.name, attributes);
65
+ },
66
+ toggleCodeBlock: (attributes) => ({ commands }) => {
67
+ return commands.toggleNode(this.name, "paragraph", attributes);
68
+ }
69
+ };
70
+ },
71
+ addKeyboardShortcuts() {
72
+ return {
73
+ "Mod-Alt-c": () => this.editor.commands.toggleCodeBlock(),
74
+ Backspace: () => {
75
+ const { empty, $anchor } = this.editor.state.selection;
76
+ const isAtStart = $anchor.pos === 1;
77
+ if (!empty || $anchor.parent.type.name !== this.name) {
78
+ return false;
79
+ }
80
+ if (isAtStart || !$anchor.parent.textContent.length) {
81
+ return this.editor.commands.clearNodes();
82
+ }
83
+ return false;
84
+ },
85
+ Enter: ({ editor }) => {
86
+ if (!this.options.exitOnTripleEnter) {
87
+ return false;
88
+ }
89
+ const { state } = editor;
90
+ const { selection } = state;
91
+ const { $from, empty } = selection;
92
+ if (!empty || $from.parent.type !== this.type) {
93
+ return false;
94
+ }
95
+ const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2;
96
+ const endsWithDoubleNewline = $from.parent.textContent.endsWith("\n\n");
97
+ if (!isAtEnd || !endsWithDoubleNewline) {
98
+ return false;
99
+ }
100
+ return editor.chain().command(({ tr }) => {
101
+ tr.delete($from.pos - 2, $from.pos);
102
+ return true;
103
+ }).exitCode().run();
104
+ },
105
+ ArrowDown: ({ editor }) => {
106
+ if (!this.options.exitOnArrowDown) {
107
+ return false;
108
+ }
109
+ const { state } = editor;
110
+ const { selection, doc } = state;
111
+ const { $from, empty } = selection;
112
+ if (!empty || $from.parent.type !== this.type) {
113
+ return false;
114
+ }
115
+ const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2;
116
+ if (!isAtEnd) {
117
+ return false;
118
+ }
119
+ const after = $from.after();
120
+ if (after === void 0) {
121
+ return false;
122
+ }
123
+ const nodeAfter = doc.nodeAt(after);
124
+ if (nodeAfter) {
125
+ return false;
126
+ }
127
+ return editor.commands.exitCode();
128
+ }
129
+ };
130
+ },
131
+ addInputRules() {
132
+ return [
133
+ _core.textblockTypeInputRule.call(void 0, {
134
+ find: backtickInputRegex,
135
+ type: this.type,
136
+ getAttributes: (match) => ({
137
+ language: match[1]
138
+ })
139
+ }),
140
+ _core.textblockTypeInputRule.call(void 0, {
141
+ find: tildeInputRegex,
142
+ type: this.type,
143
+ getAttributes: (match) => ({
144
+ language: match[1]
145
+ })
146
+ })
147
+ ];
148
+ },
149
+ addProseMirrorPlugins() {
150
+ return [
151
+ new (0, _state.Plugin)({
152
+ key: new (0, _state.PluginKey)("codeBlockVSCodeHandler"),
153
+ props: {
154
+ handlePaste: (view, event) => {
155
+ if (!event.clipboardData) {
156
+ return false;
157
+ }
158
+ if (this.editor.isActive(this.type.name)) {
159
+ return false;
160
+ }
161
+ const text = event.clipboardData.getData("text/plain");
162
+ const vscode = event.clipboardData.getData("vscode-editor-data");
163
+ const vscodeData = vscode ? JSON.parse(vscode) : void 0;
164
+ const language = vscodeData == null ? void 0 : vscodeData.mode;
165
+ if (!text || !language) {
166
+ return false;
167
+ }
168
+ const { tr } = view.state;
169
+ tr.replaceSelectionWith(this.type.create({ language }));
170
+ tr.setSelection(_state.TextSelection.near(tr.doc.resolve(Math.max(0, tr.selection.from - 2))));
171
+ tr.insertText(text.replace(/\r\n?/g, "\n"));
172
+ tr.setMeta("paste", true);
173
+ view.dispatch(tr);
174
+ return true;
175
+ }
176
+ }
177
+ })
178
+ ];
179
+ }
180
+ });
181
+
182
+ // src/index.ts
183
+ var src_default = CodeBlock;
184
+
185
+
186
+
187
+
188
+
189
+ exports.CodeBlock = CodeBlock; exports.backtickInputRegex = backtickInputRegex; exports.default = src_default; exports.tildeInputRegex = tildeInputRegex;
@@ -1,43 +1,46 @@
1
- import { Node } from '@tiptap/core';
2
- export interface CodeBlockOptions {
3
- /**
4
- * Adds a prefix to language classes that are applied to code tags.
5
- * Defaults to `'language-'`.
6
- */
7
- languageClassPrefix: string;
8
- /**
9
- * Define whether the node should be exited on triple enter.
10
- * Defaults to `true`.
11
- */
12
- exitOnTripleEnter: boolean;
13
- /**
14
- * Define whether the node should be exited on arrow down if there is no node after it.
15
- * Defaults to `true`.
16
- */
17
- exitOnArrowDown: boolean;
18
- /**
19
- * Custom HTML attributes that should be added to the rendered HTML tag.
20
- */
21
- HTMLAttributes: Record<string, any>;
22
- }
23
- declare module '@tiptap/core' {
24
- interface Commands<ReturnType> {
25
- codeBlock: {
26
- /**
27
- * Set a code block
28
- */
29
- setCodeBlock: (attributes?: {
30
- language: string;
31
- }) => ReturnType;
32
- /**
33
- * Toggle a code block
34
- */
35
- toggleCodeBlock: (attributes?: {
36
- language: string;
37
- }) => ReturnType;
38
- };
39
- }
40
- }
41
- export declare const backtickInputRegex: RegExp;
42
- export declare const tildeInputRegex: RegExp;
43
- export declare const CodeBlock: Node<CodeBlockOptions, any>;
1
+ import { Node } from '@tiptap/core';
2
+
3
+ interface CodeBlockOptions {
4
+ /**
5
+ * Adds a prefix to language classes that are applied to code tags.
6
+ * Defaults to `'language-'`.
7
+ */
8
+ languageClassPrefix: string;
9
+ /**
10
+ * Define whether the node should be exited on triple enter.
11
+ * Defaults to `true`.
12
+ */
13
+ exitOnTripleEnter: boolean;
14
+ /**
15
+ * Define whether the node should be exited on arrow down if there is no node after it.
16
+ * Defaults to `true`.
17
+ */
18
+ exitOnArrowDown: boolean;
19
+ /**
20
+ * Custom HTML attributes that should be added to the rendered HTML tag.
21
+ */
22
+ HTMLAttributes: Record<string, any>;
23
+ }
24
+ declare module '@tiptap/core' {
25
+ interface Commands<ReturnType> {
26
+ codeBlock: {
27
+ /**
28
+ * Set a code block
29
+ */
30
+ setCodeBlock: (attributes?: {
31
+ language: string;
32
+ }) => ReturnType;
33
+ /**
34
+ * Toggle a code block
35
+ */
36
+ toggleCodeBlock: (attributes?: {
37
+ language: string;
38
+ }) => ReturnType;
39
+ };
40
+ }
41
+ }
42
+ declare const backtickInputRegex: RegExp;
43
+ declare const tildeInputRegex: RegExp;
44
+ declare const CodeBlock: Node<CodeBlockOptions, any>;
45
+
46
+ export { CodeBlock, CodeBlockOptions, backtickInputRegex, CodeBlock as default, tildeInputRegex };
package/dist/index.js ADDED
@@ -0,0 +1,189 @@
1
+ // src/code-block.ts
2
+ import { mergeAttributes, Node, textblockTypeInputRule } from "@tiptap/core";
3
+ import { Plugin, PluginKey, TextSelection } from "@tiptap/pm/state";
4
+ var backtickInputRegex = /^```([a-z]+)?[\s\n]$/;
5
+ var tildeInputRegex = /^~~~([a-z]+)?[\s\n]$/;
6
+ var CodeBlock = Node.create({
7
+ name: "codeBlock",
8
+ addOptions() {
9
+ return {
10
+ languageClassPrefix: "language-",
11
+ exitOnTripleEnter: true,
12
+ exitOnArrowDown: true,
13
+ HTMLAttributes: {}
14
+ };
15
+ },
16
+ content: "text*",
17
+ marks: "",
18
+ group: "block",
19
+ code: true,
20
+ defining: true,
21
+ addAttributes() {
22
+ return {
23
+ language: {
24
+ default: null,
25
+ parseHTML: (element) => {
26
+ var _a;
27
+ const { languageClassPrefix } = this.options;
28
+ const classNames = [...((_a = element.firstElementChild) == null ? void 0 : _a.classList) || []];
29
+ const languages = classNames.filter((className) => className.startsWith(languageClassPrefix)).map((className) => className.replace(languageClassPrefix, ""));
30
+ const language = languages[0];
31
+ if (!language) {
32
+ return null;
33
+ }
34
+ return language;
35
+ },
36
+ rendered: false
37
+ }
38
+ };
39
+ },
40
+ parseHTML() {
41
+ return [
42
+ {
43
+ tag: "pre",
44
+ preserveWhitespace: "full"
45
+ }
46
+ ];
47
+ },
48
+ renderHTML({ node, HTMLAttributes }) {
49
+ return [
50
+ "pre",
51
+ mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
52
+ [
53
+ "code",
54
+ {
55
+ class: node.attrs.language ? this.options.languageClassPrefix + node.attrs.language : null
56
+ },
57
+ 0
58
+ ]
59
+ ];
60
+ },
61
+ addCommands() {
62
+ return {
63
+ setCodeBlock: (attributes) => ({ commands }) => {
64
+ return commands.setNode(this.name, attributes);
65
+ },
66
+ toggleCodeBlock: (attributes) => ({ commands }) => {
67
+ return commands.toggleNode(this.name, "paragraph", attributes);
68
+ }
69
+ };
70
+ },
71
+ addKeyboardShortcuts() {
72
+ return {
73
+ "Mod-Alt-c": () => this.editor.commands.toggleCodeBlock(),
74
+ Backspace: () => {
75
+ const { empty, $anchor } = this.editor.state.selection;
76
+ const isAtStart = $anchor.pos === 1;
77
+ if (!empty || $anchor.parent.type.name !== this.name) {
78
+ return false;
79
+ }
80
+ if (isAtStart || !$anchor.parent.textContent.length) {
81
+ return this.editor.commands.clearNodes();
82
+ }
83
+ return false;
84
+ },
85
+ Enter: ({ editor }) => {
86
+ if (!this.options.exitOnTripleEnter) {
87
+ return false;
88
+ }
89
+ const { state } = editor;
90
+ const { selection } = state;
91
+ const { $from, empty } = selection;
92
+ if (!empty || $from.parent.type !== this.type) {
93
+ return false;
94
+ }
95
+ const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2;
96
+ const endsWithDoubleNewline = $from.parent.textContent.endsWith("\n\n");
97
+ if (!isAtEnd || !endsWithDoubleNewline) {
98
+ return false;
99
+ }
100
+ return editor.chain().command(({ tr }) => {
101
+ tr.delete($from.pos - 2, $from.pos);
102
+ return true;
103
+ }).exitCode().run();
104
+ },
105
+ ArrowDown: ({ editor }) => {
106
+ if (!this.options.exitOnArrowDown) {
107
+ return false;
108
+ }
109
+ const { state } = editor;
110
+ const { selection, doc } = state;
111
+ const { $from, empty } = selection;
112
+ if (!empty || $from.parent.type !== this.type) {
113
+ return false;
114
+ }
115
+ const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2;
116
+ if (!isAtEnd) {
117
+ return false;
118
+ }
119
+ const after = $from.after();
120
+ if (after === void 0) {
121
+ return false;
122
+ }
123
+ const nodeAfter = doc.nodeAt(after);
124
+ if (nodeAfter) {
125
+ return false;
126
+ }
127
+ return editor.commands.exitCode();
128
+ }
129
+ };
130
+ },
131
+ addInputRules() {
132
+ return [
133
+ textblockTypeInputRule({
134
+ find: backtickInputRegex,
135
+ type: this.type,
136
+ getAttributes: (match) => ({
137
+ language: match[1]
138
+ })
139
+ }),
140
+ textblockTypeInputRule({
141
+ find: tildeInputRegex,
142
+ type: this.type,
143
+ getAttributes: (match) => ({
144
+ language: match[1]
145
+ })
146
+ })
147
+ ];
148
+ },
149
+ addProseMirrorPlugins() {
150
+ return [
151
+ new Plugin({
152
+ key: new PluginKey("codeBlockVSCodeHandler"),
153
+ props: {
154
+ handlePaste: (view, event) => {
155
+ if (!event.clipboardData) {
156
+ return false;
157
+ }
158
+ if (this.editor.isActive(this.type.name)) {
159
+ return false;
160
+ }
161
+ const text = event.clipboardData.getData("text/plain");
162
+ const vscode = event.clipboardData.getData("vscode-editor-data");
163
+ const vscodeData = vscode ? JSON.parse(vscode) : void 0;
164
+ const language = vscodeData == null ? void 0 : vscodeData.mode;
165
+ if (!text || !language) {
166
+ return false;
167
+ }
168
+ const { tr } = view.state;
169
+ tr.replaceSelectionWith(this.type.create({ language }));
170
+ tr.setSelection(TextSelection.near(tr.doc.resolve(Math.max(0, tr.selection.from - 2))));
171
+ tr.insertText(text.replace(/\r\n?/g, "\n"));
172
+ tr.setMeta("paste", true);
173
+ view.dispatch(tr);
174
+ return true;
175
+ }
176
+ }
177
+ })
178
+ ];
179
+ }
180
+ });
181
+
182
+ // src/index.ts
183
+ var src_default = CodeBlock;
184
+ export {
185
+ CodeBlock,
186
+ backtickInputRegex,
187
+ src_default as default,
188
+ tildeInputRegex
189
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tiptap/extension-code-block",
3
3
  "description": "code block extension for tiptap",
4
- "version": "2.0.0-beta.209",
4
+ "version": "2.0.0-beta.210",
5
5
  "homepage": "https://tiptap.dev",
6
6
  "keywords": [
7
7
  "tiptap",
@@ -15,30 +15,43 @@
15
15
  "type": "module",
16
16
  "exports": {
17
17
  ".": {
18
- "types": "./dist/packages/extension-code-block/src/index.d.ts",
19
- "import": "./dist/tiptap-extension-code-block.esm.js",
20
- "require": "./dist/tiptap-extension-code-block.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.cjs",
24
- "umd": "dist/tiptap-extension-code-block.umd.js",
25
- "module": "dist/tiptap-extension-code-block.esm.js",
26
- "types": "dist/packages/extension-code-block/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
- "prosemirror-state": "^1.4.1"
31
+ "@tiptap/core": "^2.0.0-beta.209",
32
+ "@tiptap/pm": "^2.0.0-beta.209"
34
33
  },
35
34
  "devDependencies": {
36
- "@tiptap/core": "^2.0.0-beta.209",
37
- "prosemirror-state": "^1.4.1"
35
+ "@tiptap/core": "^2.0.0-beta.210",
36
+ "@tiptap/pm": "^2.0.0-beta.210"
38
37
  },
39
38
  "repository": {
40
39
  "type": "git",
41
40
  "url": "https://github.com/ueberdosis/tiptap",
42
41
  "directory": "packages/extension-code-block"
42
+ },
43
+ "scripts": {
44
+ "build": "tsup"
45
+ },
46
+ "tsup": {
47
+ "entry": [
48
+ "src/index.ts"
49
+ ],
50
+ "dts": true,
51
+ "splitting": true,
52
+ "format": [
53
+ "esm",
54
+ "cjs"
55
+ ]
43
56
  }
44
57
  }
package/src/code-block.ts CHANGED
@@ -1,26 +1,26 @@
1
1
  import { mergeAttributes, Node, textblockTypeInputRule } from '@tiptap/core'
2
- import { Plugin, PluginKey, TextSelection } from 'prosemirror-state'
2
+ import { Plugin, PluginKey, TextSelection } from '@tiptap/pm/state'
3
3
 
4
4
  export interface CodeBlockOptions {
5
5
  /**
6
6
  * Adds a prefix to language classes that are applied to code tags.
7
7
  * Defaults to `'language-'`.
8
8
  */
9
- languageClassPrefix: string,
9
+ languageClassPrefix: string
10
10
  /**
11
11
  * Define whether the node should be exited on triple enter.
12
12
  * Defaults to `true`.
13
13
  */
14
- exitOnTripleEnter: boolean,
14
+ exitOnTripleEnter: boolean
15
15
  /**
16
16
  * Define whether the node should be exited on arrow down if there is no node after it.
17
17
  * Defaults to `true`.
18
18
  */
19
- exitOnArrowDown: boolean,
19
+ exitOnArrowDown: boolean
20
20
  /**
21
21
  * Custom HTML attributes that should be added to the rendered HTML tag.
22
22
  */
23
- HTMLAttributes: Record<string, any>,
23
+ HTMLAttributes: Record<string, any>
24
24
  }
25
25
 
26
26
  declare module '@tiptap/core' {
@@ -29,11 +29,11 @@ declare module '@tiptap/core' {
29
29
  /**
30
30
  * Set a code block
31
31
  */
32
- setCodeBlock: (attributes?: { language: string }) => ReturnType,
32
+ setCodeBlock: (attributes?: { language: string }) => ReturnType
33
33
  /**
34
34
  * Toggle a code block
35
35
  */
36
- toggleCodeBlock: (attributes?: { language: string }) => ReturnType,
36
+ toggleCodeBlock: (attributes?: { language: string }) => ReturnType
37
37
  }
38
38
  }
39
39
  }
@@ -69,7 +69,7 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
69
69
  default: null,
70
70
  parseHTML: element => {
71
71
  const { languageClassPrefix } = this.options
72
- const classNames = [...element.firstElementChild?.classList || []]
72
+ const classNames = [...(element.firstElementChild?.classList || [])]
73
73
  const languages = classNames
74
74
  .filter(className => className.startsWith(languageClassPrefix))
75
75
  .map(className => className.replace(languageClassPrefix, ''))
@@ -113,12 +113,14 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
113
113
 
114
114
  addCommands() {
115
115
  return {
116
- setCodeBlock: attributes => ({ commands }) => {
117
- return commands.setNode(this.name, attributes)
118
- },
119
- toggleCodeBlock: attributes => ({ commands }) => {
120
- return commands.toggleNode(this.name, 'paragraph', attributes)
121
- },
116
+ setCodeBlock:
117
+ attributes => ({ commands }) => {
118
+ return commands.setNode(this.name, attributes)
119
+ },
120
+ toggleCodeBlock:
121
+ attributes => ({ commands }) => {
122
+ return commands.toggleNode(this.name, 'paragraph', attributes)
123
+ },
122
124
  }
123
125
  },
124
126
 
@@ -249,9 +251,7 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
249
251
 
250
252
  const text = event.clipboardData.getData('text/plain')
251
253
  const vscode = event.clipboardData.getData('vscode-editor-data')
252
- const vscodeData = vscode
253
- ? JSON.parse(vscode)
254
- : undefined
254
+ const vscodeData = vscode ? JSON.parse(vscode) : undefined
255
255
  const language = vscodeData?.mode
256
256
 
257
257
  if (!text || !language) {
@@ -1,3 +0,0 @@
1
- import { CodeBlock } from './code-block';
2
- export * from './code-block';
3
- export default CodeBlock;