@tiptap/extension-code-block-lowlight 2.0.0-beta.71 → 2.0.0-beta.72

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.
@@ -3,4 +3,4 @@ export declare function LowlightPlugin({ name, lowlight, defaultLanguage }: {
3
3
  name: string;
4
4
  lowlight: any;
5
5
  defaultLanguage: string | null | undefined;
6
- }): Plugin<any, any>;
6
+ }): Plugin<any>;
@@ -64,7 +64,7 @@ function LowlightPlugin({ name, lowlight, defaultLanguage }) {
64
64
  if (!['highlight', 'highlightAuto', 'listLanguages'].every(api => isFunction(lowlight[api]))) {
65
65
  throw Error('You should provide an instance of lowlight to use the code-block-lowlight extension');
66
66
  }
67
- return new prosemirrorState.Plugin({
67
+ const lowlightPlugin = new prosemirrorState.Plugin({
68
68
  key: new prosemirrorState.PluginKey('lowlight'),
69
69
  state: {
70
70
  init: (_, { doc }) => getDecorations({
@@ -112,10 +112,11 @@ function LowlightPlugin({ name, lowlight, defaultLanguage }) {
112
112
  },
113
113
  props: {
114
114
  decorations(state) {
115
- return this.getState(state);
115
+ return lowlightPlugin.getState(state);
116
116
  },
117
117
  },
118
118
  });
119
+ return lowlightPlugin;
119
120
  }
120
121
 
121
122
  const CodeBlockLowlight = CodeBlock__default["default"].extend({
@@ -1 +1 @@
1
- {"version":3,"file":"tiptap-extension-code-block-lowlight.cjs.js","sources":["../src/lowlight-plugin.ts","../src/code-block-lowlight.ts"],"sourcesContent":["import { findChildren } from '@tiptap/core'\nimport { Node as ProsemirrorNode } from 'prosemirror-model'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { Decoration, DecorationSet } from 'prosemirror-view'\n\nfunction parseNodes(nodes: any[], className: string[] = []): { text: string, classes: string[] }[] {\n return nodes\n .map(node => {\n const classes = [\n ...className,\n ...node.properties\n ? node.properties.className\n : [],\n ]\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 getDecorations({\n doc,\n name,\n lowlight,\n defaultLanguage,\n}: { doc: ProsemirrorNode, name: string, lowlight: any, defaultLanguage: string | null | undefined }) {\n const decorations: Decoration[] = []\n\n findChildren(doc, node => node.type.name === name)\n .forEach(block => {\n let from = block.pos + 1\n const language = block.node.attrs.language || defaultLanguage\n const languages = lowlight.listLanguages()\n const nodes = language && languages.includes(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\nfunction isFunction(param: Function) {\n return typeof param === 'function'\n}\n\nexport function LowlightPlugin({ name, lowlight, defaultLanguage }: { name: string, lowlight: any, defaultLanguage: string | null | undefined }) {\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 return new Plugin({\n key: new PluginKey('lowlight'),\n\n state: {\n init: (_, { doc }) => 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 && (\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 step.from !== undefined\n // @ts-ignore\n && step.to !== undefined\n && oldNodes.some(node => {\n // @ts-ignore\n return node.pos >= step.from\n // @ts-ignore\n && node.pos + node.node.nodeSize <= step.to\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 this.getState(state)\n },\n },\n })\n}\n","import CodeBlock, { CodeBlockOptions } from '@tiptap/extension-code-block'\n\nimport { LowlightPlugin } from './lowlight-plugin'\n\nexport interface CodeBlockLowlightOptions extends CodeBlockOptions {\n lowlight: any,\n defaultLanguage: string | null | undefined,\n}\n\nexport const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({\n addOptions() {\n return {\n ...this.parent?.(),\n lowlight: {},\n defaultLanguage: null,\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"],"names":["findChildren","Decoration","DecorationSet","Plugin","PluginKey","CodeBlock"],"mappings":";;;;;;;;;;;;;AAKA,SAAS,UAAU,CAAC,KAAY,EAAE,YAAsB,EAAE,EAAA;AACxD,IAAA,OAAO,KAAK;SACT,GAAG,CAAC,IAAI,IAAG;AACV,QAAA,MAAM,OAAO,GAAG;AACd,YAAA,GAAG,SAAS;YACZ,GAAG,IAAI,CAAC,UAAU;AAChB,kBAAE,IAAI,CAAC,UAAU,CAAC,SAAS;AAC3B,kBAAE,EAAE;SACP,CAAA;QAED,IAAI,IAAI,CAAC,QAAQ,EAAE;YACjB,OAAO,UAAU,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAA;AAC1C,SAAA;QAED,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,KAAK;YAChB,OAAO;SACR,C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,CAAC,CAAA;AACJ;;AC9Ha,MAAA,iBAAiB,GAAGK,6BAAS,CAAC,MAAM,CAA2B;IAC1E,UAAU,GAAA;;QACR,OAAO;AACL,YAAA,GAAG,CAAA,EAAA,GAAA,IAAI,CAAC,MAAM,MAAI,IAAA,IAAA,EAAA,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAA,IAAA,CAAA,IAAA,CAAA;AAClB,YAAA,QAAQ,EAAE,EAAE;AACZ,YAAA,eAAe,EAAE,IAAI;SACtB,CAAA;KACF;IAED,qBAAqB,GAAA;;QACnB,OAAO;AACL,YAAA,GAAG,CAAA,CAAA,EAAA,GAAA,IAAI,CAAC,MAAM,MAAA,IAAA,IAAA,EAAA,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAA,IAAA,CAAA,IAAA,CAAI,KAAI,EAAE;AACxB,YAAA,cAAc,CAAC;gBACb,IAAI,EAAE,IAAI,CAAC,IAAI;AACf,gBAAA,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,QAAQ;AAC/B,gBAAA,eAAe,EAAE,IAAI,CAAC,OAAO,CAAC,eAAe;aAC9C,CAAC;SACH,CAAA;KACF;AACF,CAAA;;;;;"}
1
+ {"version":3,"file":"tiptap-extension-code-block-lowlight.cjs.js","sources":["../src/lowlight-plugin.ts","../src/code-block-lowlight.ts"],"sourcesContent":["import { findChildren } from '@tiptap/core'\nimport { Node as ProsemirrorNode } from 'prosemirror-model'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { Decoration, DecorationSet } from 'prosemirror-view'\n\nfunction parseNodes(nodes: any[], className: string[] = []): { text: string, classes: string[] }[] {\n return nodes\n .map(node => {\n const classes = [\n ...className,\n ...node.properties\n ? node.properties.className\n : [],\n ]\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 getDecorations({\n doc,\n name,\n lowlight,\n defaultLanguage,\n}: { doc: ProsemirrorNode, name: string, lowlight: any, defaultLanguage: string | null | undefined }) {\n const decorations: Decoration[] = []\n\n findChildren(doc, node => node.type.name === name)\n .forEach(block => {\n let from = block.pos + 1\n const language = block.node.attrs.language || defaultLanguage\n const languages = lowlight.listLanguages()\n const nodes = language && languages.includes(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\nfunction isFunction(param: Function) {\n return typeof param === 'function'\n}\n\nexport function LowlightPlugin({ name, lowlight, defaultLanguage }: { name: string, lowlight: any, defaultLanguage: string | null | undefined }) {\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 }) => 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 && (\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 step.from !== undefined\n // @ts-ignore\n && step.to !== undefined\n && oldNodes.some(node => {\n // @ts-ignore\n return node.pos >= step.from\n // @ts-ignore\n && node.pos + node.node.nodeSize <= step.to\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 CodeBlock, { CodeBlockOptions } from '@tiptap/extension-code-block'\n\nimport { LowlightPlugin } from './lowlight-plugin'\n\nexport interface CodeBlockLowlightOptions extends CodeBlockOptions {\n lowlight: any,\n defaultLanguage: string | null | undefined,\n}\n\nexport const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({\n addOptions() {\n return {\n ...this.parent?.(),\n lowlight: {},\n defaultLanguage: null,\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"],"names":["findChildren","Decoration","DecorationSet","Plugin","PluginKey","CodeBlock"],"mappings":";;;;;;;;;;;;;AAKA,SAAS,UAAU,CAAC,KAAY,EAAE,YAAsB,EAAE,EAAA;AACxD,IAAA,OAAO,KAAK;SACT,GAAG,CAAC,IAAI,IAAG;AACV,QAAA,MAAM,OAAO,GAAG;AACd,YAAA,GAAG,SAAS;YACZ,GAAG,IAAI,CAAC,UAAU;AAChB,kBAAE,IAAI,CAAC,UAAU,CAAC,SAAS;AAC3B,kBAAE,EAAE;SACP,CAAA;QAED,IAAI,IAAI,CAAC,QAAQ,EAAE;YACjB,OAAO,UAAU,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAA;AAC1C,SAAA;QAED,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,KAAK;YAChB,OAAO;SACR,CAAA;AACH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;AACF,KAAA,CAAC,CAAA;AAEF,IAAA,OAAO,cAAc,CAAA;AACvB;;AChIa,MAAA,iBAAiB,GAAGK,6BAAS,CAAC,MAAM,CAA2B;IAC1E,UAAU,GAAA;;QACR,OAAO;AACL,YAAA,GAAG,CAAA,EAAA,GAAA,IAAI,CAAC,MAAM,MAAI,IAAA,IAAA,EAAA,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAA,IAAA,CAAA,IAAA,CAAA;AAClB,YAAA,QAAQ,EAAE,EAAE;AACZ,YAAA,eAAe,EAAE,IAAI;SACtB,CAAA;KACF;IAED,qBAAqB,GAAA;;QACnB,OAAO;AACL,YAAA,GAAG,CAAA,CAAA,EAAA,GAAA,IAAI,CAAC,MAAM,MAAA,IAAA,IAAA,EAAA,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAA,IAAA,CAAA,IAAA,CAAI,KAAI,EAAE;AACxB,YAAA,cAAc,CAAC;gBACb,IAAI,EAAE,IAAI,CAAC,IAAI;AACf,gBAAA,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,QAAQ;AAC/B,gBAAA,eAAe,EAAE,IAAI,CAAC,OAAO,CAAC,eAAe;aAC9C,CAAC;SACH,CAAA;KACF;AACF,CAAA;;;;;"}
@@ -56,7 +56,7 @@ function LowlightPlugin({ name, lowlight, defaultLanguage }) {
56
56
  if (!['highlight', 'highlightAuto', 'listLanguages'].every(api => isFunction(lowlight[api]))) {
57
57
  throw Error('You should provide an instance of lowlight to use the code-block-lowlight extension');
58
58
  }
59
- return new Plugin({
59
+ const lowlightPlugin = new Plugin({
60
60
  key: new PluginKey('lowlight'),
61
61
  state: {
62
62
  init: (_, { doc }) => getDecorations({
@@ -104,10 +104,11 @@ function LowlightPlugin({ name, lowlight, defaultLanguage }) {
104
104
  },
105
105
  props: {
106
106
  decorations(state) {
107
- return this.getState(state);
107
+ return lowlightPlugin.getState(state);
108
108
  },
109
109
  },
110
110
  });
111
+ return lowlightPlugin;
111
112
  }
112
113
 
113
114
  const CodeBlockLowlight = CodeBlock.extend({
@@ -1 +1 @@
1
- {"version":3,"file":"tiptap-extension-code-block-lowlight.esm.js","sources":["../src/lowlight-plugin.ts","../src/code-block-lowlight.ts"],"sourcesContent":["import { findChildren } from '@tiptap/core'\nimport { Node as ProsemirrorNode } from 'prosemirror-model'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { Decoration, DecorationSet } from 'prosemirror-view'\n\nfunction parseNodes(nodes: any[], className: string[] = []): { text: string, classes: string[] }[] {\n return nodes\n .map(node => {\n const classes = [\n ...className,\n ...node.properties\n ? node.properties.className\n : [],\n ]\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 getDecorations({\n doc,\n name,\n lowlight,\n defaultLanguage,\n}: { doc: ProsemirrorNode, name: string, lowlight: any, defaultLanguage: string | null | undefined }) {\n const decorations: Decoration[] = []\n\n findChildren(doc, node => node.type.name === name)\n .forEach(block => {\n let from = block.pos + 1\n const language = block.node.attrs.language || defaultLanguage\n const languages = lowlight.listLanguages()\n const nodes = language && languages.includes(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\nfunction isFunction(param: Function) {\n return typeof param === 'function'\n}\n\nexport function LowlightPlugin({ name, lowlight, defaultLanguage }: { name: string, lowlight: any, defaultLanguage: string | null | undefined }) {\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 return new Plugin({\n key: new PluginKey('lowlight'),\n\n state: {\n init: (_, { doc }) => 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 && (\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 step.from !== undefined\n // @ts-ignore\n && step.to !== undefined\n && oldNodes.some(node => {\n // @ts-ignore\n return node.pos >= step.from\n // @ts-ignore\n && node.pos + node.node.nodeSize <= step.to\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 this.getState(state)\n },\n },\n })\n}\n","import CodeBlock, { CodeBlockOptions } from '@tiptap/extension-code-block'\n\nimport { LowlightPlugin } from './lowlight-plugin'\n\nexport interface CodeBlockLowlightOptions extends CodeBlockOptions {\n lowlight: any,\n defaultLanguage: string | null | undefined,\n}\n\nexport const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({\n addOptions() {\n return {\n ...this.parent?.(),\n lowlight: {},\n defaultLanguage: null,\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"],"names":[],"mappings":";;;;;AAKA,SA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../src/lowlight-plugin.ts","../src/code-block-lowlight.ts"],"sourcesContent":["import { findChildren } from '@tiptap/core'\nimport { Node as ProsemirrorNode } from 'prosemirror-model'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { Decoration, DecorationSet } from 'prosemirror-view'\n\nfunction parseNodes(nodes: any[], className: string[] = []): { text: string, classes: string[] }[] {\n return nodes\n .map(node => {\n const classes = [\n ...className,\n ...node.properties\n ? node.properties.className\n : [],\n ]\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 getDecorations({\n doc,\n name,\n lowlight,\n defaultLanguage,\n}: { doc: ProsemirrorNode, name: string, lowlight: any, defaultLanguage: string | null | undefined }) {\n const decorations: Decoration[] = []\n\n findChildren(doc, node => node.type.name === name)\n .forEach(block => {\n let from = block.pos + 1\n const language = block.node.attrs.language || defaultLanguage\n const languages = lowlight.listLanguages()\n const nodes = language && languages.includes(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\nfunction isFunction(param: Function) {\n return typeof param === 'function'\n}\n\nexport function LowlightPlugin({ name, lowlight, defaultLanguage }: { name: string, lowlight: any, defaultLanguage: string | null | undefined }) {\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 }) => 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 && (\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 step.from !== undefined\n // @ts-ignore\n && step.to !== undefined\n && oldNodes.some(node => {\n // @ts-ignore\n return node.pos >= step.from\n // @ts-ignore\n && node.pos + node.node.nodeSize <= step.to\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 CodeBlock, { CodeBlockOptions } from '@tiptap/extension-code-block'\n\nimport { LowlightPlugin } from './lowlight-plugin'\n\nexport interface CodeBlockLowlightOptions extends CodeBlockOptions {\n lowlight: any,\n defaultLanguage: string | null | undefined,\n}\n\nexport const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({\n addOptions() {\n return {\n ...this.parent?.(),\n lowlight: {},\n defaultLanguage: null,\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"],"names":[],"mappings":";;;;;AAKA,SAAS,UAAU,CAAC,KAAY,EAAE,YAAsB,EAAE,EAAA;AACxD,IAAA,OAAO,KAAK;SACT,GAAG,C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
  throw Error('You should provide an instance of lowlight to use the code-block-lowlight extension');
63
63
  }
64
- return new prosemirrorState.Plugin({
64
+ const lowlightPlugin = new prosemirrorState.Plugin({
65
65
  key: new prosemirrorState.PluginKey('lowlight'),
66
66
  state: {
67
67
  init: (_, { doc }) => getDecorations({
@@ -109,10 +109,11 @@
109
109
  },
110
110
  props: {
111
111
  decorations(state) {
112
- return this.getState(state);
112
+ return lowlightPlugin.getState(state);
113
113
  },
114
114
  },
115
115
  });
116
+ return lowlightPlugin;
116
117
  }
117
118
 
118
119
  const CodeBlockLowlight = CodeBlock__default["default"].extend({
@@ -1 +1 @@
1
- {"version":3,"file":"tiptap-extension-code-block-lowlight.umd.js","sources":["../src/lowlight-plugin.ts","../src/code-block-lowlight.ts"],"sourcesContent":["import { findChildren } from '@tiptap/core'\nimport { Node as ProsemirrorNode } from 'prosemirror-model'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { Decoration, DecorationSet } from 'prosemirror-view'\n\nfunction parseNodes(nodes: any[], className: string[] = []): { text: string, classes: string[] }[] {\n return nodes\n .map(node => {\n const classes = [\n ...className,\n ...node.properties\n ? node.properties.className\n : [],\n ]\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 getDecorations({\n doc,\n name,\n lowlight,\n defaultLanguage,\n}: { doc: ProsemirrorNode, name: string, lowlight: any, defaultLanguage: string | null | undefined }) {\n const decorations: Decoration[] = []\n\n findChildren(doc, node => node.type.name === name)\n .forEach(block => {\n let from = block.pos + 1\n const language = block.node.attrs.language || defaultLanguage\n const languages = lowlight.listLanguages()\n const nodes = language && languages.includes(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\nfunction isFunction(param: Function) {\n return typeof param === 'function'\n}\n\nexport function LowlightPlugin({ name, lowlight, defaultLanguage }: { name: string, lowlight: any, defaultLanguage: string | null | undefined }) {\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 return new Plugin({\n key: new PluginKey('lowlight'),\n\n state: {\n init: (_, { doc }) => 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 && (\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 step.from !== undefined\n // @ts-ignore\n && step.to !== undefined\n && oldNodes.some(node => {\n // @ts-ignore\n return node.pos >= step.from\n // @ts-ignore\n && node.pos + node.node.nodeSize <= step.to\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 this.getState(state)\n },\n },\n })\n}\n","import CodeBlock, { CodeBlockOptions } from '@tiptap/extension-code-block'\n\nimport { LowlightPlugin } from './lowlight-plugin'\n\nexport interface CodeBlockLowlightOptions extends CodeBlockOptions {\n lowlight: any,\n defaultLanguage: string | null | undefined,\n}\n\nexport const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({\n addOptions() {\n return {\n ...this.parent?.(),\n lowlight: {},\n defaultLanguage: null,\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"],"names":["findChildren","Decoration","DecorationSet","Plugin","PluginKey","CodeBlock"],"mappings":";;;;;;;;;;EAKA,SAAS,UAAU,CAAC,KAAY,EAAE,YAAsB,EAAE,EAAA;EACxD,IAAA,OAAO,KAAK;WACT,GAAG,CAAC,IAAI,IAAG;EACV,QAAA,MAAM,OAAO,GAAG;EACd,YAAA,GAAG,SAAS;cACZ,GAAG,IAAI,CAAC,UAAU;EAChB,kBAAE,IAAI,CAAC,UAAU,CAAC,SAAS;EAC3B,kBAAE,EAAE;WACP,CAAA;UAED,IAAI,IAAI,CAAC,QAAQ,EAAE;cACjB,OAAO,UAAU,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAA;EAC1C,SAAA;UAED,OAAO;cACL,IAAI,EAAE,IAAI,CAAC,KAAK;cAChB,OAAO;WACR,CAAA;EACH,KAAC,CAAC;EACD,SAAA,IAAI,EAAE,CAAA;EACX,CAAC;EAED,SAAS,iBAAiB,CAAC,MAAW,EAAA;;MAEpC,OAAO,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,QAAQ,IAAI,EAAE,CAAA;EAC9C,CAAC;EAED,SAAS,cAAc,CAAC,EACtB,GAAG,EACH,IAAI,EACJ,QAAQ,EACR,eAAe,GACmF,EAAA;MAClG,MAAM,WAAW,GAAiB,EAAE,CAAA;EAEpC,IAAAA,iBAAY,CAAC,GAAG,EAAE,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC;WAC/C,OAAO,CAAC,KAAK,IAAG;EACf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AA,IAAA,EAAA,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAA,IAAA,CAAA,IAAA,CAAI,KAAI,EAAE;EACxB,YAAA,cAAc,CAAC;kBACb,IAAI,EAAE,IAAI,CAAC,IAAI;EACf,gBAAA,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,QAAQ;EAC/B,gBAAA,eAAe,EAAE,IAAI,CAAC,OAAO,CAAC,eAAe;eAC9C,CAAC;WACH,CAAA;OACF;EACF,CAAA;;;;;;;;;;;"}
1
+ {"version":3,"file":"tiptap-extension-code-block-lowlight.umd.js","sources":["../src/lowlight-plugin.ts","../src/code-block-lowlight.ts"],"sourcesContent":["import { findChildren } from '@tiptap/core'\nimport { Node as ProsemirrorNode } from 'prosemirror-model'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { Decoration, DecorationSet } from 'prosemirror-view'\n\nfunction parseNodes(nodes: any[], className: string[] = []): { text: string, classes: string[] }[] {\n return nodes\n .map(node => {\n const classes = [\n ...className,\n ...node.properties\n ? node.properties.className\n : [],\n ]\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 getDecorations({\n doc,\n name,\n lowlight,\n defaultLanguage,\n}: { doc: ProsemirrorNode, name: string, lowlight: any, defaultLanguage: string | null | undefined }) {\n const decorations: Decoration[] = []\n\n findChildren(doc, node => node.type.name === name)\n .forEach(block => {\n let from = block.pos + 1\n const language = block.node.attrs.language || defaultLanguage\n const languages = lowlight.listLanguages()\n const nodes = language && languages.includes(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\nfunction isFunction(param: Function) {\n return typeof param === 'function'\n}\n\nexport function LowlightPlugin({ name, lowlight, defaultLanguage }: { name: string, lowlight: any, defaultLanguage: string | null | undefined }) {\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 }) => 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 && (\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 step.from !== undefined\n // @ts-ignore\n && step.to !== undefined\n && oldNodes.some(node => {\n // @ts-ignore\n return node.pos >= step.from\n // @ts-ignore\n && node.pos + node.node.nodeSize <= step.to\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 CodeBlock, { CodeBlockOptions } from '@tiptap/extension-code-block'\n\nimport { LowlightPlugin } from './lowlight-plugin'\n\nexport interface CodeBlockLowlightOptions extends CodeBlockOptions {\n lowlight: any,\n defaultLanguage: string | null | undefined,\n}\n\nexport const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({\n addOptions() {\n return {\n ...this.parent?.(),\n lowlight: {},\n defaultLanguage: null,\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"],"names":["findChildren","Decoration","DecorationSet","Plugin","PluginKey","CodeBlock"],"mappings":";;;;;;;;;;EAKA,SAAS,UAAU,CAAC,KAAY,EAAE,YAAsB,EAAE,EAAA;EACxD,IAAA,OAAO,KAAK;WACT,GAAG,CAAC,IAAI,IAAG;EACV,QAAA,MAAM,OAAO,GAAG;EACd,YAAA,GAAG,SAAS;cACZ,GAAG,IAAI,CAAC,UAAU;EAChB,kBAAE,IAAI,CAAC,UAAU,CAAC,SAAS;EAC3B,kBAAE,EAAE;WACP,CAAA;UAED,IAAI,IAAI,CAAC,QAAQ,EAAE;cACjB,OAAO,UAAU,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAA;EAC1C,SAAA;UAED,OAAO;cACL,IAAI,EAAE,IAAI,CAAC,KAAK;cAChB,OAAO;WACR,CAAA;EACH,KAAC,CAAC;EACD,SAAA,IAAI,EAAE,CAAA;EACX,CAAC;EAED,SAAS,iBAAiB,CAAC,MAAW,EAAA;;MAEpC,OAAO,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,QAAQ,IAAI,EAAE,CAAA;EAC9C,CAAC;EAED,SAAS,cAAc,CAAC,EACtB,GAAG,EACH,IAAI,EACJ,QAAQ,EACR,eAAe,GACmF,EAAA;MAClG,MAAM,WAAW,GAAiB,EAAE,CAAA;EAEpC,IAAAA,iBAAY,CAAC,GAAG,EAAE,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC;WAC/C,OAAO,CAAC,KAAK,IAAG;EACf,QAAA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A,EAAA,GAAA,IAAI,CAAC,MAAM,MAAA,IAAA,IAAA,EAAA,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAA,IAAA,CAAA,IAAA,CAAI,KAAI,EAAE;EACxB,YAAA,cAAc,CAAC;kBACb,IAAI,EAAE,IAAI,CAAC,IAAI;EACf,gBAAA,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,QAAQ;EAC/B,gBAAA,eAAe,EAAE,IAAI,CAAC,OAAO,CAAC,eAAe;eAC9C,CAAC;WACH,CAAA;OACF;EACF,CAAA;;;;;;;;;;;"}
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.71",
4
+ "version": "2.0.0-beta.72",
5
5
  "homepage": "https://tiptap.dev",
6
6
  "keywords": [
7
7
  "tiptap",
@@ -25,16 +25,17 @@
25
25
  "lowlight": ">=1.20.0"
26
26
  },
27
27
  "dependencies": {
28
- "@tiptap/extension-code-block": "^2.0.0-beta.40",
28
+ "@tiptap/extension-code-block": "^2.0.0-beta.41",
29
29
  "@types/lowlight": "^0.0.3",
30
- "prosemirror-model": "^1.16.1",
31
- "prosemirror-state": "^1.3.4",
32
- "prosemirror-view": "^1.23.6"
30
+ "lowlight": "^1.20.0",
31
+ "prosemirror-model": "^1.17.0",
32
+ "prosemirror-state": "^1.4.0",
33
+ "prosemirror-view": "^1.25.0"
33
34
  },
34
35
  "repository": {
35
36
  "type": "git",
36
37
  "url": "https://github.com/ueberdosis/tiptap",
37
38
  "directory": "packages/extension-code-block-lowlight"
38
39
  },
39
- "gitHead": "591c0807a2ab5c34b4b7fe12c12511fe4f493ebd"
40
+ "gitHead": "a1e612bf897a14065b4f9ba6d48925c97d136811"
40
41
  }
@@ -74,7 +74,7 @@ export function LowlightPlugin({ name, lowlight, defaultLanguage }: { name: stri
74
74
  throw Error('You should provide an instance of lowlight to use the code-block-lowlight extension')
75
75
  }
76
76
 
77
- return new Plugin({
77
+ const lowlightPlugin: Plugin<any> = new Plugin({
78
78
  key: new PluginKey('lowlight'),
79
79
 
80
80
  state: {
@@ -129,8 +129,10 @@ export function LowlightPlugin({ name, lowlight, defaultLanguage }: { name: stri
129
129
 
130
130
  props: {
131
131
  decorations(state) {
132
- return this.getState(state)
132
+ return lowlightPlugin.getState(state)
133
133
  },
134
134
  },
135
135
  })
136
+
137
+ return lowlightPlugin
136
138
  }