import { At as Prec, B as tags, Dt as EditorState, Et as EditorSelection, H as NodeProp, I as syntaxTree, J as parseMixed, K as Parser, M as languageDataProp, O as indentNodeProp, R as Tag, T as foldService, U as NodeSet, Vt as countColumn, W as NodeType, Z as EditorView, _ as defineLanguageFacet, c as Language, d as ParseContext, dt as keymap, j as indentUnit, l as LanguageDescription, q as Tree, u as LanguageSupport, w as foldNodeProp, z as styleTags } from "./dist-CCADb07R.js";
import { t as CompletionContext } from "./dist-JEhxD_cn.js";
import { n as html, r as htmlCompletionSource } from "./dist-UcOPnRMa.js";
var CompositeBlock = class l {
  static create(R, z, B, V, H) {
    return new l(R, z, B, V + (V << 8) + R + (z << 4) | 0, H, [], []);
  }
  constructor(l3, R, z, B, H, U, W) {
    this.type = l3, this.value = R, this.from = z, this.hash = B, this.end = H, this.children = U, this.positions = W, this.hashProp = [[NodeProp.contextHash, B]];
  }
  addChild(l3, R) {
    l3.prop(NodeProp.contextHash) != this.hash && (l3 = new Tree(l3.type, l3.children, l3.positions, l3.length, this.hashProp)), this.children.push(l3), this.positions.push(R);
  }
  toTree(l3, R = this.end) {
    let z = this.children.length - 1;
    return z >= 0 && (R = Math.max(R, this.positions[z] + this.children[z].length + this.from)), new Tree(l3.types[this.type], this.children, this.positions, R - this.from).balance({ makeTree: (l4, R2, z2) => new Tree(NodeType.none, l4, R2, z2, this.hashProp) });
  }
}, Type;
(function(l3) {
  l3[l3.Document = 1] = "Document", l3[l3.CodeBlock = 2] = "CodeBlock", l3[l3.FencedCode = 3] = "FencedCode", l3[l3.Blockquote = 4] = "Blockquote", l3[l3.HorizontalRule = 5] = "HorizontalRule", l3[l3.BulletList = 6] = "BulletList", l3[l3.OrderedList = 7] = "OrderedList", l3[l3.ListItem = 8] = "ListItem", l3[l3.ATXHeading1 = 9] = "ATXHeading1", l3[l3.ATXHeading2 = 10] = "ATXHeading2", l3[l3.ATXHeading3 = 11] = "ATXHeading3", l3[l3.ATXHeading4 = 12] = "ATXHeading4", l3[l3.ATXHeading5 = 13] = "ATXHeading5", l3[l3.ATXHeading6 = 14] = "ATXHeading6", l3[l3.SetextHeading1 = 15] = "SetextHeading1", l3[l3.SetextHeading2 = 16] = "SetextHeading2", l3[l3.HTMLBlock = 17] = "HTMLBlock", l3[l3.LinkReference = 18] = "LinkReference", l3[l3.Paragraph = 19] = "Paragraph", l3[l3.CommentBlock = 20] = "CommentBlock", l3[l3.ProcessingInstructionBlock = 21] = "ProcessingInstructionBlock", l3[l3.Escape = 22] = "Escape", l3[l3.Entity = 23] = "Entity", l3[l3.HardBreak = 24] = "HardBreak", l3[l3.Emphasis = 25] = "Emphasis", l3[l3.StrongEmphasis = 26] = "StrongEmphasis", l3[l3.Link = 27] = "Link", l3[l3.Image = 28] = "Image", l3[l3.InlineCode = 29] = "InlineCode", l3[l3.HTMLTag = 30] = "HTMLTag", l3[l3.Comment = 31] = "Comment", l3[l3.ProcessingInstruction = 32] = "ProcessingInstruction", l3[l3.Autolink = 33] = "Autolink", l3[l3.HeaderMark = 34] = "HeaderMark", l3[l3.QuoteMark = 35] = "QuoteMark", l3[l3.ListMark = 36] = "ListMark", l3[l3.LinkMark = 37] = "LinkMark", l3[l3.EmphasisMark = 38] = "EmphasisMark", l3[l3.CodeMark = 39] = "CodeMark", l3[l3.CodeText = 40] = "CodeText", l3[l3.CodeInfo = 41] = "CodeInfo", l3[l3.LinkTitle = 42] = "LinkTitle", l3[l3.LinkLabel = 43] = "LinkLabel", l3[l3.URL = 44] = "URL";
})(Type || (Type = {}));
var LeafBlock = class {
  constructor(l3, R) {
    this.start = l3, this.content = R, this.marks = [], this.parsers = [];
  }
}, Line = class {
  constructor() {
    this.text = "", this.baseIndent = 0, this.basePos = 0, this.depth = 0, this.markers = [], this.pos = 0, this.indent = 0, this.next = -1;
  }
  forward() {
    this.basePos > this.pos && this.forwardInner();
  }
  forwardInner() {
    let l3 = this.skipSpace(this.basePos);
    this.indent = this.countIndent(l3, this.pos, this.indent), this.pos = l3, this.next = l3 == this.text.length ? -1 : this.text.charCodeAt(l3);
  }
  skipSpace(l3) {
    return skipSpace(this.text, l3);
  }
  reset(l3) {
    for (this.text = l3, this.baseIndent = this.basePos = this.pos = this.indent = 0, this.forwardInner(), this.depth = 1; this.markers.length; ) this.markers.pop();
  }
  moveBase(l3) {
    this.basePos = l3, this.baseIndent = this.countIndent(l3, this.pos, this.indent);
  }
  moveBaseColumn(l3) {
    this.baseIndent = l3, this.basePos = this.findColumn(l3);
  }
  addMarker(l3) {
    this.markers.push(l3);
  }
  countIndent(l3, R = 0, z = 0) {
    for (let B = R; B < l3; B++) z += this.text.charCodeAt(B) == 9 ? 4 - z % 4 : 1;
    return z;
  }
  findColumn(l3) {
    let R = 0;
    for (let z = 0; R < this.text.length && z < l3; R++) z += this.text.charCodeAt(R) == 9 ? 4 - z % 4 : 1;
    return R;
  }
  scrub() {
    if (!this.baseIndent) return this.text;
    let l3 = "";
    for (let R = 0; R < this.basePos; R++) l3 += " ";
    return l3 + this.text.slice(this.basePos);
  }
};
function skipForList(l3, R, z) {
  if (z.pos == z.text.length || l3 != R.block && z.indent >= R.stack[z.depth + 1].value + z.baseIndent) return true;
  if (z.indent >= z.baseIndent + 4) return false;
  let B = (l3.type == Type.OrderedList ? isOrderedList : isBulletList)(z, R, false);
  return B > 0 && (l3.type != Type.BulletList || isHorizontalRule(z, R, false) < 0) && z.text.charCodeAt(z.pos + B - 1) == l3.value;
}
var DefaultSkipMarkup = {
  [Type.Blockquote](l3, R, z) {
    return z.next == 62 ? (z.markers.push(elt(Type.QuoteMark, R.lineStart + z.pos, R.lineStart + z.pos + 1)), z.moveBase(z.pos + (space(z.text.charCodeAt(z.pos + 1)) ? 2 : 1)), l3.end = R.lineStart + z.text.length, true) : false;
  },
  [Type.ListItem](l3, R, z) {
    return z.indent < z.baseIndent + l3.value && z.next > -1 ? false : (z.moveBaseColumn(z.baseIndent + l3.value), true);
  },
  [Type.OrderedList]: skipForList,
  [Type.BulletList]: skipForList,
  [Type.Document]() {
    return true;
  }
};
function space(l3) {
  return l3 == 32 || l3 == 9 || l3 == 10 || l3 == 13;
}
function skipSpace(l3, R = 0) {
  for (; R < l3.length && space(l3.charCodeAt(R)); ) R++;
  return R;
}
function skipSpaceBack(l3, R, z) {
  for (; R > z && space(l3.charCodeAt(R - 1)); ) R--;
  return R;
}
function isFencedCode(l3) {
  if (l3.next != 96 && l3.next != 126) return -1;
  let R = l3.pos + 1;
  for (; R < l3.text.length && l3.text.charCodeAt(R) == l3.next; ) R++;
  if (R < l3.pos + 3) return -1;
  if (l3.next == 96) {
    for (let z = R; z < l3.text.length; z++) if (l3.text.charCodeAt(z) == 96) return -1;
  }
  return R;
}
function isBlockquote(l3) {
  return l3.next == 62 ? l3.text.charCodeAt(l3.pos + 1) == 32 ? 2 : 1 : -1;
}
function isHorizontalRule(l3, R, z) {
  if (l3.next != 42 && l3.next != 45 && l3.next != 95) return -1;
  let B = 1;
  for (let R2 = l3.pos + 1; R2 < l3.text.length; R2++) {
    let z2 = l3.text.charCodeAt(R2);
    if (z2 == l3.next) B++;
    else if (!space(z2)) return -1;
  }
  return z && l3.next == 45 && isSetextUnderline(l3) > -1 && l3.depth == R.stack.length && R.parser.leafBlockParsers.indexOf(DefaultLeafBlocks.SetextHeading) > -1 || B < 3 ? -1 : 1;
}
function inList(l3, R) {
  for (let z = l3.stack.length - 1; z >= 0; z--) if (l3.stack[z].type == R) return true;
  return false;
}
function isBulletList(l3, R, z) {
  return (l3.next == 45 || l3.next == 43 || l3.next == 42) && (l3.pos == l3.text.length - 1 || space(l3.text.charCodeAt(l3.pos + 1))) && (!z || inList(R, Type.BulletList) || l3.skipSpace(l3.pos + 2) < l3.text.length) ? 1 : -1;
}
function isOrderedList(l3, R, z) {
  let B = l3.pos, V = l3.next;
  for (; V >= 48 && V <= 57; ) {
    if (B++, B == l3.text.length) return -1;
    V = l3.text.charCodeAt(B);
  }
  return B == l3.pos || B > l3.pos + 9 || V != 46 && V != 41 || B < l3.text.length - 1 && !space(l3.text.charCodeAt(B + 1)) || z && !inList(R, Type.OrderedList) && (l3.skipSpace(B + 1) == l3.text.length || B > l3.pos + 1 || l3.next != 49) ? -1 : B + 1 - l3.pos;
}
function isAtxHeading(l3) {
  if (l3.next != 35) return -1;
  let R = l3.pos + 1;
  for (; R < l3.text.length && l3.text.charCodeAt(R) == 35; ) R++;
  if (R < l3.text.length && l3.text.charCodeAt(R) != 32) return -1;
  let z = R - l3.pos;
  return z > 6 ? -1 : z;
}
function isSetextUnderline(l3) {
  if (l3.next != 45 && l3.next != 61 || l3.indent >= l3.baseIndent + 4) return -1;
  let R = l3.pos + 1;
  for (; R < l3.text.length && l3.text.charCodeAt(R) == l3.next; ) R++;
  let z = R;
  for (; R < l3.text.length && space(l3.text.charCodeAt(R)); ) R++;
  return R == l3.text.length ? z : -1;
}
var EmptyLine = /^[ \t]*$/, CommentEnd = /-->/, ProcessingEnd = /\?>/, HTMLBlockStyle = [
  [/^<(?:script|pre|style)(?:\s|>|$)/i, /<\/(?:script|pre|style)>/i],
  [/^\s*<!--/, CommentEnd],
  [/^\s*<\?/, ProcessingEnd],
  [/^\s*<![A-Z]/, />/],
  [/^\s*<!\[CDATA\[/, /\]\]>/],
  [/^\s*<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h1|h2|h3|h4|h5|h6|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?:\s|\/?>|$)/i, EmptyLine],
  [/^\s*(?:<\/[a-z][\w-]*\s*>|<[a-z][\w-]*(\s+[a-z:_][\w-.]*(?:\s*=\s*(?:[^\s"'=<>`]+|'[^']*'|"[^"]*"))?)*\s*>)\s*$/i, EmptyLine]
];
function isHTMLBlock(l3, R, z) {
  if (l3.next != 60) return -1;
  let B = l3.text.slice(l3.pos);
  for (let l4 = 0, R2 = HTMLBlockStyle.length - (z ? 1 : 0); l4 < R2; l4++) if (HTMLBlockStyle[l4][0].test(B)) return l4;
  return -1;
}
function getListIndent(l3, R) {
  let z = l3.countIndent(R, l3.pos, l3.indent), B = l3.countIndent(l3.skipSpace(R), R, z);
  return B >= z + 5 ? z + 1 : B;
}
function addCodeText(l3, R, z) {
  let B = l3.length - 1;
  B >= 0 && l3[B].to == R && l3[B].type == Type.CodeText ? l3[B].to = z : l3.push(elt(Type.CodeText, R, z));
}
var DefaultBlockParsers = {
  LinkReference: void 0,
  IndentedCode(l3, R) {
    let z = R.baseIndent + 4;
    if (R.indent < z) return false;
    let B = R.findColumn(z), V = l3.lineStart + B, H = l3.lineStart + R.text.length, U = [], W = [];
    for (addCodeText(U, V, H); l3.nextLine() && R.depth >= l3.stack.length; ) if (R.pos == R.text.length) {
      addCodeText(W, l3.lineStart - 1, l3.lineStart);
      for (let l4 of R.markers) W.push(l4);
    } else if (R.indent < z) break;
    else {
      if (W.length) {
        for (let l4 of W) l4.type == Type.CodeText ? addCodeText(U, l4.from, l4.to) : U.push(l4);
        W = [];
      }
      addCodeText(U, l3.lineStart - 1, l3.lineStart);
      for (let l4 of R.markers) U.push(l4);
      H = l3.lineStart + R.text.length;
      let z2 = l3.lineStart + R.findColumn(R.baseIndent + 4);
      z2 < H && addCodeText(U, z2, H);
    }
    return W.length && (W = W.filter((l4) => l4.type != Type.CodeText), W.length && (R.markers = W.concat(R.markers))), l3.addNode(l3.buffer.writeElements(U, -V).finish(Type.CodeBlock, H - V), V), true;
  },
  FencedCode(l3, R) {
    let z = isFencedCode(R);
    if (z < 0) return false;
    let B = l3.lineStart + R.pos, V = R.next, H = z - R.pos, U = R.skipSpace(z), W = skipSpaceBack(R.text, R.text.length, U), G = [elt(Type.CodeMark, B, B + H)];
    U < W && G.push(elt(Type.CodeInfo, l3.lineStart + U, l3.lineStart + W));
    for (let z2 = true, B2 = true, U2 = false; l3.nextLine() && R.depth >= l3.stack.length; z2 = false) {
      let W2 = R.pos;
      if (R.indent - R.baseIndent < 4) for (; W2 < R.text.length && R.text.charCodeAt(W2) == V; ) W2++;
      if (W2 - R.pos >= H && R.skipSpace(W2) == R.text.length) {
        for (let l4 of R.markers) G.push(l4);
        B2 && U2 && addCodeText(G, l3.lineStart - 1, l3.lineStart), G.push(elt(Type.CodeMark, l3.lineStart + R.pos, l3.lineStart + W2)), l3.nextLine();
        break;
      } else {
        U2 = true, z2 || (addCodeText(G, l3.lineStart - 1, l3.lineStart), B2 = false);
        for (let l4 of R.markers) G.push(l4);
        let V2 = l3.lineStart + R.basePos, H2 = l3.lineStart + R.text.length;
        V2 < H2 && (addCodeText(G, V2, H2), B2 = false);
      }
    }
    return l3.addNode(l3.buffer.writeElements(G, -B).finish(Type.FencedCode, l3.prevLineEnd() - B), B), true;
  },
  Blockquote(l3, R) {
    let z = isBlockquote(R);
    return z < 0 ? false : (l3.startContext(Type.Blockquote, R.pos), l3.addNode(Type.QuoteMark, l3.lineStart + R.pos, l3.lineStart + R.pos + 1), R.moveBase(R.pos + z), null);
  },
  HorizontalRule(l3, R) {
    if (isHorizontalRule(R, l3, false) < 0) return false;
    let z = l3.lineStart + R.pos;
    return l3.nextLine(), l3.addNode(Type.HorizontalRule, z), true;
  },
  BulletList(l3, R) {
    let z = isBulletList(R, l3, false);
    if (z < 0) return false;
    l3.block.type != Type.BulletList && l3.startContext(Type.BulletList, R.basePos, R.next);
    let B = getListIndent(R, R.pos + 1);
    return l3.startContext(Type.ListItem, R.basePos, B - R.baseIndent), l3.addNode(Type.ListMark, l3.lineStart + R.pos, l3.lineStart + R.pos + z), R.moveBaseColumn(B), null;
  },
  OrderedList(l3, R) {
    let z = isOrderedList(R, l3, false);
    if (z < 0) return false;
    l3.block.type != Type.OrderedList && l3.startContext(Type.OrderedList, R.basePos, R.text.charCodeAt(R.pos + z - 1));
    let B = getListIndent(R, R.pos + z);
    return l3.startContext(Type.ListItem, R.basePos, B - R.baseIndent), l3.addNode(Type.ListMark, l3.lineStart + R.pos, l3.lineStart + R.pos + z), R.moveBaseColumn(B), null;
  },
  ATXHeading(l3, R) {
    let z = isAtxHeading(R);
    if (z < 0) return false;
    let B = R.pos, V = l3.lineStart + B, H = skipSpaceBack(R.text, R.text.length, B), U = H;
    for (; U > B && R.text.charCodeAt(U - 1) == R.next; ) U--;
    (U == H || U == B || !space(R.text.charCodeAt(U - 1))) && (U = R.text.length);
    let W = l3.buffer.write(Type.HeaderMark, 0, z).writeElements(l3.parser.parseInline(R.text.slice(B + z + 1, U), V + z + 1), -V);
    U < R.text.length && W.write(Type.HeaderMark, U - B, H - B);
    let G = W.finish(Type.ATXHeading1 - 1 + z, R.text.length - B);
    return l3.nextLine(), l3.addNode(G, V), true;
  },
  HTMLBlock(l3, R) {
    let z = isHTMLBlock(R, l3, false);
    if (z < 0) return false;
    let B = l3.lineStart + R.pos, V = HTMLBlockStyle[z][1], H = [], U = V != EmptyLine;
    for (; !V.test(R.text) && l3.nextLine(); ) {
      if (R.depth < l3.stack.length) {
        U = false;
        break;
      }
      for (let l4 of R.markers) H.push(l4);
    }
    U && l3.nextLine();
    let W = V == CommentEnd ? Type.CommentBlock : V == ProcessingEnd ? Type.ProcessingInstructionBlock : Type.HTMLBlock, G = l3.prevLineEnd();
    return l3.addNode(l3.buffer.writeElements(H, -B).finish(W, G - B), B), true;
  },
  SetextHeading: void 0
}, LinkReferenceParser = class {
  constructor(l3) {
    this.stage = 0, this.elts = [], this.pos = 0, this.start = l3.start, this.advance(l3.content);
  }
  nextLine(l3, R, z) {
    if (this.stage == -1) return false;
    let B = z.content + "\n" + R.scrub(), V = this.advance(B);
    return V > -1 && V < B.length ? this.complete(l3, z, V) : false;
  }
  finish(l3, R) {
    return (this.stage == 2 || this.stage == 3) && skipSpace(R.content, this.pos) == R.content.length ? this.complete(l3, R, R.content.length) : false;
  }
  complete(l3, R, z) {
    return l3.addLeafElement(R, elt(Type.LinkReference, this.start, this.start + z, this.elts)), true;
  }
  nextStage(l3) {
    return l3 ? (this.pos = l3.to - this.start, this.elts.push(l3), this.stage++, true) : (l3 === false && (this.stage = -1), false);
  }
  advance(l3) {
    for (; ; ) if (this.stage == -1) return -1;
    else if (this.stage == 0) {
      if (!this.nextStage(parseLinkLabel(l3, this.pos, this.start, true))) return -1;
      if (l3.charCodeAt(this.pos) != 58) return this.stage = -1;
      this.elts.push(elt(Type.LinkMark, this.pos + this.start, this.pos + this.start + 1)), this.pos++;
    } else if (this.stage == 1) {
      if (!this.nextStage(parseURL(l3, skipSpace(l3, this.pos), this.start))) return -1;
    } else if (this.stage == 2) {
      let R = skipSpace(l3, this.pos), z = 0;
      if (R > this.pos) {
        let B = parseLinkTitle(l3, R, this.start);
        if (B) {
          let R2 = lineEnd(l3, B.to - this.start);
          R2 > 0 && (this.nextStage(B), z = R2);
        }
      }
      return z || (z = lineEnd(l3, this.pos)), z > 0 && z < l3.length ? z : -1;
    } else return lineEnd(l3, this.pos);
  }
};
function lineEnd(l3, R) {
  for (; R < l3.length; R++) {
    let z = l3.charCodeAt(R);
    if (z == 10) break;
    if (!space(z)) return -1;
  }
  return R;
}
var SetextHeadingParser = class {
  nextLine(l3, R, z) {
    let B = R.depth < l3.stack.length ? -1 : isSetextUnderline(R), V = R.next;
    if (B < 0) return false;
    let H = elt(Type.HeaderMark, l3.lineStart + R.pos, l3.lineStart + B);
    return l3.nextLine(), l3.addLeafElement(z, elt(V == 61 ? Type.SetextHeading1 : Type.SetextHeading2, z.start, l3.prevLineEnd(), [...l3.parser.parseInline(z.content, z.start), H])), true;
  }
  finish() {
    return false;
  }
}, DefaultLeafBlocks = {
  LinkReference(l3, R) {
    return R.content.charCodeAt(0) == 91 ? new LinkReferenceParser(R) : null;
  },
  SetextHeading() {
    return new SetextHeadingParser();
  }
}, DefaultEndLeaf = [
  (l3, R) => isAtxHeading(R) >= 0,
  (l3, R) => isFencedCode(R) >= 0,
  (l3, R) => isBlockquote(R) >= 0,
  (l3, R) => isBulletList(R, l3, true) >= 0,
  (l3, R) => isOrderedList(R, l3, true) >= 0,
  (l3, R) => isHorizontalRule(R, l3, true) >= 0,
  (l3, R) => isHTMLBlock(R, l3, true) >= 0
], scanLineResult = {
  text: "",
  end: 0
}, BlockContext = class {
  constructor(l3, R, z, B) {
    this.parser = l3, this.input = R, this.ranges = B, this.line = new Line(), this.atEnd = false, this.reusePlaceholders = /* @__PURE__ */ new Map(), this.stoppedAt = null, this.rangeI = 0, this.to = B[B.length - 1].to, this.lineStart = this.absoluteLineStart = this.absoluteLineEnd = B[0].from, this.block = CompositeBlock.create(Type.Document, 0, this.lineStart, 0, 0), this.stack = [this.block], this.fragments = z.length ? new FragmentCursor(z, R) : null, this.readLine();
  }
  get parsedPos() {
    return this.absoluteLineStart;
  }
  advance() {
    if (this.stoppedAt != null && this.absoluteLineStart > this.stoppedAt) return this.finish();
    let { line: l3 } = this;
    for (; ; ) {
      for (let R2 = 0; ; ) {
        let z = l3.depth < this.stack.length ? this.stack[this.stack.length - 1] : null;
        for (; R2 < l3.markers.length && (!z || l3.markers[R2].from < z.end); ) {
          let z2 = l3.markers[R2++];
          this.addNode(z2.type, z2.from, z2.to);
        }
        if (!z) break;
        this.finishContext();
      }
      if (l3.pos < l3.text.length) break;
      if (!this.nextLine()) return this.finish();
    }
    if (this.fragments && this.reuseFragment(l3.basePos)) return null;
    start: for (; ; ) {
      for (let R2 of this.parser.blockParsers) if (R2) {
        let z = R2(this, l3);
        if (z != 0) {
          if (z == 1) return null;
          l3.forward();
          continue start;
        }
      }
      break;
    }
    let R = new LeafBlock(this.lineStart + l3.pos, l3.text.slice(l3.pos));
    for (let l4 of this.parser.leafBlockParsers) if (l4) {
      let z = l4(this, R);
      z && R.parsers.push(z);
    }
    lines: for (; this.nextLine() && l3.pos != l3.text.length; ) {
      if (l3.indent < l3.baseIndent + 4) {
        for (let z of this.parser.endLeafBlock) if (z(this, l3, R)) break lines;
      }
      for (let z of R.parsers) if (z.nextLine(this, l3, R)) return null;
      R.content += "\n" + l3.scrub();
      for (let z of l3.markers) R.marks.push(z);
    }
    return this.finishLeaf(R), null;
  }
  stopAt(l3) {
    if (this.stoppedAt != null && this.stoppedAt < l3) throw RangeError("Can't move stoppedAt forward");
    this.stoppedAt = l3;
  }
  reuseFragment(l3) {
    if (!this.fragments.moveTo(this.absoluteLineStart + l3, this.absoluteLineStart) || !this.fragments.matches(this.block.hash)) return false;
    let R = this.fragments.takeNodes(this);
    return R ? (this.absoluteLineStart += R, this.lineStart = toRelative(this.absoluteLineStart, this.ranges), this.moveRangeI(), this.absoluteLineStart < this.to ? (this.lineStart++, this.absoluteLineStart++, this.readLine()) : (this.atEnd = true, this.readLine()), true) : false;
  }
  get depth() {
    return this.stack.length;
  }
  parentType(l3 = this.depth - 1) {
    return this.parser.nodeSet.types[this.stack[l3].type];
  }
  nextLine() {
    return this.lineStart += this.line.text.length, this.absoluteLineEnd >= this.to ? (this.absoluteLineStart = this.absoluteLineEnd, this.atEnd = true, this.readLine(), false) : (this.lineStart++, this.absoluteLineStart = this.absoluteLineEnd + 1, this.moveRangeI(), this.readLine(), true);
  }
  peekLine() {
    return this.scanLine(this.absoluteLineEnd + 1).text;
  }
  moveRangeI() {
    for (; this.rangeI < this.ranges.length - 1 && this.absoluteLineStart >= this.ranges[this.rangeI].to; ) this.rangeI++, this.absoluteLineStart = Math.max(this.absoluteLineStart, this.ranges[this.rangeI].from);
  }
  scanLine(l3) {
    let R = scanLineResult;
    if (R.end = l3, l3 >= this.to) R.text = "";
    else if (R.text = this.lineChunkAt(l3), R.end += R.text.length, this.ranges.length > 1) {
      let l4 = this.absoluteLineStart, z = this.rangeI;
      for (; this.ranges[z].to < R.end; ) {
        z++;
        let B = this.ranges[z].from, V = this.lineChunkAt(B);
        R.end = B + V.length, R.text = R.text.slice(0, this.ranges[z - 1].to - l4) + V, l4 = R.end - R.text.length;
      }
    }
    return R;
  }
  readLine() {
    let { line: l3 } = this, { text: R, end: z } = this.scanLine(this.absoluteLineStart);
    for (this.absoluteLineEnd = z, l3.reset(R); l3.depth < this.stack.length; l3.depth++) {
      let R2 = this.stack[l3.depth], z2 = this.parser.skipContextMarkup[R2.type];
      if (!z2) throw Error("Unhandled block context " + Type[R2.type]);
      let B = this.line.markers.length;
      if (!z2(R2, this, l3)) {
        this.line.markers.length > B && (R2.end = this.line.markers[this.line.markers.length - 1].to), l3.forward();
        break;
      }
      l3.forward();
    }
  }
  lineChunkAt(l3) {
    let R = this.input.chunk(l3), z;
    if (this.input.lineChunks) z = R == "\n" ? "" : R;
    else {
      let l4 = R.indexOf("\n");
      z = l4 < 0 ? R : R.slice(0, l4);
    }
    return l3 + z.length > this.to ? z.slice(0, this.to - l3) : z;
  }
  prevLineEnd() {
    return this.atEnd ? this.lineStart : this.lineStart - 1;
  }
  startContext(l3, R, z = 0) {
    this.block = CompositeBlock.create(l3, z, this.lineStart + R, this.block.hash, this.lineStart + this.line.text.length), this.stack.push(this.block);
  }
  startComposite(l3, R, z = 0) {
    this.startContext(this.parser.getNodeType(l3), R, z);
  }
  addNode(l3, R, z) {
    typeof l3 == "number" && (l3 = new Tree(this.parser.nodeSet.types[l3], none, none, (z ?? this.prevLineEnd()) - R)), this.block.addChild(l3, R - this.block.from);
  }
  addElement(l3) {
    this.block.addChild(l3.toTree(this.parser.nodeSet), l3.from - this.block.from);
  }
  addLeafElement(l3, R) {
    this.addNode(this.buffer.writeElements(injectMarks(R.children, l3.marks), -R.from).finish(R.type, R.to - R.from), R.from);
  }
  finishContext() {
    let l3 = this.stack.pop(), R = this.stack[this.stack.length - 1];
    R.addChild(l3.toTree(this.parser.nodeSet), l3.from - R.from), this.block = R;
  }
  finish() {
    for (; this.stack.length > 1; ) this.finishContext();
    return this.addGaps(this.block.toTree(this.parser.nodeSet, this.lineStart));
  }
  addGaps(l3) {
    return this.ranges.length > 1 ? injectGaps(this.ranges, 0, l3.topNode, this.ranges[0].from, this.reusePlaceholders) : l3;
  }
  finishLeaf(l3) {
    for (let R2 of l3.parsers) if (R2.finish(this, l3)) return;
    let R = injectMarks(this.parser.parseInline(l3.content, l3.start), l3.marks);
    this.addNode(this.buffer.writeElements(R, -l3.start).finish(Type.Paragraph, l3.content.length), l3.start);
  }
  elt(l3, R, z, B) {
    return typeof l3 == "string" ? elt(this.parser.getNodeType(l3), R, z, B) : new TreeElement(l3, R);
  }
  get buffer() {
    return new Buffer(this.parser.nodeSet);
  }
};
function injectGaps(l3, R, z, B, V) {
  let H = l3[R].to, U = [], W = [], G = z.from + B;
  function K(z2, V2) {
    for (; V2 ? z2 >= H : z2 > H; ) {
      let V3 = l3[R + 1].from - H;
      B += V3, z2 += V3, R++, H = l3[R].to;
    }
  }
  for (let q = z.firstChild; q; q = q.nextSibling) {
    K(q.from + B, true);
    let z2 = q.from + B, J, Y = V.get(q.tree);
    Y ? J = Y : q.to + B > H ? (J = injectGaps(l3, R, q, B, V), K(q.to + B, false)) : J = q.toTree(), U.push(J), W.push(z2 - G);
  }
  return K(z.to + B, false), new Tree(z.type, U, W, z.to + B - G, z.tree ? z.tree.propValues : void 0);
}
var MarkdownParser = class l2 extends Parser {
  constructor(l3, R, z, B, V, H, U, W, G) {
    super(), this.nodeSet = l3, this.blockParsers = R, this.leafBlockParsers = z, this.blockNames = B, this.endLeafBlock = V, this.skipContextMarkup = H, this.inlineParsers = U, this.inlineNames = W, this.wrappers = G, this.nodeTypes = /* @__PURE__ */ Object.create(null);
    for (let R2 of l3.types) this.nodeTypes[R2.name] = R2.id;
  }
  createParse(l3, R, z) {
    let B = new BlockContext(this, l3, R, z);
    for (let V of this.wrappers) B = V(B, l3, R, z);
    return B;
  }
  configure(R) {
    let z = resolveConfig(R);
    if (!z) return this;
    let { nodeSet: B, skipContextMarkup: H } = this, U = this.blockParsers.slice(), W = this.leafBlockParsers.slice(), G = this.blockNames.slice(), K = this.inlineParsers.slice(), J = this.inlineNames.slice(), X = this.endLeafBlock.slice(), Q = this.wrappers;
    if (nonEmpty(z.defineNodes)) {
      H = Object.assign({}, H);
      let l3 = B.types.slice(), R2;
      for (let B2 of z.defineNodes) {
        let { name: z2, block: U2, composite: W2, style: G2 } = typeof B2 == "string" ? { name: B2 } : B2;
        if (l3.some((l4) => l4.name == z2)) continue;
        W2 && (H[l3.length] = (l4, R3, z3) => W2(R3, z3, l4.value));
        let K2 = l3.length, J2 = W2 ? ["Block", "BlockContext"] : U2 ? K2 >= Type.ATXHeading1 && K2 <= Type.SetextHeading2 ? [
          "Block",
          "LeafBlock",
          "Heading"
        ] : ["Block", "LeafBlock"] : void 0;
        l3.push(NodeType.define({
          id: K2,
          name: z2,
          props: J2 && [[NodeProp.group, J2]]
        })), G2 && (R2 || (R2 = {}), Array.isArray(G2) || G2 instanceof Tag ? R2[z2] = G2 : Object.assign(R2, G2));
      }
      B = new NodeSet(l3), R2 && (B = B.extend(styleTags(R2)));
    }
    if (nonEmpty(z.props) && (B = B.extend(...z.props)), nonEmpty(z.remove)) for (let l3 of z.remove) {
      let R2 = this.blockNames.indexOf(l3), z2 = this.inlineNames.indexOf(l3);
      R2 > -1 && (U[R2] = W[R2] = void 0), z2 > -1 && (K[z2] = void 0);
    }
    if (nonEmpty(z.parseBlock)) for (let l3 of z.parseBlock) {
      let R2 = G.indexOf(l3.name);
      if (R2 > -1) U[R2] = l3.parse, W[R2] = l3.leaf;
      else {
        let R3 = l3.before ? findName(G, l3.before) : l3.after ? findName(G, l3.after) + 1 : G.length - 1;
        U.splice(R3, 0, l3.parse), W.splice(R3, 0, l3.leaf), G.splice(R3, 0, l3.name);
      }
      l3.endLeaf && X.push(l3.endLeaf);
    }
    if (nonEmpty(z.parseInline)) for (let l3 of z.parseInline) {
      let R2 = J.indexOf(l3.name);
      if (R2 > -1) K[R2] = l3.parse;
      else {
        let R3 = l3.before ? findName(J, l3.before) : l3.after ? findName(J, l3.after) + 1 : J.length - 1;
        K.splice(R3, 0, l3.parse), J.splice(R3, 0, l3.name);
      }
    }
    return z.wrap && (Q = Q.concat(z.wrap)), new l2(B, U, W, G, X, H, K, J, Q);
  }
  getNodeType(l3) {
    let R = this.nodeTypes[l3];
    if (R == null) throw RangeError(`Unknown node type '${l3}'`);
    return R;
  }
  parseInline(l3, R) {
    let z = new InlineContext(this, l3, R);
    outer: for (let l4 = R; l4 < z.end; ) {
      let R2 = z.char(l4);
      for (let B of this.inlineParsers) if (B) {
        let V = B(z, R2, l4);
        if (V >= 0) {
          l4 = V;
          continue outer;
        }
      }
      l4++;
    }
    return z.resolveMarkers(0);
  }
};
function nonEmpty(l3) {
  return l3 != null && l3.length > 0;
}
function resolveConfig(l3) {
  if (!Array.isArray(l3)) return l3;
  if (l3.length == 0) return null;
  let R = resolveConfig(l3[0]);
  if (l3.length == 1) return R;
  let z = resolveConfig(l3.slice(1));
  if (!z || !R) return R || z;
  let B = (l4, R2) => (l4 || none).concat(R2 || none), V = R.wrap, H = z.wrap;
  return {
    props: B(R.props, z.props),
    defineNodes: B(R.defineNodes, z.defineNodes),
    parseBlock: B(R.parseBlock, z.parseBlock),
    parseInline: B(R.parseInline, z.parseInline),
    remove: B(R.remove, z.remove),
    wrap: V ? H ? (l4, R2, z2, B2) => V(H(l4, R2, z2, B2), R2, z2, B2) : V : H
  };
}
function findName(l3, R) {
  let z = l3.indexOf(R);
  if (z < 0) throw RangeError(`Position specified relative to unknown parser ${R}`);
  return z;
}
var nodeTypes = [NodeType.none];
for (let l3 = 1, R; R = Type[l3]; l3++) nodeTypes[l3] = NodeType.define({
  id: l3,
  name: R,
  props: l3 >= Type.Escape ? [] : [[NodeProp.group, l3 in DefaultSkipMarkup ? ["Block", "BlockContext"] : ["Block", "LeafBlock"]]],
  top: R == "Document"
});
var none = [], Buffer = class {
  constructor(l3) {
    this.nodeSet = l3, this.content = [], this.nodes = [];
  }
  write(l3, R, z, B = 0) {
    return this.content.push(l3, R, z, 4 + B * 4), this;
  }
  writeElements(l3, R = 0) {
    for (let z of l3) z.writeTo(this, R);
    return this;
  }
  finish(l3, R) {
    return Tree.build({
      buffer: this.content,
      nodeSet: this.nodeSet,
      reused: this.nodes,
      topID: l3,
      length: R
    });
  }
}, Element = class {
  constructor(l3, R, z, B = none) {
    this.type = l3, this.from = R, this.to = z, this.children = B;
  }
  writeTo(l3, R) {
    let z = l3.content.length;
    l3.writeElements(this.children, R), l3.content.push(this.type, this.from + R, this.to + R, l3.content.length + 4 - z);
  }
  toTree(l3) {
    return new Buffer(l3).writeElements(this.children, -this.from).finish(this.type, this.to - this.from);
  }
}, TreeElement = class {
  constructor(l3, R) {
    this.tree = l3, this.from = R;
  }
  get to() {
    return this.from + this.tree.length;
  }
  get type() {
    return this.tree.type.id;
  }
  get children() {
    return none;
  }
  writeTo(l3, R) {
    l3.nodes.push(this.tree), l3.content.push(l3.nodes.length - 1, this.from + R, this.to + R, -1);
  }
  toTree() {
    return this.tree;
  }
};
function elt(l3, R, z, B) {
  return new Element(l3, R, z, B);
}
var EmphasisUnderscore = {
  resolve: "Emphasis",
  mark: "EmphasisMark"
}, EmphasisAsterisk = {
  resolve: "Emphasis",
  mark: "EmphasisMark"
}, LinkStart = {}, ImageStart = {}, InlineDelimiter = class {
  constructor(l3, R, z, B) {
    this.type = l3, this.from = R, this.to = z, this.side = B;
  }
}, Escapable = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", Punctuation = /[!"#$%&'()*+,\-.\/:;<=>?@\[\\\]^_`{|}~\xA1\u2010-\u2027]/;
try {
  Punctuation = RegExp("[\\p{S}|\\p{P}]", "u");
} catch {
}
var DefaultInline = {
  Escape(l3, R, z) {
    if (R != 92 || z == l3.end - 1) return -1;
    let B = l3.char(z + 1);
    for (let R2 = 0; R2 < 32; R2++) if (Escapable.charCodeAt(R2) == B) return l3.append(elt(Type.Escape, z, z + 2));
    return -1;
  },
  Entity(l3, R, z) {
    if (R != 38) return -1;
    let B = /^(?:#\d+|#x[a-f\d]+|\w+);/i.exec(l3.slice(z + 1, z + 31));
    return B ? l3.append(elt(Type.Entity, z, z + 1 + B[0].length)) : -1;
  },
  InlineCode(l3, R, z) {
    if (R != 96 || z && l3.char(z - 1) == 96) return -1;
    let B = z + 1;
    for (; B < l3.end && l3.char(B) == 96; ) B++;
    let V = B - z, H = 0;
    for (; B < l3.end; B++) if (l3.char(B) == 96) {
      if (H++, H == V && l3.char(B + 1) != 96) return l3.append(elt(Type.InlineCode, z, B + 1, [elt(Type.CodeMark, z, z + V), elt(Type.CodeMark, B + 1 - V, B + 1)]));
    } else H = 0;
    return -1;
  },
  HTMLTag(l3, R, z) {
    if (R != 60 || z == l3.end - 1) return -1;
    let B = l3.slice(z + 1, l3.end), V = /^(?:[a-z][-\w+.]+:[^\s>]+|[a-z\d.!#$%&'*+/=?^_`{|}~-]+@[a-z\d](?:[a-z\d-]{0,61}[a-z\d])?(?:\.[a-z\d](?:[a-z\d-]{0,61}[a-z\d])?)*)>/i.exec(B);
    if (V) return l3.append(elt(Type.Autolink, z, z + 1 + V[0].length, [
      elt(Type.LinkMark, z, z + 1),
      elt(Type.URL, z + 1, z + V[0].length),
      elt(Type.LinkMark, z + V[0].length, z + 1 + V[0].length)
    ]));
    let H = /^!--[^>](?:-[^-]|[^-])*?-->/i.exec(B);
    if (H) return l3.append(elt(Type.Comment, z, z + 1 + H[0].length));
    let U = /^\?[^]*?\?>/.exec(B);
    if (U) return l3.append(elt(Type.ProcessingInstruction, z, z + 1 + U[0].length));
    let W = /^(?:![A-Z][^]*?>|!\[CDATA\[[^]*?\]\]>|\/\s*[a-zA-Z][\w-]*\s*>|\s*[a-zA-Z][\w-]*(\s+[a-zA-Z:_][\w-.:]*(?:\s*=\s*(?:[^\s"'=<>`]+|'[^']*'|"[^"]*"))?)*\s*(\/\s*)?>)/.exec(B);
    return W ? l3.append(elt(Type.HTMLTag, z, z + 1 + W[0].length)) : -1;
  },
  Emphasis(l3, R, z) {
    if (R != 95 && R != 42) return -1;
    let B = z + 1;
    for (; l3.char(B) == R; ) B++;
    let V = l3.slice(z - 1, z), H = l3.slice(B, B + 1), U = Punctuation.test(V), W = Punctuation.test(H), G = /\s|^$/.test(V), K = /\s|^$/.test(H), q = !K && (!W || G || U), J = !G && (!U || K || W), Y = q && (R == 42 || !J || U), X = J && (R == 42 || !q || W);
    return l3.append(new InlineDelimiter(R == 95 ? EmphasisUnderscore : EmphasisAsterisk, z, B, (Y ? 1 : 0) | (X ? 2 : 0)));
  },
  HardBreak(l3, R, z) {
    if (R == 92 && l3.char(z + 1) == 10) return l3.append(elt(Type.HardBreak, z, z + 2));
    if (R == 32) {
      let R2 = z + 1;
      for (; l3.char(R2) == 32; ) R2++;
      if (l3.char(R2) == 10 && R2 >= z + 2) return l3.append(elt(Type.HardBreak, z, R2 + 1));
    }
    return -1;
  },
  Link(l3, R, z) {
    return R == 91 ? l3.append(new InlineDelimiter(LinkStart, z, z + 1, 1)) : -1;
  },
  Image(l3, R, z) {
    return R == 33 && l3.char(z + 1) == 91 ? l3.append(new InlineDelimiter(ImageStart, z, z + 2, 1)) : -1;
  },
  LinkEnd(l3, R, z) {
    if (R != 93) return -1;
    for (let R2 = l3.parts.length - 1; R2 >= 0; R2--) {
      let B = l3.parts[R2];
      if (B instanceof InlineDelimiter && (B.type == LinkStart || B.type == ImageStart)) {
        if (!B.side || l3.skipSpace(B.to) == z && !/[(\[]/.test(l3.slice(z + 1, z + 2))) return l3.parts[R2] = null, -1;
        let V = l3.takeContent(R2), H = l3.parts[R2] = finishLink(l3, V, B.type == LinkStart ? Type.Link : Type.Image, B.from, z + 1);
        if (B.type == LinkStart) for (let z2 = 0; z2 < R2; z2++) {
          let R3 = l3.parts[z2];
          R3 instanceof InlineDelimiter && R3.type == LinkStart && (R3.side = 0);
        }
        return H.to;
      }
    }
    return -1;
  }
};
function finishLink(l3, R, z, B, V) {
  let { text: H } = l3, U = l3.char(V), W = V;
  if (R.unshift(elt(Type.LinkMark, B, B + (z == Type.Image ? 2 : 1))), R.push(elt(Type.LinkMark, V - 1, V)), U == 40) {
    let z2 = l3.skipSpace(V + 1), B2 = parseURL(H, z2 - l3.offset, l3.offset), U2;
    B2 && (z2 = l3.skipSpace(B2.to), z2 != B2.to && (U2 = parseLinkTitle(H, z2 - l3.offset, l3.offset), U2 && (z2 = l3.skipSpace(U2.to)))), l3.char(z2) == 41 && (R.push(elt(Type.LinkMark, V, V + 1)), W = z2 + 1, B2 && R.push(B2), U2 && R.push(U2), R.push(elt(Type.LinkMark, z2, W)));
  } else if (U == 91) {
    let z2 = parseLinkLabel(H, V - l3.offset, l3.offset, false);
    z2 && (R.push(z2), W = z2.to);
  }
  return elt(z, B, W, R);
}
function parseURL(l3, R, z) {
  if (l3.charCodeAt(R) == 60) {
    for (let B = R + 1; B < l3.length; B++) {
      let V = l3.charCodeAt(B);
      if (V == 62) return elt(Type.URL, R + z, B + 1 + z);
      if (V == 60 || V == 10) return false;
    }
    return null;
  } else {
    let B = 0, V = R;
    for (let R2 = false; V < l3.length; V++) {
      let z2 = l3.charCodeAt(V);
      if (space(z2)) break;
      if (R2) R2 = false;
      else if (z2 == 40) B++;
      else if (z2 == 41) {
        if (!B) break;
        B--;
      } else z2 == 92 && (R2 = true);
    }
    return V > R ? elt(Type.URL, R + z, V + z) : V == l3.length ? null : false;
  }
}
function parseLinkTitle(l3, R, z) {
  let B = l3.charCodeAt(R);
  if (B != 39 && B != 34 && B != 40) return false;
  let V = B == 40 ? 41 : B;
  for (let B2 = R + 1, H = false; B2 < l3.length; B2++) {
    let U = l3.charCodeAt(B2);
    if (H) H = false;
    else if (U == V) return elt(Type.LinkTitle, R + z, B2 + 1 + z);
    else U == 92 && (H = true);
  }
  return null;
}
function parseLinkLabel(l3, R, z, B) {
  for (let V = false, H = R + 1, U = Math.min(l3.length, H + 999); H < U; H++) {
    let U2 = l3.charCodeAt(H);
    if (V) V = false;
    else if (U2 == 93) return B ? false : elt(Type.LinkLabel, R + z, H + 1 + z);
    else {
      if (B && !space(U2) && (B = false), U2 == 91) return false;
      U2 == 92 && (V = true);
    }
  }
  return null;
}
var InlineContext = class {
  constructor(l3, R, z) {
    this.parser = l3, this.text = R, this.offset = z, this.parts = [];
  }
  char(l3) {
    return l3 >= this.end ? -1 : this.text.charCodeAt(l3 - this.offset);
  }
  get end() {
    return this.offset + this.text.length;
  }
  slice(l3, R) {
    return this.text.slice(l3 - this.offset, R - this.offset);
  }
  append(l3) {
    return this.parts.push(l3), l3.to;
  }
  addDelimiter(l3, R, z, B, V) {
    return this.append(new InlineDelimiter(l3, R, z, (B ? 1 : 0) | (V ? 2 : 0)));
  }
  get hasOpenLink() {
    for (let l3 = this.parts.length - 1; l3 >= 0; l3--) {
      let R = this.parts[l3];
      if (R instanceof InlineDelimiter && (R.type == LinkStart || R.type == ImageStart)) return true;
    }
    return false;
  }
  addElement(l3) {
    return this.append(l3);
  }
  resolveMarkers(l3) {
    for (let R2 = l3; R2 < this.parts.length; R2++) {
      let z = this.parts[R2];
      if (!(z instanceof InlineDelimiter && z.type.resolve && z.side & 2)) continue;
      let B = z.type == EmphasisUnderscore || z.type == EmphasisAsterisk, V = z.to - z.from, H, U = R2 - 1;
      for (; U >= l3; U--) {
        let l4 = this.parts[U];
        if (l4 instanceof InlineDelimiter && l4.side & 1 && l4.type == z.type && !(B && (z.side & 1 || l4.side & 2) && (l4.to - l4.from + V) % 3 == 0 && ((l4.to - l4.from) % 3 || V % 3))) {
          H = l4;
          break;
        }
      }
      if (!H) continue;
      let W = z.type.resolve, G = [], K = H.from, q = z.to;
      if (B) {
        let l4 = Math.min(2, H.to - H.from, V);
        K = H.to - l4, q = z.from + l4, W = l4 == 1 ? "Emphasis" : "StrongEmphasis";
      }
      H.type.mark && G.push(this.elt(H.type.mark, K, H.to));
      for (let l4 = U + 1; l4 < R2; l4++) this.parts[l4] instanceof Element && G.push(this.parts[l4]), this.parts[l4] = null;
      z.type.mark && G.push(this.elt(z.type.mark, z.from, q));
      let J = this.elt(W, K, q, G);
      this.parts[U] = B && H.from != K ? new InlineDelimiter(H.type, H.from, K, H.side) : null, (this.parts[R2] = B && z.to != q ? new InlineDelimiter(z.type, q, z.to, z.side) : null) ? this.parts.splice(R2, 0, J) : this.parts[R2] = J;
    }
    let R = [];
    for (let z = l3; z < this.parts.length; z++) {
      let l4 = this.parts[z];
      l4 instanceof Element && R.push(l4);
    }
    return R;
  }
  findOpeningDelimiter(l3) {
    for (let R = this.parts.length - 1; R >= 0; R--) {
      let z = this.parts[R];
      if (z instanceof InlineDelimiter && z.type == l3 && z.side & 1) return R;
    }
    return null;
  }
  takeContent(l3) {
    let R = this.resolveMarkers(l3);
    return this.parts.length = l3, R;
  }
  getDelimiterAt(l3) {
    let R = this.parts[l3];
    return R instanceof InlineDelimiter ? R : null;
  }
  skipSpace(l3) {
    return skipSpace(this.text, l3 - this.offset) + this.offset;
  }
  elt(l3, R, z, B) {
    return typeof l3 == "string" ? elt(this.parser.getNodeType(l3), R, z, B) : new TreeElement(l3, R);
  }
};
InlineContext.linkStart = LinkStart, InlineContext.imageStart = ImageStart;
function injectMarks(l3, R) {
  if (!R.length) return l3;
  if (!l3.length) return R;
  let z = l3.slice(), B = 0;
  for (let l4 of R) {
    for (; B < z.length && z[B].to < l4.to; ) B++;
    if (B < z.length && z[B].from < l4.from) {
      let R2 = z[B];
      R2 instanceof Element && (z[B] = new Element(R2.type, R2.from, R2.to, injectMarks(R2.children, [l4])));
    } else z.splice(B++, 0, l4);
  }
  return z;
}
var NotLast = [
  Type.CodeBlock,
  Type.ListItem,
  Type.OrderedList,
  Type.BulletList
], FragmentCursor = class {
  constructor(l3, R) {
    this.fragments = l3, this.input = R, this.i = 0, this.fragment = null, this.fragmentEnd = -1, this.cursor = null, l3.length && (this.fragment = l3[this.i++]);
  }
  nextFragment() {
    this.fragment = this.i < this.fragments.length ? this.fragments[this.i++] : null, this.cursor = null, this.fragmentEnd = -1;
  }
  moveTo(l3, R) {
    for (; this.fragment && this.fragment.to <= l3; ) this.nextFragment();
    if (!this.fragment || this.fragment.from > (l3 ? l3 - 1 : 0)) return false;
    if (this.fragmentEnd < 0) {
      let l4 = this.fragment.to;
      for (; l4 > 0 && this.input.read(l4 - 1, l4) != "\n"; ) l4--;
      this.fragmentEnd = l4 ? l4 - 1 : 0;
    }
    let z = this.cursor;
    z || (z = this.cursor = this.fragment.tree.cursor(), z.firstChild());
    let B = l3 + this.fragment.offset;
    for (; z.to <= B; ) if (!z.parent()) return false;
    for (; ; ) {
      if (z.from >= B) return this.fragment.from <= R;
      if (!z.childAfter(B)) return false;
    }
  }
  matches(l3) {
    let R = this.cursor.tree;
    return R && R.prop(NodeProp.contextHash) == l3;
  }
  takeNodes(l3) {
    let R = this.cursor, z = this.fragment.offset, B = this.fragmentEnd - (this.fragment.openEnd ? 1 : 0), V = l3.absoluteLineStart, H = V, U = l3.block.children.length, W = H, G = U;
    for (; ; ) {
      if (R.to - z > B) {
        if (R.type.isAnonymous && R.firstChild()) continue;
        break;
      }
      let V2 = toRelative(R.from - z, l3.ranges);
      if (R.to - z <= l3.ranges[l3.rangeI].to) l3.addNode(R.tree, V2);
      else {
        let z2 = new Tree(l3.parser.nodeSet.types[Type.Paragraph], [], [], 0, l3.block.hashProp);
        l3.reusePlaceholders.set(z2, R.tree), l3.addNode(z2, V2);
      }
      if (R.type.is("Block") && (NotLast.indexOf(R.type.id) < 0 ? (H = R.to - z, U = l3.block.children.length) : (H = W, U = G), W = R.to - z, G = l3.block.children.length), !R.nextSibling()) break;
    }
    for (; l3.block.children.length > U; ) l3.block.children.pop(), l3.block.positions.pop();
    return H - V;
  }
};
function toRelative(l3, R) {
  let z = l3;
  for (let B = 1; B < R.length; B++) {
    let V = R[B - 1].to, H = R[B].from;
    V < l3 && (z -= H - V);
  }
  return z;
}
var markdownHighlighting = styleTags({
  "Blockquote/...": tags.quote,
  HorizontalRule: tags.contentSeparator,
  "ATXHeading1/... SetextHeading1/...": tags.heading1,
  "ATXHeading2/... SetextHeading2/...": tags.heading2,
  "ATXHeading3/...": tags.heading3,
  "ATXHeading4/...": tags.heading4,
  "ATXHeading5/...": tags.heading5,
  "ATXHeading6/...": tags.heading6,
  "Comment CommentBlock": tags.comment,
  Escape: tags.escape,
  Entity: tags.character,
  "Emphasis/...": tags.emphasis,
  "StrongEmphasis/...": tags.strong,
  "Link/... Image/...": tags.link,
  "OrderedList/... BulletList/...": tags.list,
  "BlockQuote/...": tags.quote,
  "InlineCode CodeText": tags.monospace,
  "URL Autolink": tags.url,
  "HeaderMark HardBreak QuoteMark ListMark LinkMark EmphasisMark CodeMark": tags.processingInstruction,
  "CodeInfo LinkLabel": tags.labelName,
  LinkTitle: tags.string,
  Paragraph: tags.content
}), parser = new MarkdownParser(new NodeSet(nodeTypes).extend(markdownHighlighting), Object.keys(DefaultBlockParsers).map((l3) => DefaultBlockParsers[l3]), Object.keys(DefaultBlockParsers).map((l3) => DefaultLeafBlocks[l3]), Object.keys(DefaultBlockParsers), DefaultEndLeaf, DefaultSkipMarkup, Object.keys(DefaultInline).map((l3) => DefaultInline[l3]), Object.keys(DefaultInline), []);
function leftOverSpace(l3, R, z) {
  let B = [];
  for (let V = l3.firstChild, H = R; ; V = V.nextSibling) {
    let l4 = V ? V.from : z;
    if (l4 > H && B.push({
      from: H,
      to: l4
    }), !V) break;
    H = V.to;
  }
  return B;
}
function parseCode(l3) {
  let { codeParser: R, htmlParser: z } = l3;
  return { wrap: parseMixed((l4, B) => {
    let V = l4.type.id;
    if (R && (V == Type.CodeBlock || V == Type.FencedCode)) {
      let z2 = "";
      if (V == Type.FencedCode) {
        let R2 = l4.node.getChild(Type.CodeInfo);
        R2 && (z2 = B.read(R2.from, R2.to));
      }
      let H = R(z2);
      if (H) return {
        parser: H,
        overlay: (l5) => l5.type.id == Type.CodeText,
        bracketed: V == Type.FencedCode
      };
    } else if (z && (V == Type.HTMLBlock || V == Type.HTMLTag || V == Type.CommentBlock)) return {
      parser: z,
      overlay: leftOverSpace(l4.node, l4.from, l4.to)
    };
    return null;
  }) };
}
var StrikethroughDelim = {
  resolve: "Strikethrough",
  mark: "StrikethroughMark"
}, Strikethrough = {
  defineNodes: [{
    name: "Strikethrough",
    style: { "Strikethrough/...": tags.strikethrough }
  }, {
    name: "StrikethroughMark",
    style: tags.processingInstruction
  }],
  parseInline: [{
    name: "Strikethrough",
    parse(l3, R, z) {
      if (R != 126 || l3.char(z + 1) != 126 || l3.char(z + 2) == 126) return -1;
      let B = l3.slice(z - 1, z), V = l3.slice(z + 2, z + 3), H = /\s|^$/.test(B), U = /\s|^$/.test(V), W = Punctuation.test(B), G = Punctuation.test(V);
      return l3.addDelimiter(StrikethroughDelim, z, z + 2, !U && (!G || H || W), !H && (!W || U || G));
    },
    after: "Emphasis"
  }]
};
function parseRow(l3, R, z = 0, B, V = 0) {
  let H = 0, U = true, W = -1, G = -1, K = false, q = () => {
    B.push(l3.elt("TableCell", V + W, V + G, l3.parser.parseInline(R.slice(W, G), V + W)));
  };
  for (let J = z; J < R.length; J++) {
    let z2 = R.charCodeAt(J);
    z2 == 124 && !K ? ((!U || W > -1) && H++, U = false, B && (W > -1 && q(), B.push(l3.elt("TableDelimiter", J + V, J + V + 1))), W = G = -1) : (K || z2 != 32 && z2 != 9) && (W < 0 && (W = J), G = J + 1), K = !K && z2 == 92;
  }
  return W > -1 && (H++, B && q()), H;
}
function hasPipe(l3, R) {
  for (let z = R; z < l3.length; z++) {
    let R2 = l3.charCodeAt(z);
    if (R2 == 124) return true;
    R2 == 92 && z++;
  }
  return false;
}
var delimiterLine = /^\|?(\s*:?-+:?\s*\|)+(\s*:?-+:?\s*)?$/, TableParser = class {
  constructor() {
    this.rows = null;
  }
  nextLine(l3, R, z) {
    if (this.rows == null) {
      this.rows = false;
      let B;
      if ((R.next == 45 || R.next == 58 || R.next == 124) && delimiterLine.test(B = R.text.slice(R.pos))) {
        let V = [];
        parseRow(l3, z.content, 0, V, z.start) == parseRow(l3, B, R.pos) && (this.rows = [l3.elt("TableHeader", z.start, z.start + z.content.length, V), l3.elt("TableDelimiter", l3.lineStart + R.pos, l3.lineStart + R.text.length)]);
      }
    } else if (this.rows) {
      let z2 = [];
      parseRow(l3, R.text, R.pos, z2, l3.lineStart), this.rows.push(l3.elt("TableRow", l3.lineStart + R.pos, l3.lineStart + R.text.length, z2));
    }
    return false;
  }
  finish(l3, R) {
    return this.rows ? (l3.addLeafElement(R, l3.elt("Table", R.start, R.start + R.content.length, this.rows)), true) : false;
  }
}, Table = {
  defineNodes: [
    {
      name: "Table",
      block: true
    },
    {
      name: "TableHeader",
      style: { "TableHeader/...": tags.heading }
    },
    "TableRow",
    {
      name: "TableCell",
      style: tags.content
    },
    {
      name: "TableDelimiter",
      style: tags.processingInstruction
    }
  ],
  parseBlock: [{
    name: "Table",
    leaf(l3, R) {
      return hasPipe(R.content, 0) ? new TableParser() : null;
    },
    endLeaf(l3, R, z) {
      if (z.parsers.some((l4) => l4 instanceof TableParser) || !hasPipe(R.text, R.basePos)) return false;
      let B = l3.peekLine();
      return delimiterLine.test(B) && parseRow(l3, R.text, R.basePos) == parseRow(l3, B, R.basePos);
    },
    before: "SetextHeading"
  }]
}, TaskParser = class {
  nextLine() {
    return false;
  }
  finish(l3, R) {
    return l3.addLeafElement(R, l3.elt("Task", R.start, R.start + R.content.length, [l3.elt("TaskMarker", R.start, R.start + 3), ...l3.parser.parseInline(R.content.slice(3), R.start + 3)])), true;
  }
}, TaskList = {
  defineNodes: [{
    name: "Task",
    block: true,
    style: tags.list
  }, {
    name: "TaskMarker",
    style: tags.atom
  }],
  parseBlock: [{
    name: "TaskList",
    leaf(l3, R) {
      return /^\[[ xX]\][ \t]/.test(R.content) && l3.parentType().name == "ListItem" ? new TaskParser() : null;
    },
    after: "SetextHeading"
  }]
}, autolinkRE = /(www\.)|(https?:\/\/)|([\w.+-]{1,100}@)|(mailto:|xmpp:)/gy, urlRE = /[\w-]+(\.[\w-]+)+(\/[^\s<]*)?/gy, lastTwoDomainWords = /[\w-]+\.[\w-]+($|\/)/, emailRE = /[\w.+-]+@[\w-]+(\.[\w.-]+)+/gy, xmppResourceRE = /\/[a-zA-Z\d@.]+/gy;
function count(l3, R, z, B) {
  let V = 0;
  for (let H = R; H < z; H++) l3[H] == B && V++;
  return V;
}
function autolinkURLEnd(l3, R) {
  urlRE.lastIndex = R;
  let z = urlRE.exec(l3);
  if (!z || lastTwoDomainWords.exec(z[0])[0].indexOf("_") > -1) return -1;
  let B = R + z[0].length;
  for (; ; ) {
    let z2 = l3[B - 1], V;
    if (/[?!.,:*_~]/.test(z2) || z2 == ")" && count(l3, R, B, ")") > count(l3, R, B, "(")) B--;
    else if (z2 == ";" && (V = /&(?:#\d+|#x[a-f\d]+|\w+);$/.exec(l3.slice(R, B)))) B = R + V.index;
    else break;
  }
  return B;
}
function autolinkEmailEnd(l3, R) {
  emailRE.lastIndex = R;
  let z = emailRE.exec(l3);
  if (!z) return -1;
  let B = z[0][z[0].length - 1];
  return B == "_" || B == "-" ? -1 : R + z[0].length - (B == "." ? 1 : 0);
}
var GFM = [
  Table,
  TaskList,
  Strikethrough,
  { parseInline: [{
    name: "Autolink",
    parse(l3, R, z) {
      let B = z - l3.offset;
      if (B && /\w/.test(l3.text[B - 1])) return -1;
      autolinkRE.lastIndex = B;
      let V = autolinkRE.exec(l3.text), H = -1;
      return !V || (V[1] || V[2] ? (H = autolinkURLEnd(l3.text, B + V[0].length), H > -1 && l3.hasOpenLink && (H = B + /([^\[\]]|\[[^\]]*\])*/.exec(l3.text.slice(B, H))[0].length)) : V[3] ? H = autolinkEmailEnd(l3.text, B) : (H = autolinkEmailEnd(l3.text, B + V[0].length), H > -1 && V[0] == "xmpp:" && (xmppResourceRE.lastIndex = H, V = xmppResourceRE.exec(l3.text), V && (H = V.index + V[0].length))), H < 0) ? -1 : (l3.addElement(l3.elt("URL", z, H + l3.offset)), H + l3.offset);
    }
  }] }
];
function parseSubSuper(l3, R, z) {
  return (B, V, H) => {
    if (V != l3 || B.char(H + 1) == l3) return -1;
    let U = [B.elt(z, H, H + 1)];
    for (let V2 = H + 1; V2 < B.end; V2++) {
      let W = B.char(V2);
      if (W == l3) return B.addElement(B.elt(R, H, V2 + 1, U.concat(B.elt(z, V2, V2 + 1))));
      if (W == 92 && U.push(B.elt("Escape", V2, V2++ + 2)), space(W)) break;
    }
    return -1;
  };
}
var Superscript = {
  defineNodes: [{
    name: "Superscript",
    style: tags.special(tags.content)
  }, {
    name: "SuperscriptMark",
    style: tags.processingInstruction
  }],
  parseInline: [{
    name: "Superscript",
    parse: parseSubSuper(94, "Superscript", "SuperscriptMark")
  }]
}, Subscript = {
  defineNodes: [{
    name: "Subscript",
    style: tags.special(tags.content)
  }, {
    name: "SubscriptMark",
    style: tags.processingInstruction
  }],
  parseInline: [{
    name: "Subscript",
    parse: parseSubSuper(126, "Subscript", "SubscriptMark")
  }]
}, Emoji = {
  defineNodes: [{
    name: "Emoji",
    style: tags.character
  }],
  parseInline: [{
    name: "Emoji",
    parse(l3, R, z) {
      let B;
      return R != 58 || !(B = /^[a-zA-Z_0-9]+:/.exec(l3.slice(z + 1, l3.end))) ? -1 : l3.addElement(l3.elt("Emoji", z, z + 1 + B[0].length));
    }
  }]
}, data = /* @__PURE__ */ defineLanguageFacet({ commentTokens: { block: {
  open: "<!--",
  close: "-->"
} } }), headingProp = /* @__PURE__ */ new NodeProp(), commonmark = /* @__PURE__ */ parser.configure({ props: [
  /* @__PURE__ */ foldNodeProp.add((l3) => !l3.is("Block") || l3.is("Document") || isHeading(l3) != null || isList(l3) ? void 0 : (l4, R) => ({
    from: R.doc.lineAt(l4.from).to,
    to: l4.to
  })),
  /* @__PURE__ */ headingProp.add(isHeading),
  /* @__PURE__ */ indentNodeProp.add({ Document: () => null }),
  /* @__PURE__ */ languageDataProp.add({ Document: data })
] });
function isHeading(l3) {
  let R = /^(?:ATX|Setext)Heading(\d)$/.exec(l3.name);
  return R ? +R[1] : void 0;
}
function isList(l3) {
  return l3.name == "OrderedList" || l3.name == "BulletList";
}
function findSectionEnd(l3, R) {
  let z = l3;
  for (; ; ) {
    let l4 = z.nextSibling, B;
    if (!l4 || (B = isHeading(l4.type)) != null && B <= R) break;
    z = l4;
  }
  return z.to;
}
var headerIndent = /* @__PURE__ */ foldService.of((l3, R, z) => {
  for (let B = syntaxTree(l3).resolveInner(z, -1); B && !(B.from < R); B = B.parent) {
    let l4 = B.type.prop(headingProp);
    if (l4 == null) continue;
    let R2 = findSectionEnd(B, l4);
    if (R2 > z) return {
      from: z,
      to: R2
    };
  }
  return null;
});
function mkLang(l3) {
  return new Language(data, l3, [], "markdown");
}
var commonmarkLanguage = /* @__PURE__ */ mkLang(commonmark), markdownLanguage = /* @__PURE__ */ mkLang(/* @__PURE__ */ commonmark.configure([
  GFM,
  Subscript,
  Superscript,
  Emoji,
  { props: [/* @__PURE__ */ foldNodeProp.add({ Table: (l3, R) => ({
    from: R.doc.lineAt(l3.from).to,
    to: l3.to
  }) })] }
]));
function getCodeParser(l3, R) {
  return (z) => {
    if (z && l3) {
      let R2 = null;
      if (z = /\S*/.exec(z)[0], R2 = typeof l3 == "function" ? l3(z) : LanguageDescription.matchLanguageName(l3, z, true), R2 instanceof LanguageDescription) return R2.support ? R2.support.language.parser : ParseContext.getSkippingParser(R2.load());
      if (R2) return R2.parser;
    }
    return R ? R.parser : null;
  };
}
var Context = class {
  constructor(l3, R, z, B, V, H, U) {
    this.node = l3, this.from = R, this.to = z, this.spaceBefore = B, this.spaceAfter = V, this.type = H, this.item = U;
  }
  blank(l3, R = true) {
    let z = this.spaceBefore + (this.node.name == "Blockquote" ? ">" : "");
    if (l3 != null) {
      for (; z.length < l3; ) z += " ";
      return z;
    } else {
      for (let l4 = this.to - this.from - z.length - this.spaceAfter.length; l4 > 0; l4--) z += " ";
      return z + (R ? this.spaceAfter : "");
    }
  }
  marker(l3, R) {
    let z = this.node.name == "OrderedList" ? String(+itemNumber(this.item, l3)[2] + R) : "";
    return this.spaceBefore + z + this.type + this.spaceAfter;
  }
};
function getContext(l3, R) {
  let z = [], B = [];
  for (let R2 = l3; R2; R2 = R2.parent) {
    if (R2.name == "FencedCode") return B;
    (R2.name == "ListItem" || R2.name == "Blockquote") && z.push(R2);
  }
  for (let l4 = z.length - 1; l4 >= 0; l4--) {
    let V = z[l4], H, U = R.lineAt(V.from), W = V.from - U.from;
    if (V.name == "Blockquote" && (H = /^ *>( ?)/.exec(U.text.slice(W)))) B.push(new Context(V, W, W + H[0].length, "", H[1], ">", null));
    else if (V.name == "ListItem" && V.parent.name == "OrderedList" && (H = /^( *)\d+([.)])( *)/.exec(U.text.slice(W)))) {
      let l5 = H[3], R2 = H[0].length;
      l5.length >= 4 && (l5 = l5.slice(0, l5.length - 4), R2 -= 4), B.push(new Context(V.parent, W, W + R2, H[1], l5, H[2], V));
    } else if (V.name == "ListItem" && V.parent.name == "BulletList" && (H = /^( *)([-+*])( {1,4}\[[ xX]\])?( +)/.exec(U.text.slice(W)))) {
      let l5 = H[4], R2 = H[0].length;
      l5.length > 4 && (l5 = l5.slice(0, l5.length - 4), R2 -= 4);
      let z2 = H[2];
      H[3] && (z2 += H[3].replace(/[xX]/, " ")), B.push(new Context(V.parent, W, W + R2, H[1], l5, z2, V));
    }
  }
  return B;
}
function itemNumber(l3, R) {
  return /^(\s*)(\d+)(?=[.)])/.exec(R.sliceString(l3.from, l3.from + 10));
}
function renumberList(l3, R, z, B = 0) {
  for (let V = -1, H = l3; ; ) {
    if (H.name == "ListItem") {
      let l5 = itemNumber(H, R), U = +l5[2];
      if (V >= 0) {
        if (U != V + 1) return;
        z.push({
          from: H.from + l5[1].length,
          to: H.from + l5[0].length,
          insert: String(V + 2 + B)
        });
      }
      V = U;
    }
    let l4 = H.nextSibling;
    if (!l4) break;
    H = l4;
  }
}
function normalizeIndent(l3, R) {
  let z = /^[ \t]*/.exec(l3)[0].length;
  if (!z || R.facet(indentUnit) != "	") return l3;
  let B = countColumn(l3, 4, z), V = "";
  for (let l4 = B; l4 > 0; ) l4 >= 4 ? (V += "	", l4 -= 4) : (V += " ", l4--);
  return V + l3.slice(z);
}
var insertNewlineContinueMarkupCommand = (l3 = {}) => ({ state: R, dispatch: z }) => {
  let V = syntaxTree(R), { doc: U } = R, W = null, G = R.changeByRange((z2) => {
    if (!z2.empty || !markdownLanguage.isActiveAt(R, z2.from, -1) && !markdownLanguage.isActiveAt(R, z2.from, 1)) return W = { range: z2 };
    let H = z2.from, G2 = U.lineAt(H), K = getContext(V.resolveInner(H, -1), U);
    for (; K.length && K[K.length - 1].from > H - G2.from; ) K.pop();
    if (!K.length) return W = { range: z2 };
    let q = K[K.length - 1];
    if (q.to - q.spaceAfter.length > H - G2.from) return W = { range: z2 };
    let J = H >= q.to - q.spaceAfter.length && !/\S/.test(G2.text.slice(q.to));
    if (q.item && J) {
      let z3 = q.node.firstChild, V2 = q.node.getChild("ListItem", "ListItem");
      if (z3.to >= H || V2 && V2.to < H || G2.from > 0 && !/[^\s>]/.test(U.lineAt(G2.from - 1).text) || l3.nonTightLists === false) {
        let l4 = K.length > 1 ? K[K.length - 2] : null, R2, z4 = "";
        l4 && l4.item ? (R2 = G2.from + l4.from, z4 = l4.marker(U, 1)) : R2 = G2.from + (l4 ? l4.to : 0);
        let V3 = [{
          from: R2,
          to: H,
          insert: z4
        }];
        return q.node.name == "OrderedList" && renumberList(q.item, U, V3, -2), l4 && l4.node.name == "OrderedList" && renumberList(l4.item, U, V3), {
          range: EditorSelection.cursor(R2 + z4.length),
          changes: V3
        };
      } else {
        let l4 = blankLine(K, R, G2);
        return {
          range: EditorSelection.cursor(H + l4.length + 1),
          changes: {
            from: G2.from,
            insert: l4 + R.lineBreak
          }
        };
      }
    }
    if (q.node.name == "Blockquote" && J && G2.from) {
      let l4 = U.lineAt(G2.from - 1), B = />\s*$/.exec(l4.text);
      if (B && B.index == q.from) {
        let V2 = R.changes([{
          from: l4.from + B.index,
          to: l4.to
        }, {
          from: G2.from + q.from,
          to: G2.to
        }]);
        return {
          range: z2.map(V2),
          changes: V2
        };
      }
    }
    let Y = [];
    q.node.name == "OrderedList" && renumberList(q.item, U, Y);
    let Z = q.item && q.item.from < G2.from, Q = "";
    if (!Z || /^[\s\d.)\-+*>]*/.exec(G2.text)[0].length >= q.to) for (let l4 = 0, R2 = K.length - 1; l4 <= R2; l4++) Q += l4 == R2 && !Z ? K[l4].marker(U, 1) : K[l4].blank(l4 < R2 ? countColumn(G2.text, 4, K[l4 + 1].from) - Q.length : null);
    let $ = H;
    for (; $ > G2.from && /\s/.test(G2.text.charAt($ - G2.from - 1)); ) $--;
    return Q = normalizeIndent(Q, R), nonTightList(q.node, R.doc) && (Q = blankLine(K, R, G2) + R.lineBreak + Q), Y.push({
      from: $,
      to: H,
      insert: R.lineBreak + Q
    }), {
      range: EditorSelection.cursor($ + Q.length + 1),
      changes: Y
    };
  });
  return W ? false : (z(R.update(G, {
    scrollIntoView: true,
    userEvent: "input"
  })), true);
}, insertNewlineContinueMarkup = /* @__PURE__ */ insertNewlineContinueMarkupCommand();
function isMark(l3) {
  return l3.name == "QuoteMark" || l3.name == "ListMark";
}
function nonTightList(l3, R) {
  if (l3.name != "OrderedList" && l3.name != "BulletList") return false;
  let z = l3.firstChild, B = l3.getChild("ListItem", "ListItem");
  if (!B) return false;
  let V = R.lineAt(z.to), H = R.lineAt(B.from), U = /^[\s>]*$/.test(V.text);
  return V.number + (U ? 0 : 1) < H.number;
}
function blankLine(l3, R, z) {
  let B = "";
  for (let R2 = 0, V = l3.length - 2; R2 <= V; R2++) B += l3[R2].blank(R2 < V ? countColumn(z.text, 4, l3[R2 + 1].from) - B.length : null, R2 < V);
  return normalizeIndent(B, R);
}
function contextNodeForDelete(l3, R) {
  let z = l3.resolveInner(R, -1), B = R;
  isMark(z) && (B = z.from, z = z.parent);
  for (let l4; l4 = z.childBefore(B); ) if (isMark(l4)) B = l4.from;
  else if (l4.name == "OrderedList" || l4.name == "BulletList") z = l4.lastChild, B = z.to;
  else break;
  return z;
}
var deleteMarkupBackward = ({ state: l3, dispatch: R }) => {
  let z = syntaxTree(l3), V = null, U = l3.changeByRange((R2) => {
    let H = R2.from, { doc: U2 } = l3;
    if (R2.empty && markdownLanguage.isActiveAt(l3, R2.from)) {
      let R3 = U2.lineAt(H), V2 = getContext(contextNodeForDelete(z, H), U2);
      if (V2.length) {
        let z2 = V2[V2.length - 1], U3 = z2.to - z2.spaceAfter.length + (z2.spaceAfter ? 1 : 0);
        if (H - R3.from > U3 && !/\S/.test(R3.text.slice(U3, H - R3.from))) return {
          range: EditorSelection.cursor(R3.from + U3),
          changes: {
            from: R3.from + U3,
            to: H
          }
        };
        if (H - R3.from == U3 && (!z2.item || R3.from <= z2.item.from || !/\S/.test(R3.text.slice(0, z2.to)))) {
          let V3 = R3.from + z2.from;
          if (z2.item && z2.node.from < z2.item.from && /\S/.test(R3.text.slice(z2.from, z2.to))) {
            let H2 = z2.blank(countColumn(R3.text, 4, z2.to) - countColumn(R3.text, 4, z2.from));
            return V3 == R3.from && (H2 = normalizeIndent(H2, l3)), {
              range: EditorSelection.cursor(V3 + H2.length),
              changes: {
                from: V3,
                to: R3.from + z2.to,
                insert: H2
              }
            };
          }
          if (V3 < H) return {
            range: EditorSelection.cursor(V3),
            changes: {
              from: V3,
              to: H
            }
          };
        }
      }
    }
    return V = { range: R2 };
  });
  return V ? false : (R(l3.update(U, {
    scrollIntoView: true,
    userEvent: "delete"
  })), true);
}, markdownKeymap = [{
  key: "Enter",
  run: insertNewlineContinueMarkup
}, {
  key: "Backspace",
  run: deleteMarkupBackward
}], htmlNoMatch = /* @__PURE__ */ html({ matchClosingTags: false });
function markdown(R = {}) {
  let { codeLanguages: z, defaultCodeLanguage: B, addKeymap: V = true, base: { parser: H } = commonmarkLanguage, completeHTMLTags: U = true, pasteURLAsLink: W = true, htmlTagLanguage: G = htmlNoMatch } = R;
  if (!(H instanceof MarkdownParser)) throw RangeError("Base parser provided to `markdown` should be a Markdown parser");
  let K = R.extensions ? [R.extensions] : [], q = [G.support, headerIndent], J;
  W && q.push(pasteURLAsLink), B instanceof LanguageSupport ? (q.push(B.support), J = B.language) : B && (J = B);
  let Y = z || J ? getCodeParser(z, J) : void 0;
  K.push(parseCode({
    codeParser: Y,
    htmlParser: G.language.parser
  })), V && q.push(Prec.high(keymap.of(markdownKeymap)));
  let X = mkLang(H.configure(K));
  return U && q.push(X.data.of({ autocomplete: htmlTagCompletion })), new LanguageSupport(X, q);
}
function htmlTagCompletion(l3) {
  let { state: R, pos: z } = l3, B = /<[:\-\.\w\u00b7-\uffff]*$/.exec(R.sliceDoc(z - 25, z));
  if (!B) return null;
  let V = syntaxTree(R).resolveInner(z, -1);
  for (; V && !V.type.isTop; ) {
    if (V.name == "CodeBlock" || V.name == "FencedCode" || V.name == "ProcessingInstructionBlock" || V.name == "CommentBlock" || V.name == "Link" || V.name == "Image") return null;
    V = V.parent;
  }
  return {
    from: z - B[0].length,
    to: z,
    options: htmlTagCompletions(),
    validFor: /^<[:\-\.\w\u00b7-\uffff]*$/
  };
}
var _tagCompletions = null;
function htmlTagCompletions() {
  if (_tagCompletions) return _tagCompletions;
  let l3 = htmlCompletionSource(new CompletionContext(EditorState.create({ extensions: htmlNoMatch }), 0, true));
  return _tagCompletions = l3 ? l3.options : [];
}
var nonPlainText = /code|horizontalrule|html|link|comment|processing|escape|entity|image|mark|url/i, pasteURLAsLink = /* @__PURE__ */ EditorView.domEventHandlers({ paste: (l3, R) => {
  var _a;
  let { main: z } = R.state.selection;
  if (z.empty) return false;
  let B = (_a = l3.clipboardData) == null ? void 0 : _a.getData("text/plain");
  if (!B || !/^(https?:\/\/|mailto:|xmpp:|www\.)/.test(B) || (/^www\./.test(B) && (B = "https://" + B), !markdownLanguage.isActiveAt(R.state, z.from, 1))) return false;
  let V = syntaxTree(R.state), U = false;
  return V.iterate({
    from: z.from,
    to: z.to,
    enter: (l4) => {
      (l4.from > z.from || nonPlainText.test(l4.name)) && (U = true);
    },
    leave: (l4) => {
      l4.to < z.to && (U = true);
    }
  }), U ? false : (R.dispatch({
    changes: [{
      from: z.from,
      insert: "["
    }, {
      from: z.to,
      insert: `](${B})`
    }],
    userEvent: "input.paste",
    scrollIntoView: true
  }), true);
} });
export {
  markdown as a,
  pasteURLAsLink as c,
  insertNewlineContinueMarkupCommand as i,
  deleteMarkupBackward as n,
  markdownKeymap as o,
  insertNewlineContinueMarkup as r,
  markdownLanguage as s,
  commonmarkLanguage as t
};
