links-notation 0.11.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -2,7 +2,14 @@
2
2
  class Link {
3
3
  constructor(id = null, values = null) {
4
4
  this.id = id;
5
- this.values = values || [];
5
+ if (values !== null && values !== undefined) {
6
+ if (!Array.isArray(values)) {
7
+ throw new TypeError("values must be an array or null");
8
+ }
9
+ this.values = values;
10
+ } else {
11
+ this.values = [];
12
+ }
6
13
  }
7
14
  toString() {
8
15
  return this.format(false);
@@ -16,7 +23,9 @@ class Link {
16
23
  } else if (this.values.length === 1) {
17
24
  return this.values[0];
18
25
  } else {
19
- const newValues = this.values.map((v) => v.simplify());
26
+ const newValues = this.values.map((v) => {
27
+ return v && typeof v.simplify === "function" ? v.simplify() : v;
28
+ });
20
29
  return new Link(this.id, newValues);
21
30
  }
22
31
  }
@@ -24,21 +33,29 @@ class Link {
24
33
  return new Link(null, [this, other]);
25
34
  }
26
35
  static getValueString(value) {
27
- return value.toLinkOrIdString();
36
+ return value && typeof value.toLinkOrIdString === "function" ? value.toLinkOrIdString() : String(value);
28
37
  }
29
38
  static escapeReference(reference) {
30
39
  if (!reference || reference.trim() === "") {
31
40
  return "";
32
41
  }
33
- const needsSingleQuotes = reference.includes(":") || reference.includes("(") || reference.includes(")") || reference.includes(" ") || reference.includes("\t") || reference.includes(`
34
- `) || reference.includes("\r") || reference.includes('"');
35
- if (needsSingleQuotes) {
42
+ const hasSingleQuote = reference.includes("'");
43
+ const hasDoubleQuote = reference.includes('"');
44
+ const needsQuoting = reference.includes(":") || reference.includes("(") || reference.includes(")") || reference.includes(" ") || reference.includes("\t") || reference.includes(`
45
+ `) || reference.includes("\r") || hasDoubleQuote || hasSingleQuote;
46
+ if (hasSingleQuote && hasDoubleQuote) {
47
+ return `'${reference.replace(/'/g, "\\'")}'`;
48
+ }
49
+ if (hasDoubleQuote) {
36
50
  return `'${reference}'`;
37
- } else if (reference.includes("'")) {
51
+ }
52
+ if (hasSingleQuote) {
38
53
  return `"${reference}"`;
39
- } else {
40
- return reference;
41
54
  }
55
+ if (needsQuoting) {
56
+ return `'${reference}'`;
57
+ }
58
+ return reference;
42
59
  }
43
60
  toLinkOrIdString() {
44
61
  if (!this.values || this.values.length === 0) {
@@ -51,16 +68,27 @@ class Link {
51
68
  return false;
52
69
  if (this.id !== other.id)
53
70
  return false;
54
- if (this.values.length !== other.values.length)
71
+ const thisValues = this.values || [];
72
+ const otherValues = other.values || [];
73
+ if (thisValues.length !== otherValues.length)
55
74
  return false;
56
- for (let i = 0;i < this.values.length; i++) {
57
- if (!this.values[i].equals(other.values[i])) {
58
- return false;
75
+ for (let i = 0;i < thisValues.length; i++) {
76
+ if (thisValues[i] && typeof thisValues[i].equals === "function") {
77
+ if (!thisValues[i].equals(otherValues[i])) {
78
+ return false;
79
+ }
80
+ } else {
81
+ if (thisValues[i] !== otherValues[i]) {
82
+ return false;
83
+ }
59
84
  }
60
85
  }
61
86
  return true;
62
87
  }
63
88
  format(lessParentheses = false, isCompoundValue = false) {
89
+ if (lessParentheses && typeof lessParentheses === "object" && (lessParentheses.constructor.name === "FormatOptions" || lessParentheses.constructor.name === "FormatConfig")) {
90
+ return this._formatWithOptions(lessParentheses, isCompoundValue);
91
+ }
64
92
  if (this.id === null && (!this.values || this.values.length === 0)) {
65
93
  return lessParentheses ? "" : "()";
66
94
  }
@@ -88,8 +116,8 @@ class Link {
88
116
  return lessParentheses && !this.needsParentheses(this.id) ? withColon : `(${withColon})`;
89
117
  }
90
118
  formatValue(value) {
91
- if (!value.format) {
92
- return Link.escapeReference(value.id || "");
119
+ if (!value || !value.format) {
120
+ return Link.escapeReference(value && value.id || "");
93
121
  }
94
122
  const isCompoundFromPaths = this._isFromPathCombination === true;
95
123
  if (isCompoundFromPaths) {
@@ -103,10 +131,110 @@ class Link {
103
131
  needsParentheses(str) {
104
132
  return str && (str.includes(" ") || str.includes(":") || str.includes("(") || str.includes(")"));
105
133
  }
134
+ _formatWithOptions(options, isCompoundValue = false) {
135
+ if (this.id === null && (!this.values || this.values.length === 0)) {
136
+ return options.lessParentheses ? "" : "()";
137
+ }
138
+ if (!this.values || this.values.length === 0) {
139
+ const escapedId = Link.escapeReference(this.id);
140
+ if (isCompoundValue) {
141
+ return `(${escapedId})`;
142
+ }
143
+ return options.lessParentheses && !this.needsParentheses(this.id) ? escapedId : `(${escapedId})`;
144
+ }
145
+ let shouldIndent = false;
146
+ if (options.shouldIndentByRefCount(this.values.length)) {
147
+ shouldIndent = true;
148
+ } else {
149
+ const valuesStr2 = this.values.map((v) => this.formatValue(v)).join(" ");
150
+ let testLine;
151
+ if (this.id !== null) {
152
+ const idStr2 = Link.escapeReference(this.id);
153
+ testLine = options.lessParentheses ? `${idStr2}: ${valuesStr2}` : `(${idStr2}: ${valuesStr2})`;
154
+ } else {
155
+ testLine = options.lessParentheses ? valuesStr2 : `(${valuesStr2})`;
156
+ }
157
+ if (options.shouldIndentByLength(testLine)) {
158
+ shouldIndent = true;
159
+ }
160
+ }
161
+ if (shouldIndent && options.preferInline === false) {
162
+ return this._formatIndented(options);
163
+ }
164
+ const valuesStr = this.values.map((v) => this.formatValue(v)).join(" ");
165
+ if (this.id === null) {
166
+ if (options.lessParentheses) {
167
+ const allSimple = this.values.every((v) => !v.values || v.values.length === 0);
168
+ if (allSimple) {
169
+ return this.values.map((v) => Link.escapeReference(v.id)).join(" ");
170
+ }
171
+ return valuesStr;
172
+ }
173
+ return `(${valuesStr})`;
174
+ }
175
+ const idStr = Link.escapeReference(this.id);
176
+ const withColon = `${idStr}: ${valuesStr}`;
177
+ return options.lessParentheses && !this.needsParentheses(this.id) ? withColon : `(${withColon})`;
178
+ }
179
+ _formatIndented(options) {
180
+ if (this.id === null) {
181
+ const lines2 = this.values.map((v) => options.indentString + this.formatValue(v));
182
+ return lines2.join(`
183
+ `);
184
+ }
185
+ const idStr = Link.escapeReference(this.id);
186
+ const lines = [`${idStr}:`];
187
+ for (const v of this.values) {
188
+ lines.push(options.indentString + this.formatValue(v));
189
+ }
190
+ return lines.join(`
191
+ `);
192
+ }
193
+ }
194
+ function _groupConsecutiveLinks(links) {
195
+ if (!links || links.length === 0) {
196
+ return links;
197
+ }
198
+ const grouped = [];
199
+ let i = 0;
200
+ while (i < links.length) {
201
+ const current = links[i];
202
+ if (current.id !== null && current.values && current.values.length > 0) {
203
+ const sameIdValues = [...current.values];
204
+ let j = i + 1;
205
+ while (j < links.length) {
206
+ const nextLink = links[j];
207
+ if (nextLink.id === current.id && nextLink.values && nextLink.values.length > 0) {
208
+ sameIdValues.push(...nextLink.values);
209
+ j++;
210
+ } else {
211
+ break;
212
+ }
213
+ }
214
+ if (j > i + 1) {
215
+ const groupedLink = new Link(current.id, sameIdValues);
216
+ grouped.push(groupedLink);
217
+ i = j;
218
+ continue;
219
+ }
220
+ }
221
+ grouped.push(current);
222
+ i++;
223
+ }
224
+ return grouped;
106
225
  }
107
226
  function formatLinks(links, lessParentheses = false) {
108
227
  if (!links || links.length === 0)
109
228
  return "";
229
+ if (lessParentheses && typeof lessParentheses === "object" && (lessParentheses.constructor.name === "FormatOptions" || lessParentheses.constructor.name === "FormatConfig")) {
230
+ const options = lessParentheses;
231
+ let linksToFormat = links;
232
+ if (options.groupConsecutive) {
233
+ linksToFormat = _groupConsecutiveLinks(links);
234
+ }
235
+ return linksToFormat.map((link) => link.format(options)).join(`
236
+ `);
237
+ }
110
238
  return links.map((link) => link.format(lessParentheses)).join(`
111
239
  `);
112
240
  }
@@ -256,114 +384,127 @@ function peg$parse(input, options) {
256
384
  const peg$c3 = '"';
257
385
  const peg$c4 = "'";
258
386
  const peg$c5 = " ";
259
- const peg$r0 = /^[^"]/;
260
- const peg$r1 = /^[^']/;
261
- const peg$r2 = /^[\r\n]/;
262
- const peg$r3 = /^[ \t]/;
387
+ const peg$r0 = /^[ \t]/;
388
+ const peg$r1 = /^[\r\n]/;
389
+ const peg$r2 = /^[^"]/;
390
+ const peg$r3 = /^[^']/;
263
391
  const peg$r4 = /^[ \t\n\r]/;
264
392
  const peg$r5 = /^[^ \t\n\r(:)]/;
265
- const peg$e0 = peg$literalExpectation(":", false);
266
- const peg$e1 = peg$literalExpectation("(", false);
267
- const peg$e2 = peg$literalExpectation(")", false);
268
- const peg$e3 = peg$literalExpectation('"', false);
269
- const peg$e4 = peg$classExpectation(['"'], true, false, false);
270
- const peg$e5 = peg$literalExpectation("'", false);
271
- const peg$e6 = peg$classExpectation(["'"], true, false, false);
272
- const peg$e7 = peg$literalExpectation(" ", false);
273
- const peg$e8 = peg$classExpectation(["\r", `
393
+ const peg$e0 = peg$classExpectation([" ", "\t"], false, false, false);
394
+ const peg$e1 = peg$classExpectation(["\r", `
274
395
  `], false, false, false);
275
- const peg$e9 = peg$anyExpectation();
276
- const peg$e10 = peg$classExpectation([" ", "\t"], false, false, false);
396
+ const peg$e2 = peg$literalExpectation(":", false);
397
+ const peg$e3 = peg$literalExpectation("(", false);
398
+ const peg$e4 = peg$literalExpectation(")", false);
399
+ const peg$e5 = peg$literalExpectation('"', false);
400
+ const peg$e6 = peg$classExpectation(['"'], true, false, false);
401
+ const peg$e7 = peg$literalExpectation("'", false);
402
+ const peg$e8 = peg$classExpectation(["'"], true, false, false);
403
+ const peg$e9 = peg$literalExpectation(" ", false);
404
+ const peg$e10 = peg$anyExpectation();
277
405
  const peg$e11 = peg$classExpectation([" ", "\t", `
278
406
  `, "\r"], false, false, false);
279
407
  const peg$e12 = peg$classExpectation([" ", "\t", `
280
408
  `, "\r", "(", ":", ")"], true, false, false);
281
- function peg$f0(links) {
409
+ function peg$f0() {
410
+ indentationStack = [0];
411
+ baseIndentation = null;
412
+ return true;
413
+ }
414
+ function peg$f1(links) {
282
415
  return links;
283
416
  }
284
- function peg$f1() {
417
+ function peg$f2() {
418
+ indentationStack = [0];
419
+ baseIndentation = null;
420
+ return true;
421
+ }
422
+ function peg$f3() {
285
423
  return [];
286
424
  }
287
- function peg$f2(fl, list) {
425
+ function peg$f4(fl, list) {
288
426
  popIndentation();
289
427
  return [fl].concat(list || []);
290
428
  }
291
- function peg$f3(l) {
429
+ function peg$f5(l) {
292
430
  return l;
293
431
  }
294
- function peg$f4(l) {
432
+ function peg$f6(l) {
295
433
  return l;
296
434
  }
297
- function peg$f5(e, l) {
435
+ function peg$f7(e, l) {
298
436
  return { id: e.id, values: e.values, children: l };
299
437
  }
300
- function peg$f6(e) {
438
+ function peg$f8(e) {
301
439
  return e;
302
440
  }
303
- function peg$f7(l) {
441
+ function peg$f9(l) {
304
442
  return l;
305
443
  }
306
- function peg$f8(i) {
444
+ function peg$f10(i) {
307
445
  return { id: i };
308
446
  }
309
- function peg$f9(ml) {
447
+ function peg$f11(ml) {
310
448
  return ml;
311
449
  }
312
- function peg$f10(il) {
450
+ function peg$f12(il) {
313
451
  return il;
314
452
  }
315
- function peg$f11(sl) {
453
+ function peg$f13(sl) {
316
454
  return sl;
317
455
  }
318
- function peg$f12(fl) {
456
+ function peg$f14(fl) {
319
457
  return fl;
320
458
  }
321
- function peg$f13(vl) {
459
+ function peg$f15(vl) {
322
460
  return vl;
323
461
  }
324
- function peg$f14(value) {
462
+ function peg$f16(value) {
325
463
  return value;
326
464
  }
327
- function peg$f15(list) {
465
+ function peg$f17(list) {
328
466
  return list;
329
467
  }
330
- function peg$f16(value) {
468
+ function peg$f18(value) {
331
469
  return value;
332
470
  }
333
- function peg$f17(list) {
471
+ function peg$f19(list) {
334
472
  return list;
335
473
  }
336
- function peg$f18(id, v) {
474
+ function peg$f20(id, v) {
337
475
  return { id, values: v };
338
476
  }
339
- function peg$f19(id, v) {
477
+ function peg$f21(id, v) {
340
478
  return { id, values: v };
341
479
  }
342
- function peg$f20(v) {
480
+ function peg$f22(v) {
343
481
  return { values: v };
344
482
  }
345
- function peg$f21(v) {
483
+ function peg$f23(v) {
346
484
  return { values: v };
347
485
  }
348
- function peg$f22(id) {
486
+ function peg$f24(id) {
349
487
  return { id, values: [] };
350
488
  }
351
- function peg$f23(chars) {
489
+ function peg$f25(chars) {
352
490
  return chars.join("");
353
491
  }
354
- function peg$f24(r) {
492
+ function peg$f26(r) {
355
493
  return r.join("");
356
494
  }
357
- function peg$f25(r) {
495
+ function peg$f27(r) {
358
496
  return r.join("");
359
497
  }
360
- function peg$f26(spaces) {
361
- return spaces.length > getCurrentIndentation();
498
+ function peg$f28(spaces) {
499
+ setBaseIndentation(spaces);
500
+ }
501
+ function peg$f29(spaces) {
502
+ return normalizeIndentation(spaces) > getCurrentIndentation();
362
503
  }
363
- function peg$f27(spaces) {
504
+ function peg$f30(spaces) {
364
505
  pushIndentation(spaces);
365
506
  }
366
- function peg$f28(spaces) {
507
+ function peg$f31(spaces) {
367
508
  return checkIndentation(spaces);
368
509
  }
369
510
  let peg$currPos = options.peg$currPos | 0;
@@ -494,15 +635,28 @@ function peg$parse(input, options) {
494
635
  return new peg$SyntaxError(peg$SyntaxError.buildMessage(expected2, found), expected2, found, location2);
495
636
  }
496
637
  function peg$parsedocument() {
497
- let s0, s1, s2, s3;
638
+ let s0, s1, s2, s3, s4, s5;
498
639
  s0 = peg$currPos;
499
- s1 = peg$parse_();
500
- s2 = peg$parselinks();
501
- if (s2 !== peg$FAILED) {
502
- s3 = peg$parseeof();
640
+ peg$savedPos = peg$currPos;
641
+ s1 = peg$f0();
642
+ if (s1) {
643
+ s1 = undefined;
644
+ } else {
645
+ s1 = peg$FAILED;
646
+ }
647
+ if (s1 !== peg$FAILED) {
648
+ s2 = peg$parseskipEmptyLines();
649
+ s3 = peg$parselinks();
503
650
  if (s3 !== peg$FAILED) {
504
- peg$savedPos = s0;
505
- s0 = peg$f0(s2);
651
+ s4 = peg$parse_();
652
+ s5 = peg$parseeof();
653
+ if (s5 !== peg$FAILED) {
654
+ peg$savedPos = s0;
655
+ s0 = peg$f1(s3);
656
+ } else {
657
+ peg$currPos = s0;
658
+ s0 = peg$FAILED;
659
+ }
506
660
  } else {
507
661
  peg$currPos = s0;
508
662
  s0 = peg$FAILED;
@@ -513,11 +667,23 @@ function peg$parse(input, options) {
513
667
  }
514
668
  if (s0 === peg$FAILED) {
515
669
  s0 = peg$currPos;
516
- s1 = peg$parse_();
517
- s2 = peg$parseeof();
518
- if (s2 !== peg$FAILED) {
519
- peg$savedPos = s0;
520
- s0 = peg$f1();
670
+ peg$savedPos = peg$currPos;
671
+ s1 = peg$f2();
672
+ if (s1) {
673
+ s1 = undefined;
674
+ } else {
675
+ s1 = peg$FAILED;
676
+ }
677
+ if (s1 !== peg$FAILED) {
678
+ s2 = peg$parse_();
679
+ s3 = peg$parseeof();
680
+ if (s3 !== peg$FAILED) {
681
+ peg$savedPos = s0;
682
+ s0 = peg$f3();
683
+ } else {
684
+ peg$currPos = s0;
685
+ s0 = peg$FAILED;
686
+ }
521
687
  } else {
522
688
  peg$currPos = s0;
523
689
  s0 = peg$FAILED;
@@ -525,6 +691,92 @@ function peg$parse(input, options) {
525
691
  }
526
692
  return s0;
527
693
  }
694
+ function peg$parseskipEmptyLines() {
695
+ let s0, s1, s2, s3;
696
+ s0 = [];
697
+ s1 = peg$currPos;
698
+ s2 = [];
699
+ s3 = input.charAt(peg$currPos);
700
+ if (peg$r0.test(s3)) {
701
+ peg$currPos++;
702
+ } else {
703
+ s3 = peg$FAILED;
704
+ if (peg$silentFails === 0) {
705
+ peg$fail(peg$e0);
706
+ }
707
+ }
708
+ while (s3 !== peg$FAILED) {
709
+ s2.push(s3);
710
+ s3 = input.charAt(peg$currPos);
711
+ if (peg$r0.test(s3)) {
712
+ peg$currPos++;
713
+ } else {
714
+ s3 = peg$FAILED;
715
+ if (peg$silentFails === 0) {
716
+ peg$fail(peg$e0);
717
+ }
718
+ }
719
+ }
720
+ s3 = input.charAt(peg$currPos);
721
+ if (peg$r1.test(s3)) {
722
+ peg$currPos++;
723
+ } else {
724
+ s3 = peg$FAILED;
725
+ if (peg$silentFails === 0) {
726
+ peg$fail(peg$e1);
727
+ }
728
+ }
729
+ if (s3 !== peg$FAILED) {
730
+ s2 = [s2, s3];
731
+ s1 = s2;
732
+ } else {
733
+ peg$currPos = s1;
734
+ s1 = peg$FAILED;
735
+ }
736
+ while (s1 !== peg$FAILED) {
737
+ s0.push(s1);
738
+ s1 = peg$currPos;
739
+ s2 = [];
740
+ s3 = input.charAt(peg$currPos);
741
+ if (peg$r0.test(s3)) {
742
+ peg$currPos++;
743
+ } else {
744
+ s3 = peg$FAILED;
745
+ if (peg$silentFails === 0) {
746
+ peg$fail(peg$e0);
747
+ }
748
+ }
749
+ while (s3 !== peg$FAILED) {
750
+ s2.push(s3);
751
+ s3 = input.charAt(peg$currPos);
752
+ if (peg$r0.test(s3)) {
753
+ peg$currPos++;
754
+ } else {
755
+ s3 = peg$FAILED;
756
+ if (peg$silentFails === 0) {
757
+ peg$fail(peg$e0);
758
+ }
759
+ }
760
+ }
761
+ s3 = input.charAt(peg$currPos);
762
+ if (peg$r1.test(s3)) {
763
+ peg$currPos++;
764
+ } else {
765
+ s3 = peg$FAILED;
766
+ if (peg$silentFails === 0) {
767
+ peg$fail(peg$e1);
768
+ }
769
+ }
770
+ if (s3 !== peg$FAILED) {
771
+ s2 = [s2, s3];
772
+ s1 = s2;
773
+ } else {
774
+ peg$currPos = s1;
775
+ s1 = peg$FAILED;
776
+ }
777
+ }
778
+ return s0;
779
+ }
528
780
  function peg$parselinks() {
529
781
  let s0, s1, s2, s3;
530
782
  s0 = peg$currPos;
@@ -537,7 +789,7 @@ function peg$parse(input, options) {
537
789
  s3 = peg$parseline();
538
790
  }
539
791
  peg$savedPos = s0;
540
- s0 = peg$f2(s1, s2);
792
+ s0 = peg$f4(s1, s2);
541
793
  } else {
542
794
  peg$currPos = s0;
543
795
  s0 = peg$FAILED;
@@ -545,14 +797,17 @@ function peg$parse(input, options) {
545
797
  return s0;
546
798
  }
547
799
  function peg$parsefirstLine() {
548
- let s0, s1;
800
+ let s0, s1, s2;
549
801
  s0 = peg$currPos;
550
- s1 = peg$parseelement();
551
- if (s1 !== peg$FAILED) {
802
+ s1 = peg$parseSET_BASE_INDENTATION();
803
+ s2 = peg$parseelement();
804
+ if (s2 !== peg$FAILED) {
552
805
  peg$savedPos = s0;
553
- s1 = peg$f3(s1);
806
+ s0 = peg$f5(s2);
807
+ } else {
808
+ peg$currPos = s0;
809
+ s0 = peg$FAILED;
554
810
  }
555
- s0 = s1;
556
811
  return s0;
557
812
  }
558
813
  function peg$parseline() {
@@ -563,7 +818,7 @@ function peg$parse(input, options) {
563
818
  s2 = peg$parseelement();
564
819
  if (s2 !== peg$FAILED) {
565
820
  peg$savedPos = s0;
566
- s0 = peg$f4(s2);
821
+ s0 = peg$f6(s2);
567
822
  } else {
568
823
  peg$currPos = s0;
569
824
  s0 = peg$FAILED;
@@ -584,7 +839,7 @@ function peg$parse(input, options) {
584
839
  s3 = peg$parselinks();
585
840
  if (s3 !== peg$FAILED) {
586
841
  peg$savedPos = s0;
587
- s0 = peg$f5(s1, s3);
842
+ s0 = peg$f7(s1, s3);
588
843
  } else {
589
844
  peg$currPos = s0;
590
845
  s0 = peg$FAILED;
@@ -602,7 +857,7 @@ function peg$parse(input, options) {
602
857
  s1 = peg$parseanyLink();
603
858
  if (s1 !== peg$FAILED) {
604
859
  peg$savedPos = s0;
605
- s1 = peg$f6(s1);
860
+ s1 = peg$f8(s1);
606
861
  }
607
862
  s0 = s1;
608
863
  }
@@ -614,7 +869,7 @@ function peg$parse(input, options) {
614
869
  s1 = peg$parsemultiLineAnyLink();
615
870
  if (s1 !== peg$FAILED) {
616
871
  peg$savedPos = s0;
617
- s1 = peg$f7(s1);
872
+ s1 = peg$f9(s1);
618
873
  }
619
874
  s0 = s1;
620
875
  if (s0 === peg$FAILED) {
@@ -622,7 +877,7 @@ function peg$parse(input, options) {
622
877
  s1 = peg$parsereference();
623
878
  if (s1 !== peg$FAILED) {
624
879
  peg$savedPos = s0;
625
- s1 = peg$f8(s1);
880
+ s1 = peg$f10(s1);
626
881
  }
627
882
  s0 = s1;
628
883
  }
@@ -636,7 +891,7 @@ function peg$parse(input, options) {
636
891
  s2 = peg$parseeol();
637
892
  if (s2 !== peg$FAILED) {
638
893
  peg$savedPos = s0;
639
- s0 = peg$f9(s1);
894
+ s0 = peg$f11(s1);
640
895
  } else {
641
896
  peg$currPos = s0;
642
897
  s0 = peg$FAILED;
@@ -650,7 +905,7 @@ function peg$parse(input, options) {
650
905
  s1 = peg$parseindentedIdLink();
651
906
  if (s1 !== peg$FAILED) {
652
907
  peg$savedPos = s0;
653
- s1 = peg$f10(s1);
908
+ s1 = peg$f12(s1);
654
909
  }
655
910
  s0 = s1;
656
911
  if (s0 === peg$FAILED) {
@@ -658,7 +913,7 @@ function peg$parse(input, options) {
658
913
  s1 = peg$parsesingleLineAnyLink();
659
914
  if (s1 !== peg$FAILED) {
660
915
  peg$savedPos = s0;
661
- s1 = peg$f11(s1);
916
+ s1 = peg$f13(s1);
662
917
  }
663
918
  s0 = s1;
664
919
  }
@@ -681,7 +936,7 @@ function peg$parse(input, options) {
681
936
  s2 = peg$parseeol();
682
937
  if (s2 !== peg$FAILED) {
683
938
  peg$savedPos = s0;
684
- s0 = peg$f12(s1);
939
+ s0 = peg$f14(s1);
685
940
  } else {
686
941
  peg$currPos = s0;
687
942
  s0 = peg$FAILED;
@@ -697,7 +952,7 @@ function peg$parse(input, options) {
697
952
  s2 = peg$parseeol();
698
953
  if (s2 !== peg$FAILED) {
699
954
  peg$savedPos = s0;
700
- s0 = peg$f13(s1);
955
+ s0 = peg$f15(s1);
701
956
  } else {
702
957
  peg$currPos = s0;
703
958
  s0 = peg$FAILED;
@@ -716,7 +971,7 @@ function peg$parse(input, options) {
716
971
  if (s1 !== peg$FAILED) {
717
972
  s2 = peg$parse_();
718
973
  peg$savedPos = s0;
719
- s0 = peg$f14(s1);
974
+ s0 = peg$f16(s1);
720
975
  } else {
721
976
  peg$currPos = s0;
722
977
  s0 = peg$FAILED;
@@ -734,7 +989,7 @@ function peg$parse(input, options) {
734
989
  s3 = peg$parsemultiLineValueAndWhitespace();
735
990
  }
736
991
  peg$savedPos = s0;
737
- s0 = peg$f15(s2);
992
+ s0 = peg$f17(s2);
738
993
  return s0;
739
994
  }
740
995
  function peg$parsesingleLineValueAndWhitespace() {
@@ -744,7 +999,7 @@ function peg$parse(input, options) {
744
999
  s2 = peg$parsereferenceOrLink();
745
1000
  if (s2 !== peg$FAILED) {
746
1001
  peg$savedPos = s0;
747
- s0 = peg$f16(s2);
1002
+ s0 = peg$f18(s2);
748
1003
  } else {
749
1004
  peg$currPos = s0;
750
1005
  s0 = peg$FAILED;
@@ -766,7 +1021,7 @@ function peg$parse(input, options) {
766
1021
  }
767
1022
  if (s1 !== peg$FAILED) {
768
1023
  peg$savedPos = s0;
769
- s1 = peg$f17(s1);
1024
+ s1 = peg$f19(s1);
770
1025
  }
771
1026
  s0 = s1;
772
1027
  return s0;
@@ -784,14 +1039,14 @@ function peg$parse(input, options) {
784
1039
  } else {
785
1040
  s4 = peg$FAILED;
786
1041
  if (peg$silentFails === 0) {
787
- peg$fail(peg$e0);
1042
+ peg$fail(peg$e2);
788
1043
  }
789
1044
  }
790
1045
  if (s4 !== peg$FAILED) {
791
1046
  s5 = peg$parsesingleLineValues();
792
1047
  if (s5 !== peg$FAILED) {
793
1048
  peg$savedPos = s0;
794
- s0 = peg$f18(s2, s5);
1049
+ s0 = peg$f20(s2, s5);
795
1050
  } else {
796
1051
  peg$currPos = s0;
797
1052
  s0 = peg$FAILED;
@@ -815,7 +1070,7 @@ function peg$parse(input, options) {
815
1070
  } else {
816
1071
  s1 = peg$FAILED;
817
1072
  if (peg$silentFails === 0) {
818
- peg$fail(peg$e1);
1073
+ peg$fail(peg$e3);
819
1074
  }
820
1075
  }
821
1076
  if (s1 !== peg$FAILED) {
@@ -829,7 +1084,7 @@ function peg$parse(input, options) {
829
1084
  } else {
830
1085
  s5 = peg$FAILED;
831
1086
  if (peg$silentFails === 0) {
832
- peg$fail(peg$e0);
1087
+ peg$fail(peg$e2);
833
1088
  }
834
1089
  }
835
1090
  if (s5 !== peg$FAILED) {
@@ -841,12 +1096,12 @@ function peg$parse(input, options) {
841
1096
  } else {
842
1097
  s8 = peg$FAILED;
843
1098
  if (peg$silentFails === 0) {
844
- peg$fail(peg$e2);
1099
+ peg$fail(peg$e4);
845
1100
  }
846
1101
  }
847
1102
  if (s8 !== peg$FAILED) {
848
1103
  peg$savedPos = s0;
849
- s0 = peg$f19(s3, s6);
1104
+ s0 = peg$f21(s3, s6);
850
1105
  } else {
851
1106
  peg$currPos = s0;
852
1107
  s0 = peg$FAILED;
@@ -871,7 +1126,7 @@ function peg$parse(input, options) {
871
1126
  s1 = peg$parsesingleLineValues();
872
1127
  if (s1 !== peg$FAILED) {
873
1128
  peg$savedPos = s0;
874
- s1 = peg$f20(s1);
1129
+ s1 = peg$f22(s1);
875
1130
  }
876
1131
  s0 = s1;
877
1132
  return s0;
@@ -885,7 +1140,7 @@ function peg$parse(input, options) {
885
1140
  } else {
886
1141
  s1 = peg$FAILED;
887
1142
  if (peg$silentFails === 0) {
888
- peg$fail(peg$e1);
1143
+ peg$fail(peg$e3);
889
1144
  }
890
1145
  }
891
1146
  if (s1 !== peg$FAILED) {
@@ -897,12 +1152,12 @@ function peg$parse(input, options) {
897
1152
  } else {
898
1153
  s4 = peg$FAILED;
899
1154
  if (peg$silentFails === 0) {
900
- peg$fail(peg$e2);
1155
+ peg$fail(peg$e4);
901
1156
  }
902
1157
  }
903
1158
  if (s4 !== peg$FAILED) {
904
1159
  peg$savedPos = s0;
905
- s0 = peg$f21(s2);
1160
+ s0 = peg$f23(s2);
906
1161
  } else {
907
1162
  peg$currPos = s0;
908
1163
  s0 = peg$FAILED;
@@ -925,14 +1180,14 @@ function peg$parse(input, options) {
925
1180
  } else {
926
1181
  s3 = peg$FAILED;
927
1182
  if (peg$silentFails === 0) {
928
- peg$fail(peg$e0);
1183
+ peg$fail(peg$e2);
929
1184
  }
930
1185
  }
931
1186
  if (s3 !== peg$FAILED) {
932
1187
  s4 = peg$parseeol();
933
1188
  if (s4 !== peg$FAILED) {
934
1189
  peg$savedPos = s0;
935
- s0 = peg$f22(s1);
1190
+ s0 = peg$f24(s1);
936
1191
  } else {
937
1192
  peg$currPos = s0;
938
1193
  s0 = peg$FAILED;
@@ -973,7 +1228,7 @@ function peg$parse(input, options) {
973
1228
  }
974
1229
  if (s1 !== peg$FAILED) {
975
1230
  peg$savedPos = s0;
976
- s1 = peg$f23(s1);
1231
+ s1 = peg$f25(s1);
977
1232
  }
978
1233
  s0 = s1;
979
1234
  return s0;
@@ -987,30 +1242,30 @@ function peg$parse(input, options) {
987
1242
  } else {
988
1243
  s1 = peg$FAILED;
989
1244
  if (peg$silentFails === 0) {
990
- peg$fail(peg$e3);
1245
+ peg$fail(peg$e5);
991
1246
  }
992
1247
  }
993
1248
  if (s1 !== peg$FAILED) {
994
1249
  s2 = [];
995
1250
  s3 = input.charAt(peg$currPos);
996
- if (peg$r0.test(s3)) {
1251
+ if (peg$r2.test(s3)) {
997
1252
  peg$currPos++;
998
1253
  } else {
999
1254
  s3 = peg$FAILED;
1000
1255
  if (peg$silentFails === 0) {
1001
- peg$fail(peg$e4);
1256
+ peg$fail(peg$e6);
1002
1257
  }
1003
1258
  }
1004
1259
  if (s3 !== peg$FAILED) {
1005
1260
  while (s3 !== peg$FAILED) {
1006
1261
  s2.push(s3);
1007
1262
  s3 = input.charAt(peg$currPos);
1008
- if (peg$r0.test(s3)) {
1263
+ if (peg$r2.test(s3)) {
1009
1264
  peg$currPos++;
1010
1265
  } else {
1011
1266
  s3 = peg$FAILED;
1012
1267
  if (peg$silentFails === 0) {
1013
- peg$fail(peg$e4);
1268
+ peg$fail(peg$e6);
1014
1269
  }
1015
1270
  }
1016
1271
  }
@@ -1024,12 +1279,12 @@ function peg$parse(input, options) {
1024
1279
  } else {
1025
1280
  s3 = peg$FAILED;
1026
1281
  if (peg$silentFails === 0) {
1027
- peg$fail(peg$e3);
1282
+ peg$fail(peg$e5);
1028
1283
  }
1029
1284
  }
1030
1285
  if (s3 !== peg$FAILED) {
1031
1286
  peg$savedPos = s0;
1032
- s0 = peg$f24(s2);
1287
+ s0 = peg$f26(s2);
1033
1288
  } else {
1034
1289
  peg$currPos = s0;
1035
1290
  s0 = peg$FAILED;
@@ -1053,30 +1308,30 @@ function peg$parse(input, options) {
1053
1308
  } else {
1054
1309
  s1 = peg$FAILED;
1055
1310
  if (peg$silentFails === 0) {
1056
- peg$fail(peg$e5);
1311
+ peg$fail(peg$e7);
1057
1312
  }
1058
1313
  }
1059
1314
  if (s1 !== peg$FAILED) {
1060
1315
  s2 = [];
1061
1316
  s3 = input.charAt(peg$currPos);
1062
- if (peg$r1.test(s3)) {
1317
+ if (peg$r3.test(s3)) {
1063
1318
  peg$currPos++;
1064
1319
  } else {
1065
1320
  s3 = peg$FAILED;
1066
1321
  if (peg$silentFails === 0) {
1067
- peg$fail(peg$e6);
1322
+ peg$fail(peg$e8);
1068
1323
  }
1069
1324
  }
1070
1325
  if (s3 !== peg$FAILED) {
1071
1326
  while (s3 !== peg$FAILED) {
1072
1327
  s2.push(s3);
1073
1328
  s3 = input.charAt(peg$currPos);
1074
- if (peg$r1.test(s3)) {
1329
+ if (peg$r3.test(s3)) {
1075
1330
  peg$currPos++;
1076
1331
  } else {
1077
1332
  s3 = peg$FAILED;
1078
1333
  if (peg$silentFails === 0) {
1079
- peg$fail(peg$e6);
1334
+ peg$fail(peg$e8);
1080
1335
  }
1081
1336
  }
1082
1337
  }
@@ -1090,12 +1345,12 @@ function peg$parse(input, options) {
1090
1345
  } else {
1091
1346
  s3 = peg$FAILED;
1092
1347
  if (peg$silentFails === 0) {
1093
- peg$fail(peg$e5);
1348
+ peg$fail(peg$e7);
1094
1349
  }
1095
1350
  }
1096
1351
  if (s3 !== peg$FAILED) {
1097
1352
  peg$savedPos = s0;
1098
- s0 = peg$f25(s2);
1353
+ s0 = peg$f27(s2);
1099
1354
  } else {
1100
1355
  peg$currPos = s0;
1101
1356
  s0 = peg$FAILED;
@@ -1110,6 +1365,36 @@ function peg$parse(input, options) {
1110
1365
  }
1111
1366
  return s0;
1112
1367
  }
1368
+ function peg$parseSET_BASE_INDENTATION() {
1369
+ let s0, s1, s2;
1370
+ s0 = peg$currPos;
1371
+ s1 = [];
1372
+ if (input.charCodeAt(peg$currPos) === 32) {
1373
+ s2 = peg$c5;
1374
+ peg$currPos++;
1375
+ } else {
1376
+ s2 = peg$FAILED;
1377
+ if (peg$silentFails === 0) {
1378
+ peg$fail(peg$e9);
1379
+ }
1380
+ }
1381
+ while (s2 !== peg$FAILED) {
1382
+ s1.push(s2);
1383
+ if (input.charCodeAt(peg$currPos) === 32) {
1384
+ s2 = peg$c5;
1385
+ peg$currPos++;
1386
+ } else {
1387
+ s2 = peg$FAILED;
1388
+ if (peg$silentFails === 0) {
1389
+ peg$fail(peg$e9);
1390
+ }
1391
+ }
1392
+ }
1393
+ peg$savedPos = s0;
1394
+ s1 = peg$f28(s1);
1395
+ s0 = s1;
1396
+ return s0;
1397
+ }
1113
1398
  function peg$parsePUSH_INDENTATION() {
1114
1399
  let s0, s1, s2;
1115
1400
  s0 = peg$currPos;
@@ -1120,7 +1405,7 @@ function peg$parse(input, options) {
1120
1405
  } else {
1121
1406
  s2 = peg$FAILED;
1122
1407
  if (peg$silentFails === 0) {
1123
- peg$fail(peg$e7);
1408
+ peg$fail(peg$e9);
1124
1409
  }
1125
1410
  }
1126
1411
  while (s2 !== peg$FAILED) {
@@ -1131,12 +1416,12 @@ function peg$parse(input, options) {
1131
1416
  } else {
1132
1417
  s2 = peg$FAILED;
1133
1418
  if (peg$silentFails === 0) {
1134
- peg$fail(peg$e7);
1419
+ peg$fail(peg$e9);
1135
1420
  }
1136
1421
  }
1137
1422
  }
1138
1423
  peg$savedPos = peg$currPos;
1139
- s2 = peg$f26(s1);
1424
+ s2 = peg$f29(s1);
1140
1425
  if (s2) {
1141
1426
  s2 = undefined;
1142
1427
  } else {
@@ -1144,7 +1429,7 @@ function peg$parse(input, options) {
1144
1429
  }
1145
1430
  if (s2 !== peg$FAILED) {
1146
1431
  peg$savedPos = s0;
1147
- s0 = peg$f27(s1);
1432
+ s0 = peg$f30(s1);
1148
1433
  } else {
1149
1434
  peg$currPos = s0;
1150
1435
  s0 = peg$FAILED;
@@ -1161,7 +1446,7 @@ function peg$parse(input, options) {
1161
1446
  } else {
1162
1447
  s2 = peg$FAILED;
1163
1448
  if (peg$silentFails === 0) {
1164
- peg$fail(peg$e7);
1449
+ peg$fail(peg$e9);
1165
1450
  }
1166
1451
  }
1167
1452
  while (s2 !== peg$FAILED) {
@@ -1172,12 +1457,12 @@ function peg$parse(input, options) {
1172
1457
  } else {
1173
1458
  s2 = peg$FAILED;
1174
1459
  if (peg$silentFails === 0) {
1175
- peg$fail(peg$e7);
1460
+ peg$fail(peg$e9);
1176
1461
  }
1177
1462
  }
1178
1463
  }
1179
1464
  peg$savedPos = peg$currPos;
1180
- s2 = peg$f28(s1);
1465
+ s2 = peg$f31(s1);
1181
1466
  if (s2) {
1182
1467
  s2 = undefined;
1183
1468
  } else {
@@ -1198,24 +1483,24 @@ function peg$parse(input, options) {
1198
1483
  s1 = peg$parse__();
1199
1484
  s2 = [];
1200
1485
  s3 = input.charAt(peg$currPos);
1201
- if (peg$r2.test(s3)) {
1486
+ if (peg$r1.test(s3)) {
1202
1487
  peg$currPos++;
1203
1488
  } else {
1204
1489
  s3 = peg$FAILED;
1205
1490
  if (peg$silentFails === 0) {
1206
- peg$fail(peg$e8);
1491
+ peg$fail(peg$e1);
1207
1492
  }
1208
1493
  }
1209
1494
  if (s3 !== peg$FAILED) {
1210
1495
  while (s3 !== peg$FAILED) {
1211
1496
  s2.push(s3);
1212
1497
  s3 = input.charAt(peg$currPos);
1213
- if (peg$r2.test(s3)) {
1498
+ if (peg$r1.test(s3)) {
1214
1499
  peg$currPos++;
1215
1500
  } else {
1216
1501
  s3 = peg$FAILED;
1217
1502
  if (peg$silentFails === 0) {
1218
- peg$fail(peg$e8);
1503
+ peg$fail(peg$e1);
1219
1504
  }
1220
1505
  }
1221
1506
  }
@@ -1244,7 +1529,7 @@ function peg$parse(input, options) {
1244
1529
  } else {
1245
1530
  s1 = peg$FAILED;
1246
1531
  if (peg$silentFails === 0) {
1247
- peg$fail(peg$e9);
1532
+ peg$fail(peg$e10);
1248
1533
  }
1249
1534
  }
1250
1535
  peg$silentFails--;
@@ -1260,23 +1545,23 @@ function peg$parse(input, options) {
1260
1545
  let s0, s1;
1261
1546
  s0 = [];
1262
1547
  s1 = input.charAt(peg$currPos);
1263
- if (peg$r3.test(s1)) {
1548
+ if (peg$r0.test(s1)) {
1264
1549
  peg$currPos++;
1265
1550
  } else {
1266
1551
  s1 = peg$FAILED;
1267
1552
  if (peg$silentFails === 0) {
1268
- peg$fail(peg$e10);
1553
+ peg$fail(peg$e0);
1269
1554
  }
1270
1555
  }
1271
1556
  while (s1 !== peg$FAILED) {
1272
1557
  s0.push(s1);
1273
1558
  s1 = input.charAt(peg$currPos);
1274
- if (peg$r3.test(s1)) {
1559
+ if (peg$r0.test(s1)) {
1275
1560
  peg$currPos++;
1276
1561
  } else {
1277
1562
  s1 = peg$FAILED;
1278
1563
  if (peg$silentFails === 0) {
1279
- peg$fail(peg$e10);
1564
+ peg$fail(peg$e0);
1280
1565
  }
1281
1566
  }
1282
1567
  }
@@ -1319,8 +1604,21 @@ function peg$parse(input, options) {
1319
1604
  return s0;
1320
1605
  }
1321
1606
  let indentationStack = [0];
1607
+ let baseIndentation = null;
1608
+ function setBaseIndentation(spaces) {
1609
+ if (baseIndentation === null) {
1610
+ baseIndentation = spaces.length;
1611
+ }
1612
+ }
1613
+ function normalizeIndentation(spaces) {
1614
+ if (baseIndentation === null) {
1615
+ return spaces.length;
1616
+ }
1617
+ return Math.max(0, spaces.length - baseIndentation);
1618
+ }
1322
1619
  function pushIndentation(spaces) {
1323
- indentationStack.push(spaces.length);
1620
+ const normalized = normalizeIndentation(spaces);
1621
+ indentationStack.push(normalized);
1324
1622
  }
1325
1623
  function popIndentation() {
1326
1624
  if (indentationStack.length > 1) {
@@ -1328,7 +1626,8 @@ function peg$parse(input, options) {
1328
1626
  }
1329
1627
  }
1330
1628
  function checkIndentation(spaces) {
1331
- return spaces.length >= indentationStack[indentationStack.length - 1];
1629
+ const normalized = normalizeIndentation(spaces);
1630
+ return normalized >= indentationStack[indentationStack.length - 1];
1332
1631
  }
1333
1632
  function getCurrentIndentation() {
1334
1633
  return indentationStack[indentationStack.length - 1];
@@ -1361,27 +1660,39 @@ function peg$parse(input, options) {
1361
1660
 
1362
1661
  // src/Parser.js
1363
1662
  class Parser {
1364
- constructor() {}
1663
+ constructor(options = {}) {
1664
+ this.maxInputSize = options.maxInputSize || 10 * 1024 * 1024;
1665
+ this.maxDepth = options.maxDepth || 1000;
1666
+ }
1365
1667
  parse(input) {
1668
+ if (typeof input !== "string") {
1669
+ throw new TypeError("Input must be a string");
1670
+ }
1671
+ if (input.length > this.maxInputSize) {
1672
+ throw new Error(`Input size exceeds maximum allowed size of ${this.maxInputSize} bytes`);
1673
+ }
1366
1674
  try {
1367
1675
  const rawResult = peg$parse(input);
1368
1676
  return this.transformResult(rawResult);
1369
1677
  } catch (error) {
1370
- throw new Error(`Parse error: ${error.message}`);
1678
+ const parseError = new Error(`Parse error: ${error.message}`);
1679
+ parseError.cause = error;
1680
+ parseError.location = error.location;
1681
+ throw parseError;
1371
1682
  }
1372
1683
  }
1373
1684
  transformResult(rawResult) {
1374
1685
  const links = [];
1375
1686
  const items = Array.isArray(rawResult) ? rawResult : [rawResult];
1376
1687
  for (const item of items) {
1377
- if (item) {
1688
+ if (item !== null && item !== undefined) {
1378
1689
  this.collectLinks(item, [], links);
1379
1690
  }
1380
1691
  }
1381
1692
  return links;
1382
1693
  }
1383
1694
  collectLinks(item, parentPath, result) {
1384
- if (!item)
1695
+ if (item === null || item === undefined)
1385
1696
  return;
1386
1697
  if (item.children && item.children.length > 0) {
1387
1698
  if (item.id && (!item.values || item.values.length === 0)) {
@@ -1438,7 +1749,7 @@ class Parser {
1438
1749
  return combined;
1439
1750
  }
1440
1751
  transformLink(item) {
1441
- if (!item)
1752
+ if (item === null || item === undefined)
1442
1753
  return null;
1443
1754
  if (item instanceof Link) {
1444
1755
  return item;
@@ -1454,9 +1765,39 @@ class Parser {
1454
1765
  return new Link(item.id || null, []);
1455
1766
  }
1456
1767
  }
1768
+ // src/FormatOptions.js
1769
+ class FormatOptions {
1770
+ constructor(options = {}) {
1771
+ this.lessParentheses = options.lessParentheses ?? false;
1772
+ this.maxLineLength = options.maxLineLength ?? 80;
1773
+ this.indentLongLines = options.indentLongLines ?? false;
1774
+ this.maxInlineRefs = options.maxInlineRefs ?? null;
1775
+ this.groupConsecutive = options.groupConsecutive ?? false;
1776
+ this.indentString = options.indentString ?? " ";
1777
+ this.preferInline = options.preferInline ?? true;
1778
+ }
1779
+ shouldIndentByLength(line) {
1780
+ if (!this.indentLongLines) {
1781
+ return false;
1782
+ }
1783
+ return line.length > this.maxLineLength;
1784
+ }
1785
+ shouldIndentByRefCount(refCount) {
1786
+ if (this.maxInlineRefs === null) {
1787
+ return false;
1788
+ }
1789
+ return refCount > this.maxInlineRefs;
1790
+ }
1791
+ }
1792
+
1793
+ // src/FormatConfig.js
1794
+ class FormatConfig extends FormatOptions {
1795
+ }
1457
1796
  export {
1458
1797
  formatLinks,
1459
1798
  Parser,
1460
1799
  LinksGroup,
1461
- Link
1800
+ Link,
1801
+ FormatOptions,
1802
+ FormatConfig
1462
1803
  };