@tiptap/extension-code-block-lowlight 3.0.0-next.3 → 3.0.0-next.4
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/LICENSE.md +21 -0
- package/README.md +5 -1
- package/dist/index.cjs +17 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +14 -8
- package/dist/index.js.map +1 -1
- package/package.json +12 -8
- package/src/code-block-lowlight.ts +2 -2
- package/src/lowlight-plugin.ts +23 -22
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024, Tiptap GmbH
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
# @tiptap/extension-code-block-lowlight
|
|
2
|
+
|
|
2
3
|
[](https://www.npmjs.com/package/@tiptap/extension-code-block-lowlight)
|
|
3
4
|
[](https://npmcharts.com/compare/tiptap?minimal=true)
|
|
4
5
|
[](https://www.npmjs.com/package/@tiptap/extension-code-block-lowlight)
|
|
5
6
|
[](https://github.com/sponsors/ueberdosis)
|
|
6
7
|
|
|
7
8
|
## Introduction
|
|
8
|
-
|
|
9
|
+
|
|
10
|
+
Tiptap is a headless wrapper around [ProseMirror](https://ProseMirror.net) – a toolkit for building rich text WYSIWYG editors, which is already in use at many well-known companies such as _New York Times_, _The Guardian_ or _Atlassian_.
|
|
9
11
|
|
|
10
12
|
## Official Documentation
|
|
13
|
+
|
|
11
14
|
Documentation can be found on the [Tiptap website](https://tiptap.dev).
|
|
12
15
|
|
|
13
16
|
## License
|
|
17
|
+
|
|
14
18
|
Tiptap is open sourced software licensed under the [MIT license](https://github.com/ueberdosis/tiptap/blob/main/LICENSE.md).
|
package/dist/index.cjs
CHANGED
|
@@ -28,12 +28,12 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
29
|
|
|
30
30
|
// src/index.ts
|
|
31
|
-
var
|
|
32
|
-
__export(
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
33
|
CodeBlockLowlight: () => CodeBlockLowlight,
|
|
34
|
-
default: () =>
|
|
34
|
+
default: () => index_default
|
|
35
35
|
});
|
|
36
|
-
module.exports = __toCommonJS(
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
37
|
|
|
38
38
|
// src/code-block-lowlight.ts
|
|
39
39
|
var import_extension_code_block = __toESM(require("@tiptap/extension-code-block"), 1);
|
|
@@ -96,9 +96,7 @@ function LowlightPlugin({
|
|
|
96
96
|
defaultLanguage
|
|
97
97
|
}) {
|
|
98
98
|
if (!["highlight", "highlightAuto", "listLanguages"].every((api) => isFunction(lowlight[api]))) {
|
|
99
|
-
throw Error(
|
|
100
|
-
"You should provide an instance of lowlight to use the code-block-lowlight extension"
|
|
101
|
-
);
|
|
99
|
+
throw Error("You should provide an instance of lowlight to use the code-block-lowlight extension");
|
|
102
100
|
}
|
|
103
101
|
const lowlightPlugin = new import_state.Plugin({
|
|
104
102
|
key: new import_state.PluginKey("lowlight"),
|
|
@@ -114,13 +112,21 @@ function LowlightPlugin({
|
|
|
114
112
|
const newNodeName = newState.selection.$head.parent.type.name;
|
|
115
113
|
const oldNodes = (0, import_core.findChildren)(oldState.doc, (node) => node.type.name === name);
|
|
116
114
|
const newNodes = (0, import_core.findChildren)(newState.doc, (node) => node.type.name === name);
|
|
117
|
-
if (transaction.docChanged &&
|
|
115
|
+
if (transaction.docChanged && // Apply decorations if:
|
|
116
|
+
// selection includes named node,
|
|
117
|
+
([oldNodeName, newNodeName].includes(name) || // OR transaction adds/removes named node,
|
|
118
|
+
newNodes.length !== oldNodes.length || // OR transaction has changes that completely encapsulte a node
|
|
119
|
+
// (for example, a transaction that affects the entire document).
|
|
120
|
+
// Such transactions can happen during collab syncing via y-prosemirror, for example.
|
|
121
|
+
transaction.steps.some((step) => {
|
|
118
122
|
return (
|
|
119
123
|
// @ts-ignore
|
|
120
|
-
step.from !== void 0 &&
|
|
124
|
+
step.from !== void 0 && // @ts-ignore
|
|
125
|
+
step.to !== void 0 && oldNodes.some((node) => {
|
|
121
126
|
return (
|
|
122
127
|
// @ts-ignore
|
|
123
|
-
node.pos >= step.from &&
|
|
128
|
+
node.pos >= step.from && // @ts-ignore
|
|
129
|
+
node.pos + node.node.nodeSize <= step.to
|
|
124
130
|
);
|
|
125
131
|
})
|
|
126
132
|
);
|
|
@@ -172,7 +178,7 @@ var CodeBlockLowlight = import_extension_code_block.default.extend({
|
|
|
172
178
|
});
|
|
173
179
|
|
|
174
180
|
// src/index.ts
|
|
175
|
-
var
|
|
181
|
+
var index_default = CodeBlockLowlight;
|
|
176
182
|
// Annotate the CommonJS export names for ESM import in node:
|
|
177
183
|
0 && (module.exports = {
|
|
178
184
|
CodeBlockLowlight
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/code-block-lowlight.ts","../src/lowlight-plugin.ts"],"sourcesContent":["import { CodeBlockLowlight } from './code-block-lowlight.js'\n\nexport * from './code-block-lowlight.js'\n\nexport default CodeBlockLowlight\n","import CodeBlock, { CodeBlockOptions } from '@tiptap/extension-code-block'\n\nimport { LowlightPlugin } from './lowlight-plugin.js'\n\nexport interface CodeBlockLowlightOptions extends CodeBlockOptions {\n /**\n * The lowlight instance.\n */\n lowlight: any
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/code-block-lowlight.ts","../src/lowlight-plugin.ts"],"sourcesContent":["import { CodeBlockLowlight } from './code-block-lowlight.js'\n\nexport * from './code-block-lowlight.js'\n\nexport default CodeBlockLowlight\n","import CodeBlock, { CodeBlockOptions } from '@tiptap/extension-code-block'\n\nimport { LowlightPlugin } from './lowlight-plugin.js'\n\nexport interface CodeBlockLowlightOptions extends CodeBlockOptions {\n /**\n * The lowlight instance.\n */\n lowlight: any\n}\n\n/**\n * This extension allows you to highlight code blocks with lowlight.\n * @see https://tiptap.dev/api/nodes/code-block-lowlight\n */\nexport const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({\n addOptions() {\n return {\n ...this.parent?.(),\n lowlight: {},\n languageClassPrefix: 'language-',\n exitOnTripleEnter: true,\n exitOnArrowDown: true,\n defaultLanguage: null,\n HTMLAttributes: {},\n }\n },\n\n addProseMirrorPlugins() {\n return [\n ...(this.parent?.() || []),\n LowlightPlugin({\n name: this.name,\n lowlight: this.options.lowlight,\n defaultLanguage: this.options.defaultLanguage,\n }),\n ]\n },\n})\n","import { findChildren } from '@tiptap/core'\nimport { Node as ProsemirrorNode } from '@tiptap/pm/model'\nimport { Plugin, PluginKey } from '@tiptap/pm/state'\nimport { Decoration, DecorationSet } from '@tiptap/pm/view'\n// @ts-ignore\nimport highlight from 'highlight.js/lib/core'\n\nfunction parseNodes(nodes: any[], className: string[] = []): { text: string; classes: string[] }[] {\n return nodes\n .map(node => {\n const classes = [...className, ...(node.properties ? node.properties.className : [])]\n\n if (node.children) {\n return parseNodes(node.children, classes)\n }\n\n return {\n text: node.value,\n classes,\n }\n })\n .flat()\n}\n\nfunction getHighlightNodes(result: any) {\n // `.value` for lowlight v1, `.children` for lowlight v2\n return result.value || result.children || []\n}\n\nfunction registered(aliasOrLanguage: string) {\n return Boolean(highlight.getLanguage(aliasOrLanguage))\n}\n\nfunction getDecorations({\n doc,\n name,\n lowlight,\n defaultLanguage,\n}: {\n doc: ProsemirrorNode\n name: string\n lowlight: any\n defaultLanguage: string | null | undefined\n}) {\n const decorations: Decoration[] = []\n\n findChildren(doc, node => node.type.name === name).forEach(block => {\n let from = block.pos + 1\n const language = block.node.attrs.language || defaultLanguage\n const languages = lowlight.listLanguages()\n\n const nodes =\n language && (languages.includes(language) || registered(language) || lowlight.registered?.(language))\n ? getHighlightNodes(lowlight.highlight(language, block.node.textContent))\n : getHighlightNodes(lowlight.highlightAuto(block.node.textContent))\n\n parseNodes(nodes).forEach(node => {\n const to = from + node.text.length\n\n if (node.classes.length) {\n const decoration = Decoration.inline(from, to, {\n class: node.classes.join(' '),\n })\n\n decorations.push(decoration)\n }\n\n from = to\n })\n })\n\n return DecorationSet.create(doc, decorations)\n}\n\n// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\nfunction isFunction(param: any): param is Function {\n return typeof param === 'function'\n}\n\nexport function LowlightPlugin({\n name,\n lowlight,\n defaultLanguage,\n}: {\n name: string\n lowlight: any\n defaultLanguage: string | null | undefined\n}) {\n if (!['highlight', 'highlightAuto', 'listLanguages'].every(api => isFunction(lowlight[api]))) {\n throw Error('You should provide an instance of lowlight to use the code-block-lowlight extension')\n }\n\n const lowlightPlugin: Plugin<any> = new Plugin({\n key: new PluginKey('lowlight'),\n\n state: {\n init: (_, { doc }) =>\n getDecorations({\n doc,\n name,\n lowlight,\n defaultLanguage,\n }),\n apply: (transaction, decorationSet, oldState, newState) => {\n const oldNodeName = oldState.selection.$head.parent.type.name\n const newNodeName = newState.selection.$head.parent.type.name\n const oldNodes = findChildren(oldState.doc, node => node.type.name === name)\n const newNodes = findChildren(newState.doc, node => node.type.name === name)\n\n if (\n transaction.docChanged &&\n // Apply decorations if:\n // selection includes named node,\n ([oldNodeName, newNodeName].includes(name) ||\n // OR transaction adds/removes named node,\n newNodes.length !== oldNodes.length ||\n // OR transaction has changes that completely encapsulte a node\n // (for example, a transaction that affects the entire document).\n // Such transactions can happen during collab syncing via y-prosemirror, for example.\n transaction.steps.some(step => {\n // @ts-ignore\n return (\n // @ts-ignore\n step.from !== undefined &&\n // @ts-ignore\n step.to !== undefined &&\n oldNodes.some(node => {\n // @ts-ignore\n return (\n // @ts-ignore\n node.pos >= step.from &&\n // @ts-ignore\n node.pos + node.node.nodeSize <= step.to\n )\n })\n )\n }))\n ) {\n return getDecorations({\n doc: transaction.doc,\n name,\n lowlight,\n defaultLanguage,\n })\n }\n\n return decorationSet.map(transaction.mapping, transaction.doc)\n },\n },\n\n props: {\n decorations(state) {\n return lowlightPlugin.getState(state)\n },\n },\n })\n\n return lowlightPlugin\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kCAA4C;;;ACA5C,kBAA6B;AAE7B,mBAAkC;AAClC,kBAA0C;AAE1C,IAAAA,eAAsB;AAEtB,SAAS,WAAW,OAAc,YAAsB,CAAC,GAA0C;AACjG,SAAO,MACJ,IAAI,UAAQ;AACX,UAAM,UAAU,CAAC,GAAG,WAAW,GAAI,KAAK,aAAa,KAAK,WAAW,YAAY,CAAC,CAAE;AAEpF,QAAI,KAAK,UAAU;AACjB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;AACjB,eAAO,eAAe,SAAS,KAAK;AAAA,MACtC;AAAA,IACF;AAAA,EACF,CAAC;AAED,SAAO;AACT;;;AD/IO,IAAM,oBAAoB,4BAAAC,QAAU,OAAiC;AAAA,EAC1E,aAAa;AAhBf;AAiBI,WAAO;AAAA,MACL,IAAG,UAAK,WAAL;AAAA,MACH,UAAU,CAAC;AAAA,MACX,qBAAqB;AAAA,MACrB,mBAAmB;AAAA,MACnB,iBAAiB;AAAA,MACjB,iBAAiB;AAAA,MACjB,gBAAgB,CAAC;AAAA,IACnB;AAAA,EACF;AAAA,EAEA,wBAAwB;AA5B1B;AA6BI,WAAO;AAAA,MACL,KAAI,UAAK,WAAL,kCAAmB,CAAC;AAAA,MACxB,eAAe;AAAA,QACb,MAAM,KAAK;AAAA,QACX,UAAU,KAAK,QAAQ;AAAA,QACvB,iBAAiB,KAAK,QAAQ;AAAA,MAChC,CAAC;AAAA,IACH;AAAA,EACF;AACF,CAAC;;;ADlCD,IAAO,gBAAQ;","names":["import_core","highlight","CodeBlock"]}
|
package/dist/index.js
CHANGED
|
@@ -59,9 +59,7 @@ function LowlightPlugin({
|
|
|
59
59
|
defaultLanguage
|
|
60
60
|
}) {
|
|
61
61
|
if (!["highlight", "highlightAuto", "listLanguages"].every((api) => isFunction(lowlight[api]))) {
|
|
62
|
-
throw Error(
|
|
63
|
-
"You should provide an instance of lowlight to use the code-block-lowlight extension"
|
|
64
|
-
);
|
|
62
|
+
throw Error("You should provide an instance of lowlight to use the code-block-lowlight extension");
|
|
65
63
|
}
|
|
66
64
|
const lowlightPlugin = new Plugin({
|
|
67
65
|
key: new PluginKey("lowlight"),
|
|
@@ -77,13 +75,21 @@ function LowlightPlugin({
|
|
|
77
75
|
const newNodeName = newState.selection.$head.parent.type.name;
|
|
78
76
|
const oldNodes = findChildren(oldState.doc, (node) => node.type.name === name);
|
|
79
77
|
const newNodes = findChildren(newState.doc, (node) => node.type.name === name);
|
|
80
|
-
if (transaction.docChanged &&
|
|
78
|
+
if (transaction.docChanged && // Apply decorations if:
|
|
79
|
+
// selection includes named node,
|
|
80
|
+
([oldNodeName, newNodeName].includes(name) || // OR transaction adds/removes named node,
|
|
81
|
+
newNodes.length !== oldNodes.length || // OR transaction has changes that completely encapsulte a node
|
|
82
|
+
// (for example, a transaction that affects the entire document).
|
|
83
|
+
// Such transactions can happen during collab syncing via y-prosemirror, for example.
|
|
84
|
+
transaction.steps.some((step) => {
|
|
81
85
|
return (
|
|
82
86
|
// @ts-ignore
|
|
83
|
-
step.from !== void 0 &&
|
|
87
|
+
step.from !== void 0 && // @ts-ignore
|
|
88
|
+
step.to !== void 0 && oldNodes.some((node) => {
|
|
84
89
|
return (
|
|
85
90
|
// @ts-ignore
|
|
86
|
-
node.pos >= step.from &&
|
|
91
|
+
node.pos >= step.from && // @ts-ignore
|
|
92
|
+
node.pos + node.node.nodeSize <= step.to
|
|
87
93
|
);
|
|
88
94
|
})
|
|
89
95
|
);
|
|
@@ -135,9 +141,9 @@ var CodeBlockLowlight = CodeBlock.extend({
|
|
|
135
141
|
});
|
|
136
142
|
|
|
137
143
|
// src/index.ts
|
|
138
|
-
var
|
|
144
|
+
var index_default = CodeBlockLowlight;
|
|
139
145
|
export {
|
|
140
146
|
CodeBlockLowlight,
|
|
141
|
-
|
|
147
|
+
index_default as default
|
|
142
148
|
};
|
|
143
149
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/code-block-lowlight.ts","../src/lowlight-plugin.ts","../src/index.ts"],"sourcesContent":["import CodeBlock, { CodeBlockOptions } from '@tiptap/extension-code-block'\n\nimport { LowlightPlugin } from './lowlight-plugin.js'\n\nexport interface CodeBlockLowlightOptions extends CodeBlockOptions {\n /**\n * The lowlight instance.\n */\n lowlight: any
|
|
1
|
+
{"version":3,"sources":["../src/code-block-lowlight.ts","../src/lowlight-plugin.ts","../src/index.ts"],"sourcesContent":["import CodeBlock, { CodeBlockOptions } from '@tiptap/extension-code-block'\n\nimport { LowlightPlugin } from './lowlight-plugin.js'\n\nexport interface CodeBlockLowlightOptions extends CodeBlockOptions {\n /**\n * The lowlight instance.\n */\n lowlight: any\n}\n\n/**\n * This extension allows you to highlight code blocks with lowlight.\n * @see https://tiptap.dev/api/nodes/code-block-lowlight\n */\nexport const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({\n addOptions() {\n return {\n ...this.parent?.(),\n lowlight: {},\n languageClassPrefix: 'language-',\n exitOnTripleEnter: true,\n exitOnArrowDown: true,\n defaultLanguage: null,\n HTMLAttributes: {},\n }\n },\n\n addProseMirrorPlugins() {\n return [\n ...(this.parent?.() || []),\n LowlightPlugin({\n name: this.name,\n lowlight: this.options.lowlight,\n defaultLanguage: this.options.defaultLanguage,\n }),\n ]\n },\n})\n","import { findChildren } from '@tiptap/core'\nimport { Node as ProsemirrorNode } from '@tiptap/pm/model'\nimport { Plugin, PluginKey } from '@tiptap/pm/state'\nimport { Decoration, DecorationSet } from '@tiptap/pm/view'\n// @ts-ignore\nimport highlight from 'highlight.js/lib/core'\n\nfunction parseNodes(nodes: any[], className: string[] = []): { text: string; classes: string[] }[] {\n return nodes\n .map(node => {\n const classes = [...className, ...(node.properties ? node.properties.className : [])]\n\n if (node.children) {\n return parseNodes(node.children, classes)\n }\n\n return {\n text: node.value,\n classes,\n }\n })\n .flat()\n}\n\nfunction getHighlightNodes(result: any) {\n // `.value` for lowlight v1, `.children` for lowlight v2\n return result.value || result.children || []\n}\n\nfunction registered(aliasOrLanguage: string) {\n return Boolean(highlight.getLanguage(aliasOrLanguage))\n}\n\nfunction getDecorations({\n doc,\n name,\n lowlight,\n defaultLanguage,\n}: {\n doc: ProsemirrorNode\n name: string\n lowlight: any\n defaultLanguage: string | null | undefined\n}) {\n const decorations: Decoration[] = []\n\n findChildren(doc, node => node.type.name === name).forEach(block => {\n let from = block.pos + 1\n const language = block.node.attrs.language || defaultLanguage\n const languages = lowlight.listLanguages()\n\n const nodes =\n language && (languages.includes(language) || registered(language) || lowlight.registered?.(language))\n ? getHighlightNodes(lowlight.highlight(language, block.node.textContent))\n : getHighlightNodes(lowlight.highlightAuto(block.node.textContent))\n\n parseNodes(nodes).forEach(node => {\n const to = from + node.text.length\n\n if (node.classes.length) {\n const decoration = Decoration.inline(from, to, {\n class: node.classes.join(' '),\n })\n\n decorations.push(decoration)\n }\n\n from = to\n })\n })\n\n return DecorationSet.create(doc, decorations)\n}\n\n// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\nfunction isFunction(param: any): param is Function {\n return typeof param === 'function'\n}\n\nexport function LowlightPlugin({\n name,\n lowlight,\n defaultLanguage,\n}: {\n name: string\n lowlight: any\n defaultLanguage: string | null | undefined\n}) {\n if (!['highlight', 'highlightAuto', 'listLanguages'].every(api => isFunction(lowlight[api]))) {\n throw Error('You should provide an instance of lowlight to use the code-block-lowlight extension')\n }\n\n const lowlightPlugin: Plugin<any> = new Plugin({\n key: new PluginKey('lowlight'),\n\n state: {\n init: (_, { doc }) =>\n getDecorations({\n doc,\n name,\n lowlight,\n defaultLanguage,\n }),\n apply: (transaction, decorationSet, oldState, newState) => {\n const oldNodeName = oldState.selection.$head.parent.type.name\n const newNodeName = newState.selection.$head.parent.type.name\n const oldNodes = findChildren(oldState.doc, node => node.type.name === name)\n const newNodes = findChildren(newState.doc, node => node.type.name === name)\n\n if (\n transaction.docChanged &&\n // Apply decorations if:\n // selection includes named node,\n ([oldNodeName, newNodeName].includes(name) ||\n // OR transaction adds/removes named node,\n newNodes.length !== oldNodes.length ||\n // OR transaction has changes that completely encapsulte a node\n // (for example, a transaction that affects the entire document).\n // Such transactions can happen during collab syncing via y-prosemirror, for example.\n transaction.steps.some(step => {\n // @ts-ignore\n return (\n // @ts-ignore\n step.from !== undefined &&\n // @ts-ignore\n step.to !== undefined &&\n oldNodes.some(node => {\n // @ts-ignore\n return (\n // @ts-ignore\n node.pos >= step.from &&\n // @ts-ignore\n node.pos + node.node.nodeSize <= step.to\n )\n })\n )\n }))\n ) {\n return getDecorations({\n doc: transaction.doc,\n name,\n lowlight,\n defaultLanguage,\n })\n }\n\n return decorationSet.map(transaction.mapping, transaction.doc)\n },\n },\n\n props: {\n decorations(state) {\n return lowlightPlugin.getState(state)\n },\n },\n })\n\n return lowlightPlugin\n}\n","import { CodeBlockLowlight } from './code-block-lowlight.js'\n\nexport * from './code-block-lowlight.js'\n\nexport default CodeBlockLowlight\n"],"mappings":";AAAA,OAAO,eAAqC;;;ACA5C,SAAS,oBAAoB;AAE7B,SAAS,QAAQ,iBAAiB;AAClC,SAAS,YAAY,qBAAqB;AAE1C,OAAO,eAAe;AAEtB,SAAS,WAAW,OAAc,YAAsB,CAAC,GAA0C;AACjG,SAAO,MACJ,IAAI,UAAQ;AACX,UAAM,UAAU,CAAC,GAAG,WAAW,GAAI,KAAK,aAAa,KAAK,WAAW,YAAY,CAAC,CAAE;AAEpF,QAAI,KAAK,UAAU;AACjB,aAAO,WAAW,KAAK,UAAU,OAAO;AAAA,IAC1C;AAEA,WAAO;AAAA,MACL,MAAM,KAAK;AAAA,MACX;AAAA,IACF;AAAA,EACF,CAAC,EACA,KAAK;AACV;AAEA,SAAS,kBAAkB,QAAa;AAEtC,SAAO,OAAO,SAAS,OAAO,YAAY,CAAC;AAC7C;AAEA,SAAS,WAAW,iBAAyB;AAC3C,SAAO,QAAQ,UAAU,YAAY,eAAe,CAAC;AACvD;AAEA,SAAS,eAAe;AAAA,EACtB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAKG;AACD,QAAM,cAA4B,CAAC;AAEnC,eAAa,KAAK,UAAQ,KAAK,KAAK,SAAS,IAAI,EAAE,QAAQ,WAAS;AA9CtE;AA+CI,QAAI,OAAO,MAAM,MAAM;AACvB,UAAM,WAAW,MAAM,KAAK,MAAM,YAAY;AAC9C,UAAM,YAAY,SAAS,cAAc;AAEzC,UAAM,QACJ,aAAa,UAAU,SAAS,QAAQ,KAAK,WAAW,QAAQ,OAAK,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@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tiptap/extension-code-block-lowlight",
|
|
3
3
|
"description": "code block extension for tiptap",
|
|
4
|
-
"version": "3.0.0-next.
|
|
4
|
+
"version": "3.0.0-next.4",
|
|
5
5
|
"homepage": "https://tiptap.dev",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"tiptap",
|
|
@@ -15,7 +15,10 @@
|
|
|
15
15
|
"type": "module",
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
18
|
-
"types":
|
|
18
|
+
"types": {
|
|
19
|
+
"import": "./dist/index.d.ts",
|
|
20
|
+
"require": "./dist/index.d.cts"
|
|
21
|
+
},
|
|
19
22
|
"import": "./dist/index.js",
|
|
20
23
|
"require": "./dist/index.cjs"
|
|
21
24
|
}
|
|
@@ -28,10 +31,10 @@
|
|
|
28
31
|
"dist"
|
|
29
32
|
],
|
|
30
33
|
"devDependencies": {
|
|
31
|
-
"@tiptap/core": "^3.0.0-next.
|
|
32
|
-
"@tiptap/extension-code-block": "^3.0.0-next.
|
|
33
|
-
"@tiptap/pm": "^3.0.0-next.
|
|
34
|
-
"lowlight": "^
|
|
34
|
+
"@tiptap/core": "^3.0.0-next.4",
|
|
35
|
+
"@tiptap/extension-code-block": "^3.0.0-next.4",
|
|
36
|
+
"@tiptap/pm": "^3.0.0-next.4",
|
|
37
|
+
"lowlight": "^3.3.0"
|
|
35
38
|
},
|
|
36
39
|
"peerDependencies": {
|
|
37
40
|
"@tiptap/core": "^3.0.0-next.1",
|
|
@@ -46,6 +49,7 @@
|
|
|
46
49
|
"directory": "packages/extension-code-block-lowlight"
|
|
47
50
|
},
|
|
48
51
|
"scripts": {
|
|
49
|
-
"build": "tsup"
|
|
52
|
+
"build": "tsup",
|
|
53
|
+
"lint": "prettier ./src/ --check && eslint --cache --quiet --no-error-on-unmatched-pattern ./src/"
|
|
50
54
|
}
|
|
51
|
-
}
|
|
55
|
+
}
|
|
@@ -6,7 +6,7 @@ export interface CodeBlockLowlightOptions extends CodeBlockOptions {
|
|
|
6
6
|
/**
|
|
7
7
|
* The lowlight instance.
|
|
8
8
|
*/
|
|
9
|
-
lowlight: any
|
|
9
|
+
lowlight: any
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
/**
|
|
@@ -28,7 +28,7 @@ export const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({
|
|
|
28
28
|
|
|
29
29
|
addProseMirrorPlugins() {
|
|
30
30
|
return [
|
|
31
|
-
...this.parent?.() || [],
|
|
31
|
+
...(this.parent?.() || []),
|
|
32
32
|
LowlightPlugin({
|
|
33
33
|
name: this.name,
|
|
34
34
|
lowlight: this.options.lowlight,
|
package/src/lowlight-plugin.ts
CHANGED
|
@@ -49,9 +49,10 @@ function getDecorations({
|
|
|
49
49
|
const language = block.node.attrs.language || defaultLanguage
|
|
50
50
|
const languages = lowlight.listLanguages()
|
|
51
51
|
|
|
52
|
-
const nodes =
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
const nodes =
|
|
53
|
+
language && (languages.includes(language) || registered(language) || lowlight.registered?.(language))
|
|
54
|
+
? getHighlightNodes(lowlight.highlight(language, block.node.textContent))
|
|
55
|
+
: getHighlightNodes(lowlight.highlightAuto(block.node.textContent))
|
|
55
56
|
|
|
56
57
|
parseNodes(nodes).forEach(node => {
|
|
57
58
|
const to = from + node.text.length
|
|
@@ -71,7 +72,8 @@ function getDecorations({
|
|
|
71
72
|
return DecorationSet.create(doc, decorations)
|
|
72
73
|
}
|
|
73
74
|
|
|
74
|
-
|
|
75
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
76
|
+
function isFunction(param: any): param is Function {
|
|
75
77
|
return typeof param === 'function'
|
|
76
78
|
}
|
|
77
79
|
|
|
@@ -85,21 +87,20 @@ export function LowlightPlugin({
|
|
|
85
87
|
defaultLanguage: string | null | undefined
|
|
86
88
|
}) {
|
|
87
89
|
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
|
-
)
|
|
90
|
+
throw Error('You should provide an instance of lowlight to use the code-block-lowlight extension')
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
const lowlightPlugin: Plugin<any> = new Plugin({
|
|
94
94
|
key: new PluginKey('lowlight'),
|
|
95
95
|
|
|
96
96
|
state: {
|
|
97
|
-
init: (_, { doc }) =>
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
97
|
+
init: (_, { doc }) =>
|
|
98
|
+
getDecorations({
|
|
99
|
+
doc,
|
|
100
|
+
name,
|
|
101
|
+
lowlight,
|
|
102
|
+
defaultLanguage,
|
|
103
|
+
}),
|
|
103
104
|
apply: (transaction, decorationSet, oldState, newState) => {
|
|
104
105
|
const oldNodeName = oldState.selection.$head.parent.type.name
|
|
105
106
|
const newNodeName = newState.selection.$head.parent.type.name
|
|
@@ -107,29 +108,29 @@ export function LowlightPlugin({
|
|
|
107
108
|
const newNodes = findChildren(newState.doc, node => node.type.name === name)
|
|
108
109
|
|
|
109
110
|
if (
|
|
110
|
-
transaction.docChanged
|
|
111
|
+
transaction.docChanged &&
|
|
111
112
|
// Apply decorations if:
|
|
112
113
|
// selection includes named node,
|
|
113
|
-
|
|
114
|
+
([oldNodeName, newNodeName].includes(name) ||
|
|
114
115
|
// OR transaction adds/removes named node,
|
|
115
|
-
|
|
116
|
+
newNodes.length !== oldNodes.length ||
|
|
116
117
|
// OR transaction has changes that completely encapsulte a node
|
|
117
118
|
// (for example, a transaction that affects the entire document).
|
|
118
119
|
// Such transactions can happen during collab syncing via y-prosemirror, for example.
|
|
119
|
-
|
|
120
|
+
transaction.steps.some(step => {
|
|
120
121
|
// @ts-ignore
|
|
121
122
|
return (
|
|
122
123
|
// @ts-ignore
|
|
123
|
-
step.from !== undefined
|
|
124
|
+
step.from !== undefined &&
|
|
124
125
|
// @ts-ignore
|
|
125
|
-
|
|
126
|
-
|
|
126
|
+
step.to !== undefined &&
|
|
127
|
+
oldNodes.some(node => {
|
|
127
128
|
// @ts-ignore
|
|
128
129
|
return (
|
|
129
130
|
// @ts-ignore
|
|
130
|
-
node.pos >= step.from
|
|
131
|
+
node.pos >= step.from &&
|
|
131
132
|
// @ts-ignore
|
|
132
|
-
|
|
133
|
+
node.pos + node.node.nodeSize <= step.to
|
|
133
134
|
)
|
|
134
135
|
})
|
|
135
136
|
)
|