{"version":3,"file":"index.cjs","names":["highlight","Decoration","DecorationSet","Plugin","PluginKey","findChildren","CodeBlock"],"sources":["../src/lowlight-plugin.ts","../src/code-block-lowlight.ts","../src/index.ts"],"sourcesContent":["import { findChildren } from '@tiptap/core'\nimport type { 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.flatMap(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}\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 &&\n      (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// oxlint-disable-next-lineno-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(\n      'You should provide an instance of lowlight to use the code-block-lowlight extension',\n    )\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 type { CodeBlockOptions } from '@tiptap/extension-code-block'\nimport { CodeBlock } 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      exitOnArrowUp: true,\n      defaultLanguage: null,\n      enableTabIndentation: false,\n      tabSize: 4,\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 { CodeBlockLowlight } from './code-block-lowlight.js'\n\nexport * from './code-block-lowlight.js'\n\nexport default CodeBlockLowlight\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAOA,SAAS,WAAW,OAAc,YAAsB,CAAC,GAA0C;CACjG,OAAO,MAAM,SAAQ,SAAQ;EAC3B,MAAM,UAAU,CAAC,GAAG,WAAW,GAAI,KAAK,aAAa,KAAK,WAAW,YAAY,CAAC,CAAE;EAEpF,IAAI,KAAK,UACP,OAAO,WAAW,KAAK,UAAU,OAAO;EAG1C,OAAO;GACL,MAAM,KAAK;GACX;EACF;CACF,CAAC;AACH;AAEA,SAAS,kBAAkB,QAAa;CAEtC,OAAO,OAAO,SAAS,OAAO,YAAY,CAAC;AAC7C;AAEA,SAAS,WAAW,iBAAyB;CAC3C,OAAO,QAAQA,sBAAAA,QAAU,YAAY,eAAe,CAAC;AACvD;AAEA,SAAS,eAAe,EACtB,KACA,MACA,UACA,mBAMC;CACD,MAAM,cAA4B,CAAC;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