index.es.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. var G = (p, h, n) => {
  2. if (!h.has(p))
  3. throw TypeError("Cannot " + n);
  4. };
  5. var r = (p, h, n) => (G(p, h, "read from private field"), n ? n.call(p) : h.get(p)), c = (p, h, n) => {
  6. if (h.has(p))
  7. throw TypeError("Cannot add the same private member more than once");
  8. h instanceof WeakSet ? h.add(p) : h.set(p, n);
  9. }, o = (p, h, n, t) => (G(p, h, "write to private field"), t ? t.call(p, n) : h.set(p, n), n);
  10. import { stackOverFlow as L, parserMatchError as W, createNodeInParserFail as X, serializerMatchError as Y } from "@milkdown/exception";
  11. import { Mark as D } from "@milkdown/prose/model";
  12. class Q {
  13. }
  14. class U {
  15. constructor() {
  16. this.elements = [], this.size = () => this.elements.length, this.top = () => this.elements.at(-1), this.push = (h) => {
  17. var n;
  18. (n = this.top()) == null || n.push(h);
  19. }, this.open = (h) => {
  20. this.elements.push(h);
  21. }, this.close = () => {
  22. const h = this.elements.pop();
  23. if (!h)
  24. throw L();
  25. return h;
  26. };
  27. }
  28. }
  29. class B extends Q {
  30. constructor(h, n, t) {
  31. super(), this.type = h, this.content = n, this.attrs = t;
  32. }
  33. push(h, ...n) {
  34. this.content.push(h, ...n);
  35. }
  36. pop() {
  37. return this.content.pop();
  38. }
  39. static create(h, n, t) {
  40. return new B(h, n, t);
  41. }
  42. }
  43. var d, N, O, T, F, k, M;
  44. const S = class S extends U {
  45. /// @internal
  46. constructor(n) {
  47. super();
  48. c(this, d, void 0);
  49. c(this, N, void 0);
  50. c(this, O, void 0);
  51. c(this, T, void 0);
  52. c(this, F, void 0);
  53. c(this, k, void 0);
  54. c(this, M, void 0);
  55. o(this, d, D.none), o(this, N, (t) => t.isText), o(this, O, (t, s) => {
  56. if (r(this, N).call(this, t) && r(this, N).call(this, s) && D.sameSet(t.marks, s.marks))
  57. return this.schema.text(t.text + s.text, t.marks);
  58. }), o(this, T, (t) => {
  59. const s = Object.values({ ...this.schema.nodes, ...this.schema.marks }).find((e) => e.spec.parseMarkdown.match(t));
  60. if (!s)
  61. throw W(t);
  62. return s;
  63. }), o(this, F, (t) => {
  64. const s = r(this, T).call(this, t);
  65. s.spec.parseMarkdown.runner(this, t, s);
  66. }), this.injectRoot = (t, s, e) => (this.openNode(s, e), this.next(t.children), this), this.openNode = (t, s) => (this.open(B.create(t, [], s)), this), o(this, k, () => {
  67. o(this, d, D.none);
  68. const t = this.close();
  69. return r(this, M).call(this, t.type, t.attrs, t.content);
  70. }), this.closeNode = () => (r(this, k).call(this), this), o(this, M, (t, s, e) => {
  71. const i = t.createAndFill(s, e, r(this, d));
  72. if (!i)
  73. throw X(t, s, e);
  74. return this.push(i), i;
  75. }), this.addNode = (t, s, e) => (r(this, M).call(this, t, s, e), this), this.openMark = (t, s) => {
  76. const e = t.create(s);
  77. return o(this, d, e.addToSet(r(this, d))), this;
  78. }, this.closeMark = (t) => (o(this, d, t.removeFromSet(r(this, d))), this), this.addText = (t) => {
  79. const s = this.top();
  80. if (!s)
  81. throw L();
  82. const e = s.pop(), i = this.schema.text(t, r(this, d));
  83. if (!e)
  84. return s.push(i), this;
  85. const a = r(this, O).call(this, e, i);
  86. return a ? (s.push(a), this) : (s.push(e, i), this);
  87. }, this.build = () => {
  88. let t;
  89. do
  90. t = r(this, k).call(this);
  91. while (this.size());
  92. return t;
  93. }, this.next = (t = []) => ([t].flat().forEach((s) => r(this, F).call(this, s)), this), this.toDoc = () => this.build(), this.run = (t, s) => {
  94. const e = t.runSync(t.parse(s), s);
  95. return this.next(e), this;
  96. }, this.schema = n;
  97. }
  98. };
  99. d = new WeakMap(), N = new WeakMap(), O = new WeakMap(), T = new WeakMap(), F = new WeakMap(), k = new WeakMap(), M = new WeakMap(), S.create = (n, t) => {
  100. const s = new S(n);
  101. return (e) => (s.run(t, e), s.toDoc());
  102. };
  103. let H = S;
  104. const q = class q extends Q {
  105. constructor(h, n, t, s = {}) {
  106. super(), this.type = h, this.children = n, this.value = t, this.props = s, this.push = (e, ...i) => {
  107. this.children || (this.children = []), this.children.push(e, ...i);
  108. }, this.pop = () => {
  109. var e;
  110. return (e = this.children) == null ? void 0 : e.pop();
  111. };
  112. }
  113. };
  114. q.create = (h, n, t, s = {}) => new q(h, n, t, s);
  115. let J = q;
  116. const Z = (p) => Object.prototype.hasOwnProperty.call(p, "size");
  117. var l, v, A, E, w, j, x, R, m, g, C, P;
  118. const z = class z extends U {
  119. /// @internal
  120. constructor(n) {
  121. super();
  122. c(this, l, void 0);
  123. c(this, v, void 0);
  124. c(this, A, void 0);
  125. c(this, E, void 0);
  126. c(this, w, void 0);
  127. c(this, j, void 0);
  128. c(this, x, void 0);
  129. c(this, R, void 0);
  130. c(this, m, void 0);
  131. c(this, g, void 0);
  132. c(this, C, void 0);
  133. c(this, P, void 0);
  134. o(this, l, D.none), o(this, v, (t) => {
  135. const s = Object.values({ ...this.schema.nodes, ...this.schema.marks }).find((e) => e.spec.toMarkdown.match(t));
  136. if (!s)
  137. throw Y(t.type);
  138. return s;
  139. }), o(this, A, (t) => r(this, v).call(this, t).spec.toMarkdown.runner(this, t)), o(this, E, (t, s) => r(this, v).call(this, t).spec.toMarkdown.runner(this, t, s)), o(this, w, (t) => {
  140. const { marks: s } = t, e = (u) => u.type.spec.priority ?? 50;
  141. [...s].sort((u, f) => e(u) - e(f)).every((u) => !r(this, E).call(this, u, t)) && r(this, A).call(this, t), s.forEach((u) => r(this, P).call(this, u));
  142. }), o(this, j, (t, s) => {
  143. var f;
  144. if (t.type === s || ((f = t.children) == null ? void 0 : f.length) !== 1)
  145. return t;
  146. const e = (y) => {
  147. var I;
  148. if (y.type === s)
  149. return y;
  150. if (((I = y.children) == null ? void 0 : I.length) !== 1)
  151. return null;
  152. const [b] = y.children;
  153. return b ? e(b) : null;
  154. }, i = e(t);
  155. if (!i)
  156. return t;
  157. const a = i.children ? [...i.children] : void 0, u = { ...t, children: a };
  158. return u.children = a, i.children = [u], i;
  159. }), o(this, x, (t) => {
  160. const { children: s } = t;
  161. return s && (t.children = s.reduce((e, i, a) => {
  162. if (a === 0)
  163. return [i];
  164. const u = e.at(-1);
  165. if (u && u.isMark && i.isMark) {
  166. i = r(this, j).call(this, i, u.type);
  167. const { children: f, ...y } = i, { children: b, ...I } = u;
  168. if (i.type === u.type && f && b && JSON.stringify(y) === JSON.stringify(I)) {
  169. const V = {
  170. ...I,
  171. children: [...b, ...f]
  172. };
  173. return e.slice(0, -1).concat(r(this, x).call(this, V));
  174. }
  175. }
  176. return e.concat(i);
  177. }, [])), t;
  178. }), o(this, R, (t) => {
  179. const s = {
  180. ...t.props,
  181. type: t.type
  182. };
  183. return t.children && (s.children = t.children), t.value && (s.value = t.value), s;
  184. }), this.openNode = (t, s, e) => (this.open(J.create(t, void 0, s, e)), this), o(this, m, () => {
  185. const t = this.close();
  186. return r(this, g).call(this, t.type, t.children, t.value, t.props);
  187. }), this.closeNode = () => (r(this, m).call(this), this), o(this, g, (t, s, e, i) => {
  188. const a = J.create(t, s, e, i), u = r(this, x).call(this, r(this, R).call(this, a));
  189. return this.push(u), u;
  190. }), this.addNode = (t, s, e, i) => (r(this, g).call(this, t, s, e, i), this), o(this, C, (t, s, e, i) => t.isInSet(r(this, l)) ? this : (o(this, l, t.addToSet(r(this, l))), this.openNode(s, e, { ...i, isMark: !0 }))), o(this, P, (t) => {
  191. t.isInSet(r(this, l)) && (o(this, l, t.type.removeFromSet(r(this, l))), r(this, m).call(this));
  192. }), this.withMark = (t, s, e, i) => (r(this, C).call(this, t, s, e, i), this), this.closeMark = (t) => (r(this, P).call(this, t), this), this.build = () => {
  193. let t = null;
  194. do
  195. t = r(this, m).call(this);
  196. while (this.size());
  197. return t;
  198. }, this.next = (t) => Z(t) ? (t.forEach((s) => {
  199. r(this, w).call(this, s);
  200. }), this) : (r(this, w).call(this, t), this), this.toString = (t) => t.stringify(this.build()), this.run = (t) => (this.next(t), this), this.schema = n;
  201. }
  202. };
  203. l = new WeakMap(), v = new WeakMap(), A = new WeakMap(), E = new WeakMap(), w = new WeakMap(), j = new WeakMap(), x = new WeakMap(), R = new WeakMap(), m = new WeakMap(), g = new WeakMap(), C = new WeakMap(), P = new WeakMap(), z.create = (n, t) => {
  204. const s = new z(n);
  205. return (e) => (s.run(e), s.toString(t));
  206. };
  207. let K = z;
  208. export {
  209. H as ParserState,
  210. K as SerializerState,
  211. U as Stack,
  212. Q as StackElement
  213. };
  214. //# sourceMappingURL=index.es.js.map