{"version":3,"sources":["jsdelivr-header.js","/npm/prosemirror-highlight@0.16.0/src/cache.ts","/npm/prosemirror-highlight@0.16.0/src/plugin.ts","/npm/prosemirror-highlight@0.16.0/src/line-number.ts"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA,uGCCA,IAAa,gBAAb,MAAa,EAGX,WAAA,CACE,GAEA,KAAK,MAAQ,IAAI,IAAI,EACvB,CAMA,GAAA,CAAI,GACF,OAAO,KAAK,MAAM,IAAI,EACxB,CASA,GAAA,CAAI,EAAa,EAAuB,GAClC,EAAM,GAIV,KAAK,MAAM,IAAI,EAAK,CAAC,EAAM,GAC7B,CAUA,OAAA,CACE,EACA,EACA,EACA,GAEA,KAAK,OAAO,GACZ,KAAK,IAAI,EAAQ,EAAM,EACzB,CAMA,MAAA,CAAO,GACL,KAAK,MAAM,OAAO,EACpB,CASA,UAAA,CAAW,GACT,MAAM,EAAc,IAAI,EAAgB,KAAK,OACvC,EAAU,EAAG,QAyBnB,OAvBA,KAAK,MAAM,QAAA,EAAU,EAAM,GAAc,KACvC,GAAI,EAAM,EACR,OAGF,MAAM,EAAS,EAAQ,UAAU,GAC3B,EAAa,EAAG,IAAI,OAAO,EAAO,KAExC,GAAI,EAAO,UAAY,GAAY,GAAG,GACpC,EAAY,OAAO,QACd,GAAI,IAAQ,EAAO,IAAK,CAE7B,MAAM,EAAqB,EACxB,IAAK,GAGG,EAAE,IAAI,EAAS,EAAG,IAE1B,OAAQ,GAA4B,MAAL,GAClC,EAAY,QAAQ,EAAK,EAAO,IAAK,EAAY,EACnD,IAGK,CACT,GChFF,SAAgB,uBAAsB,OACpC,EAAA,UACA,EAAY,CAAC,aAAc,aAAW,kBACtC,EAAqB,GAAS,EAAK,MAAM,WAqBzC,MAAM,EAAM,IAAI,UAAgC,yBAEhD,OAAO,IAAI,OAA6B,CACtC,MACA,MAAO,CACL,IAAA,CAAK,EAAG,GACN,MAAM,EAAQ,IAAI,iBACX,EAAa,GAAY,oBAC9B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EAAM,GACjB,EAAO,OAAS,GAClB,EAAe,KAAK,IAEb,aAAkB,SAC3B,EAAM,OAAO,GACb,EAAS,KAAK,IAEd,QAAQ,MACN,iDACA,EAGN,CACF,CACF,OAAS,GACP,QAAQ,MAAM,qDAAsD,EACtE,CAMA,MAAO,CAHL,EAAe,OAAS,EACpB,cAAc,OAAO,EAAK,EAAe,aACzC,EACiB,EACzB,CAEA,SAAS,kBACP,EACA,GAEA,MAAM,EAAqD,GAO3D,OANA,EAAI,YAAA,CAAa,EAAM,KACrB,GAAI,EAAK,KAAK,eAAiB,EAAU,SAAS,EAAK,KAAK,MAE1D,OADA,EAAM,KAAK,CAAC,EAAM,KACX,IAGJ,CACT,CC1LA,SAAgB,gBAAgB,GAC9B,OAAO,SAA+B,GACpC,MAAM,EAAS,EAAO,GACtB,GAAI,GAAU,MAAM,QAAQ,GAAS,CACnC,MAAM,IAAE,EAAA,QAAK,GAAY,EACnB,EAAQ,EAAM,EACd,EAAa,CAAC,GACpB,IAAK,MAAM,KAAS,EAAQ,SAAS,UACnC,EAAW,KAAK,EAAQ,EAAM,MAAQ,EAAM,GAAG,QAMjD,MAAO,IAJ2B,MAAM,KACtC,EAAA,CACC,EAAW,IAAU,sBAAsB,EAAW,EAAQ,OAEtC,EAC7B,CACA,OAAO,CACT,CACF,CAEA,SAAgB,sBACd,EACA,GAEA,OAAO,WAAW,OAAO,EAAA,IAAY,oBAAoB,GAAa,CACpE,IAAK,cACL,MAAM,IAEV,CAEA,SAAS,oBAAoB,GAC3B,MAAM,EAAO,SAAS,cAAc,QAGpC,OAFA,EAAK,UAAY,cACjB,EAAK,YAAc,EAAW,WACvB,CACT,Q","file":"/npm/prosemirror-highlight@0.16.0/dist/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.48.0.\n * Original file: /npm/prosemirror-highlight@0.16.0/dist/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import type { Node as ProseMirrorNode } from 'prosemirror-model'\nimport type { Transaction } from 'prosemirror-state'\nimport type { Decoration } from 'prosemirror-view'\n\n/**\n * Represents a cache of doc positions to the node and decorations at that position\n */\nexport class DecorationCache {\n  private cache: Map<number, [node: ProseMirrorNode, decorations: Decoration[]]>\n\n  constructor(\n    cache?: Map<number, [node: ProseMirrorNode, decorations: Decoration[]]>,\n  ) {\n    this.cache = new Map(cache)\n  }\n\n  /**\n   * Gets the cache entry at the given doc position, or null if it doesn't exist\n   * @param pos The doc position of the node you want the cache for\n   */\n  get(pos: number) {\n    return this.cache.get(pos)\n  }\n\n  /**\n   * Sets the cache entry at the given position with the give node/decoration\n   * values\n   * @param pos The doc position of the node to set the cache for\n   * @param node The node to place in cache\n   * @param decorations The decorations to place in cache\n   */\n  set(pos: number, node: ProseMirrorNode, decorations: Decoration[]): void {\n    if (pos < 0) {\n      return\n    }\n\n    this.cache.set(pos, [node, decorations])\n  }\n\n  /**\n   * Removes the value at the oldPos (if it exists) and sets the new position to\n   * the given values\n   * @param oldPos The old node position to overwrite\n   * @param newPos The new node position to set the cache for\n   * @param node The new node to place in cache\n   * @param decorations The new decorations to place in cache\n   */\n  private replace(\n    oldPos: number,\n    newPos: number,\n    node: ProseMirrorNode,\n    decorations: Decoration[],\n  ): void {\n    this.remove(oldPos)\n    this.set(newPos, node, decorations)\n  }\n\n  /**\n   * Removes the cache entry at the given position\n   * @param pos The doc position to remove from cache\n   */\n  remove(pos: number): void {\n    this.cache.delete(pos)\n  }\n\n  /**\n   * Invalidates the cache by removing all decoration entries on nodes that have\n   * changed, updating the positions of the nodes that haven't and removing all\n   * the entries that have been deleted; NOTE: this does not affect the current\n   * cache, but returns an entirely new one\n   * @param tr A transaction to map the current cache to\n   */\n  invalidate(tr: Transaction): DecorationCache {\n    const returnCache = new DecorationCache(this.cache)\n    const mapping = tr.mapping\n\n    this.cache.forEach(([node, decorations], pos) => {\n      if (pos < 0) {\n        return\n      }\n\n      const result = mapping.mapResult(pos)\n      const mappedNode = tr.doc.nodeAt(result.pos)\n\n      if (result.deleted || !mappedNode?.eq(node)) {\n        returnCache.remove(pos)\n      } else if (pos !== result.pos) {\n        // update the decorations' from/to values to match the new node position\n        const updatedDecorations = decorations\n          .map((d): Decoration | null => {\n            // @ts-expect-error: internal api\n            // eslint-disable-next-line @typescript-eslint/no-unsafe-call\n            return d.map(mapping, 0, 0) as Decoration | null\n          })\n          .filter((d): d is Decoration => d != null)\n        returnCache.replace(pos, result.pos, mappedNode, updatedDecorations)\n      }\n    })\n\n    return returnCache\n  }\n}\n","import type { Node as ProseMirrorNode } from 'prosemirror-model'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { type Decoration, DecorationSet } from 'prosemirror-view'\n\nimport { DecorationCache } from './cache'\nimport type { LanguageExtractor, Parser } from './types'\n\n/**\n * Describes the current state of the highlightPlugin\n */\nexport interface HighlightPluginState {\n  cache: DecorationCache\n  decorations: DecorationSet | undefined\n  promises: Promise<void>[]\n}\n\n/**\n * Creates a plugin that highlights the contents of all nodes (via Decorations)\n * with a type passed in blockTypes\n */\nexport function createHighlightPlugin({\n  parser,\n  nodeTypes = ['code_block', 'codeBlock'],\n  languageExtractor = (node) => node.attrs.language as string | undefined,\n}: {\n  /**\n   * A function that returns an array of decorations for the given node text\n   * content, language, and position.\n   */\n  parser: Parser\n\n  /**\n   * An array containing all the node type name to target for highlighting.\n   *\n   * @default ['code_block', 'codeBlock']\n   */\n  nodeTypes?: string[]\n\n  /**\n   * A function that returns the language string to use when highlighting that\n   * node. By default, it returns `node.attrs.language`.\n   */\n  languageExtractor?: LanguageExtractor\n}): Plugin<HighlightPluginState> {\n  const key = new PluginKey<HighlightPluginState>('prosemirror-highlight')\n\n  return new Plugin<HighlightPluginState>({\n    key,\n    state: {\n      init(_, instance) {\n        const cache = new DecorationCache()\n        const [decorations, promises] = calculateDecoration(\n          instance.doc,\n          parser,\n          nodeTypes,\n          languageExtractor,\n          cache,\n        )\n\n        return { cache, decorations, promises }\n      },\n      apply: (tr, data) => {\n        const cache = data.cache.invalidate(tr)\n        const refresh = !!tr.getMeta('prosemirror-highlight-refresh')\n\n        if (!tr.docChanged && !refresh) {\n          const decorations = data.decorations?.map(tr.mapping, tr.doc)\n          const promises = data.promises\n          return { cache, decorations, promises }\n        }\n\n        const [decorations, promises] = calculateDecoration(\n          tr.doc,\n          parser,\n          nodeTypes,\n          languageExtractor,\n          cache,\n        )\n        return { cache, decorations, promises }\n      },\n    },\n    view: (view) => {\n      const promises = new Set<Promise<void>>()\n\n      // Refresh the decorations when all promises resolve\n      const refresh = () => {\n        if (promises.size > 0 || view.isDestroyed) {\n          return\n        }\n        const tr = view.state.tr.setMeta('prosemirror-highlight-refresh', true)\n        view.dispatch(tr)\n      }\n\n      const check = () => {\n        const state = key.getState(view.state)\n\n        for (const promise of state?.promises ?? []) {\n          promises.add(promise)\n          promise\n            .then(() => {\n              promises.delete(promise)\n              refresh()\n            })\n            .catch((error) => {\n              console.error(\n                '[prosemirror-highlight] Error resolving parser:',\n                error,\n              )\n              promises.delete(promise)\n            })\n        }\n      }\n\n      check()\n\n      return {\n        update: () => {\n          check()\n        },\n      }\n    },\n    props: {\n      decorations(this, state) {\n        return this.getState(state)?.decorations\n      },\n    },\n  })\n}\n\nfunction calculateDecoration(\n  doc: ProseMirrorNode,\n  parser: Parser,\n  nodeTypes: string[],\n  languageExtractor: LanguageExtractor,\n  cache: DecorationCache,\n): [DecorationSet | undefined, Promise<void>[]] {\n  const allDecorations: Decoration[][] = []\n  const promises: Promise<void>[] = []\n  const nodes = collectCodeBlocks(doc, nodeTypes)\n\n  try {\n    for (const [node, pos] of nodes) {\n      const language = languageExtractor(node)\n      const cached = cache.get(pos)\n\n      if (cached) {\n        const [_, decorations] = cached\n        if (decorations.length > 0) {\n          allDecorations.push(decorations)\n        }\n      } else {\n        const parsed = parser({\n          content: node.textContent,\n          language: language || undefined,\n          pos,\n          size: node.nodeSize,\n        })\n        if (parsed && Array.isArray(parsed)) {\n          cache.set(pos, node, parsed)\n          if (parsed.length > 0) {\n            allDecorations.push(parsed)\n          }\n        } else if (parsed instanceof Promise) {\n          cache.remove(pos)\n          promises.push(parsed)\n        } else {\n          console.error(\n            `[prosemirror-highlight] Invalid parser result:`,\n            parsed,\n          )\n        }\n      }\n    }\n  } catch (error) {\n    console.error(`[prosemirror-highlight] Error parsing code blocks:`, error)\n  }\n\n  const decorationSet =\n    allDecorations.length > 0\n      ? DecorationSet.create(doc, allDecorations.flat())\n      : undefined\n  return [decorationSet, promises]\n}\n\nfunction collectCodeBlocks(\n  doc: ProseMirrorNode,\n  nodeTypes: string[],\n): Array<[node: ProseMirrorNode, pos: number]> {\n  const nodes: Array<[node: ProseMirrorNode, pos: number]> = []\n  doc.descendants((node, pos) => {\n    if (node.type.inlineContent && nodeTypes.includes(node.type.name)) {\n      nodes.push([node, pos])\n      return false\n    }\n  })\n  return nodes\n}\n","import { Decoration } from 'prosemirror-view'\n\nimport type { Parser, ParserOptions } from './types'\n\n/**\n * Returns a new parser that adds line numbers to the parsed decorations.\n *\n * Line numbers are added as `<span class=\"line-number\">` elements with\n * the line number as the text content.\n */\nexport function withLineNumbers(parser: Parser): Parser {\n  return function parserWithLineNumbers(options: ParserOptions) {\n    const parsed = parser(options)\n    if (parsed && Array.isArray(parsed)) {\n      const { pos, content } = options\n      const start = pos + 1\n      const lineStarts = [start]\n      for (const match of content.matchAll(/\\r?\\n/g)) {\n        lineStarts.push(start + match.index + match[0].length)\n      }\n      const decorations: Decoration[] = Array.from(\n        lineStarts,\n        (lineStart, index) => createLineStartWidget(lineStart, index + 1),\n      )\n      return [...decorations, ...parsed]\n    }\n    return parsed\n  }\n}\n\nexport function createLineStartWidget(\n  from: number,\n  lineNumber: number,\n): Decoration {\n  return Decoration.widget(from, () => createLineStartSpan(lineNumber), {\n    key: 'line-number',\n    side: -20,\n  })\n}\n\nfunction createLineStartSpan(lineNumber: number) {\n  const span = document.createElement('span')\n  span.className = 'line-number'\n  span.textContent = lineNumber.toString()\n  return span\n}\n"]}