links-notation 0.23.0 → 0.24.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
@@ -1,3 +1,49 @@
1
+ // src/ReferenceLiteral.js
2
+ function hasReferenceLiteralPrefix(text, start = 0) {
3
+ return /^~[0-9]+\{/.test(text.slice(start));
4
+ }
5
+ function encodeReferenceLiteral(text) {
6
+ if (typeof text !== "string" || !text.isWellFormed()) {
7
+ throw new TypeError("Reference must be well-formed Unicode text");
8
+ }
9
+ const hex = Array.from(new TextEncoder().encode(text), (byte) => byte.toString(16).padStart(2, "0")).join("");
10
+ return `~1{${hex}}`;
11
+ }
12
+ function decodeReferenceLiteral(literal) {
13
+ const match = /^~([0-9]+)\{([0-9a-fA-F]*)\}$/.exec(literal);
14
+ if (!match || match[0] !== literal || match[1] !== "1" || match[2].length % 2 !== 0) {
15
+ throw new TypeError("Invalid or unsupported reference literal (expected ~1{UTF-8 hex})");
16
+ }
17
+ return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(Uint8Array.from(match[2].match(/../g) ?? [], (hex) => parseInt(hex, 16)));
18
+ }
19
+ function formatReference(text) {
20
+ if (typeof text !== "string" || !text.isWellFormed()) {
21
+ throw new TypeError("Reference must be well-formed Unicode text");
22
+ }
23
+ if (!text || /[\u0000-\u001f\u007f]/u.test(text)) {
24
+ return encodeReferenceLiteral(text);
25
+ }
26
+ if (!text.startsWith("#") && !/^~[0-9]+\{/.test(text) && !/[\p{White_Space}\ufeff():"'`]/u.test(text)) {
27
+ return text;
28
+ }
29
+ let choice = null;
30
+ for (const quote of ["'", '"', "`"]) {
31
+ if (text.startsWith(quote))
32
+ continue;
33
+ let longest = 0;
34
+ let run = 0;
35
+ for (const character of text) {
36
+ run = character === quote ? run + 1 : 0;
37
+ longest = Math.max(longest, run);
38
+ }
39
+ const count = longest + 1 | 1;
40
+ if (choice === null || count < choice.count)
41
+ choice = { quote, count };
42
+ }
43
+ const delimiter = choice.quote.repeat(choice.count);
44
+ return delimiter + text + delimiter;
45
+ }
46
+
1
47
  // src/Link.js
2
48
  class Link {
3
49
  constructor(id = null, values = null) {
@@ -39,26 +85,7 @@ class Link {
39
85
  if (reference === null || reference === undefined) {
40
86
  return "";
41
87
  }
42
- if (reference === "") {
43
- return '""';
44
- }
45
- const hasSingleQuote = reference.includes("'");
46
- const hasDoubleQuote = reference.includes('"');
47
- const needsQuoting = reference.startsWith("#") || reference.includes(":") || reference.includes("(") || reference.includes(")") || reference.includes(" ") || reference.includes("\t") || reference.includes(`
48
- `) || reference.includes("\r") || hasDoubleQuote || hasSingleQuote;
49
- if (hasSingleQuote && hasDoubleQuote) {
50
- return `'${reference.replace(/'/g, "\\'")}'`;
51
- }
52
- if (hasDoubleQuote) {
53
- return `'${reference}'`;
54
- }
55
- if (hasSingleQuote) {
56
- return `"${reference}"`;
57
- }
58
- if (needsQuoting) {
59
- return `'${reference}'`;
60
- }
61
- return reference;
88
+ return formatReference(reference);
62
89
  }
63
90
  toLinkOrIdString() {
64
91
  if (!this.values || this.values.length === 0) {
@@ -631,14 +658,19 @@ function peg$parse(input, options) {
631
658
  const peg$c0 = "(";
632
659
  const peg$c1 = ")";
633
660
  const peg$c2 = ":";
634
- const peg$c3 = '"';
635
- const peg$c4 = "'";
636
- const peg$c5 = "`";
637
- const peg$c6 = " ";
661
+ const peg$c3 = "~";
662
+ const peg$c4 = "{";
663
+ const peg$c5 = "}";
664
+ const peg$c6 = '"';
665
+ const peg$c7 = "'";
666
+ const peg$c8 = "`";
667
+ const peg$c9 = " ";
638
668
  const peg$r0 = /^[ \t]/;
639
669
  const peg$r1 = /^[\r\n]/;
640
- const peg$r2 = /^[ \t\n\r]/;
641
- const peg$r3 = /^[^ \t\n\r(:)]/;
670
+ const peg$r2 = /^[0-9]/;
671
+ const peg$r3 = /^[^} \t\r\n()]/;
672
+ const peg$r4 = /^[ \t\n\r]/;
673
+ const peg$r5 = /^[^ \t\n\r(:)]/;
642
674
  const peg$e0 = peg$classExpectation([" ", "\t"], false, false, false);
643
675
  const peg$e1 = peg$classExpectation(["\r", `
644
676
  `], false, false, false);
@@ -646,13 +678,19 @@ function peg$parse(input, options) {
646
678
  const peg$e3 = peg$literalExpectation("(", false);
647
679
  const peg$e4 = peg$literalExpectation(")", false);
648
680
  const peg$e5 = peg$literalExpectation(":", false);
649
- const peg$e6 = peg$literalExpectation('"', false);
650
- const peg$e7 = peg$literalExpectation("'", false);
651
- const peg$e8 = peg$literalExpectation("`", false);
652
- const peg$e9 = peg$literalExpectation(" ", false);
653
- const peg$e10 = peg$classExpectation([" ", "\t", `
681
+ const peg$e6 = peg$literalExpectation("~", false);
682
+ const peg$e7 = peg$classExpectation([["0", "9"]], false, false, false);
683
+ const peg$e8 = peg$literalExpectation("{", false);
684
+ const peg$e9 = peg$classExpectation(["}", " ", "\t", "\r", `
685
+ `, "(", ")"], true, false, false);
686
+ const peg$e10 = peg$literalExpectation("}", false);
687
+ const peg$e11 = peg$literalExpectation('"', false);
688
+ const peg$e12 = peg$literalExpectation("'", false);
689
+ const peg$e13 = peg$literalExpectation("`", false);
690
+ const peg$e14 = peg$literalExpectation(" ", false);
691
+ const peg$e15 = peg$classExpectation([" ", "\t", `
654
692
  `, "\r"], false, false, false);
655
- const peg$e11 = peg$classExpectation([" ", "\t", `
693
+ const peg$e16 = peg$classExpectation([" ", "\t", `
656
694
  `, "\r", "(", ":", ")"], true, false, false);
657
695
  function peg$f0() {
658
696
  return resetState();
@@ -749,10 +787,19 @@ function peg$parse(input, options) {
749
787
  function peg$f28(id) {
750
788
  return { id, values: [] };
751
789
  }
752
- function peg$f29(chars) {
790
+ function peg$f29() {
791
+ return hasReferenceLiteralPrefix(input, offset());
792
+ }
793
+ function peg$f30(literal) {
794
+ return decodeReferenceLiteral(literal);
795
+ }
796
+ function peg$f31() {
797
+ return !hasReferenceLiteralPrefix(input, offset());
798
+ }
799
+ function peg$f32(chars) {
753
800
  return chars.join("");
754
801
  }
755
- function peg$f30() {
802
+ function peg$f33() {
756
803
  const pos = offset();
757
804
  const result = parseQuotedStringAt(input, pos, '"');
758
805
  if (result) {
@@ -762,19 +809,19 @@ function peg$parse(input, options) {
762
809
  }
763
810
  return false;
764
811
  }
765
- function peg$f31(chars) {
812
+ function peg$f34(chars) {
766
813
  return parsedValue;
767
814
  }
768
- function peg$f32(c, cs) {
815
+ function peg$f35(c, cs) {
769
816
  return [c].concat(cs).join("");
770
817
  }
771
- function peg$f33() {
818
+ function peg$f36() {
772
819
  return parsedLength > 1 && (parsedLength--, true);
773
820
  }
774
- function peg$f34(c) {
821
+ function peg$f37(c) {
775
822
  return c;
776
823
  }
777
- function peg$f35() {
824
+ function peg$f38() {
778
825
  const pos = offset();
779
826
  const result = parseQuotedStringAt(input, pos, "'");
780
827
  if (result) {
@@ -784,19 +831,19 @@ function peg$parse(input, options) {
784
831
  }
785
832
  return false;
786
833
  }
787
- function peg$f36(chars) {
834
+ function peg$f39(chars) {
788
835
  return parsedValue;
789
836
  }
790
- function peg$f37(c, cs) {
837
+ function peg$f40(c, cs) {
791
838
  return [c].concat(cs).join("");
792
839
  }
793
- function peg$f38() {
840
+ function peg$f41() {
794
841
  return parsedLength > 1 && (parsedLength--, true);
795
842
  }
796
- function peg$f39(c) {
843
+ function peg$f42(c) {
797
844
  return c;
798
845
  }
799
- function peg$f40() {
846
+ function peg$f43() {
800
847
  const pos = offset();
801
848
  const result = parseQuotedStringAt(input, pos, "`");
802
849
  if (result) {
@@ -806,34 +853,34 @@ function peg$parse(input, options) {
806
853
  }
807
854
  return false;
808
855
  }
809
- function peg$f41(chars) {
856
+ function peg$f44(chars) {
810
857
  return parsedValue;
811
858
  }
812
- function peg$f42(c, cs) {
859
+ function peg$f45(c, cs) {
813
860
  return [c].concat(cs).join("");
814
861
  }
815
- function peg$f43() {
862
+ function peg$f46() {
816
863
  return parsedLength > 1 && (parsedLength--, true);
817
864
  }
818
- function peg$f44(c) {
865
+ function peg$f47(c) {
819
866
  return c;
820
867
  }
821
- function peg$f45() {
868
+ function peg$f48() {
822
869
  return indentationStack.slice();
823
870
  }
824
- function peg$f46(spaces) {
871
+ function peg$f49(spaces) {
825
872
  setBaseIndentation(spaces);
826
873
  }
827
- function peg$f47(spaces) {
874
+ function peg$f50(spaces) {
828
875
  return normalizeIndentation(spaces) > getCurrentIndentation();
829
876
  }
830
- function peg$f48(spaces) {
877
+ function peg$f51(spaces) {
831
878
  pushIndentation(spaces);
832
879
  }
833
- function peg$f49(spaces) {
880
+ function peg$f52(spaces) {
834
881
  return checkIndentation(spaces);
835
882
  }
836
- function peg$f50() {
883
+ function peg$f53() {
837
884
  return isInsideNestedContext();
838
885
  }
839
886
  let peg$currPos = options.peg$currPos | 0;
@@ -1680,30 +1727,198 @@ function peg$parse(input, options) {
1680
1727
  }
1681
1728
  function peg$parsereference() {
1682
1729
  let s0;
1683
- s0 = peg$parsequotedReference();
1730
+ s0 = peg$parsereferenceLiteral();
1684
1731
  if (s0 === peg$FAILED) {
1685
- s0 = peg$parsesimpleReference();
1732
+ s0 = peg$parsequotedReference();
1733
+ if (s0 === peg$FAILED) {
1734
+ s0 = peg$parsesimpleReference();
1735
+ }
1686
1736
  }
1687
1737
  return s0;
1688
1738
  }
1689
- function peg$parsesimpleReference() {
1690
- let s0, s1, s2;
1739
+ function peg$parsereferenceLiteral() {
1740
+ let s0, s1, s2, s3, s4, s5, s6, s7, s8, s9;
1691
1741
  s0 = peg$currPos;
1692
- s1 = [];
1693
- s2 = peg$parsereferenceSymbol();
1742
+ s1 = peg$currPos;
1743
+ peg$silentFails++;
1744
+ if (input.charCodeAt(peg$currPos) === 126) {
1745
+ s2 = peg$c3;
1746
+ peg$currPos++;
1747
+ } else {
1748
+ s2 = peg$FAILED;
1749
+ if (peg$silentFails === 0) {
1750
+ peg$fail(peg$e6);
1751
+ }
1752
+ }
1753
+ peg$silentFails--;
1694
1754
  if (s2 !== peg$FAILED) {
1695
- while (s2 !== peg$FAILED) {
1696
- s1.push(s2);
1697
- s2 = peg$parsereferenceSymbol();
1755
+ peg$currPos = s1;
1756
+ s1 = undefined;
1757
+ } else {
1758
+ s1 = peg$FAILED;
1759
+ }
1760
+ if (s1 !== peg$FAILED) {
1761
+ peg$savedPos = peg$currPos;
1762
+ s2 = peg$f29();
1763
+ if (s2) {
1764
+ s2 = undefined;
1765
+ } else {
1766
+ s2 = peg$FAILED;
1767
+ }
1768
+ if (s2 !== peg$FAILED) {
1769
+ s3 = peg$currPos;
1770
+ s4 = peg$currPos;
1771
+ if (input.charCodeAt(peg$currPos) === 126) {
1772
+ s5 = peg$c3;
1773
+ peg$currPos++;
1774
+ } else {
1775
+ s5 = peg$FAILED;
1776
+ if (peg$silentFails === 0) {
1777
+ peg$fail(peg$e6);
1778
+ }
1779
+ }
1780
+ if (s5 !== peg$FAILED) {
1781
+ s6 = [];
1782
+ s7 = input.charAt(peg$currPos);
1783
+ if (peg$r2.test(s7)) {
1784
+ peg$currPos++;
1785
+ } else {
1786
+ s7 = peg$FAILED;
1787
+ if (peg$silentFails === 0) {
1788
+ peg$fail(peg$e7);
1789
+ }
1790
+ }
1791
+ if (s7 !== peg$FAILED) {
1792
+ while (s7 !== peg$FAILED) {
1793
+ s6.push(s7);
1794
+ s7 = input.charAt(peg$currPos);
1795
+ if (peg$r2.test(s7)) {
1796
+ peg$currPos++;
1797
+ } else {
1798
+ s7 = peg$FAILED;
1799
+ if (peg$silentFails === 0) {
1800
+ peg$fail(peg$e7);
1801
+ }
1802
+ }
1803
+ }
1804
+ } else {
1805
+ s6 = peg$FAILED;
1806
+ }
1807
+ if (s6 !== peg$FAILED) {
1808
+ if (input.charCodeAt(peg$currPos) === 123) {
1809
+ s7 = peg$c4;
1810
+ peg$currPos++;
1811
+ } else {
1812
+ s7 = peg$FAILED;
1813
+ if (peg$silentFails === 0) {
1814
+ peg$fail(peg$e8);
1815
+ }
1816
+ }
1817
+ if (s7 !== peg$FAILED) {
1818
+ s8 = [];
1819
+ s9 = input.charAt(peg$currPos);
1820
+ if (peg$r3.test(s9)) {
1821
+ peg$currPos++;
1822
+ } else {
1823
+ s9 = peg$FAILED;
1824
+ if (peg$silentFails === 0) {
1825
+ peg$fail(peg$e9);
1826
+ }
1827
+ }
1828
+ while (s9 !== peg$FAILED) {
1829
+ s8.push(s9);
1830
+ s9 = input.charAt(peg$currPos);
1831
+ if (peg$r3.test(s9)) {
1832
+ peg$currPos++;
1833
+ } else {
1834
+ s9 = peg$FAILED;
1835
+ if (peg$silentFails === 0) {
1836
+ peg$fail(peg$e9);
1837
+ }
1838
+ }
1839
+ }
1840
+ if (input.charCodeAt(peg$currPos) === 125) {
1841
+ s9 = peg$c5;
1842
+ peg$currPos++;
1843
+ } else {
1844
+ s9 = peg$FAILED;
1845
+ if (peg$silentFails === 0) {
1846
+ peg$fail(peg$e10);
1847
+ }
1848
+ }
1849
+ if (s9 !== peg$FAILED) {
1850
+ s5 = [s5, s6, s7, s8, s9];
1851
+ s4 = s5;
1852
+ } else {
1853
+ peg$currPos = s4;
1854
+ s4 = peg$FAILED;
1855
+ }
1856
+ } else {
1857
+ peg$currPos = s4;
1858
+ s4 = peg$FAILED;
1859
+ }
1860
+ } else {
1861
+ peg$currPos = s4;
1862
+ s4 = peg$FAILED;
1863
+ }
1864
+ } else {
1865
+ peg$currPos = s4;
1866
+ s4 = peg$FAILED;
1867
+ }
1868
+ if (s4 !== peg$FAILED) {
1869
+ s3 = input.substring(s3, peg$currPos);
1870
+ } else {
1871
+ s3 = s4;
1872
+ }
1873
+ if (s3 !== peg$FAILED) {
1874
+ peg$savedPos = s0;
1875
+ s0 = peg$f30(s3);
1876
+ } else {
1877
+ peg$currPos = s0;
1878
+ s0 = peg$FAILED;
1879
+ }
1880
+ } else {
1881
+ peg$currPos = s0;
1882
+ s0 = peg$FAILED;
1698
1883
  }
1884
+ } else {
1885
+ peg$currPos = s0;
1886
+ s0 = peg$FAILED;
1887
+ }
1888
+ return s0;
1889
+ }
1890
+ function peg$parsesimpleReference() {
1891
+ let s0, s1, s2, s3;
1892
+ s0 = peg$currPos;
1893
+ peg$savedPos = peg$currPos;
1894
+ s1 = peg$f31();
1895
+ if (s1) {
1896
+ s1 = undefined;
1699
1897
  } else {
1700
1898
  s1 = peg$FAILED;
1701
1899
  }
1702
1900
  if (s1 !== peg$FAILED) {
1703
- peg$savedPos = s0;
1704
- s1 = peg$f29(s1);
1901
+ s2 = [];
1902
+ s3 = peg$parsereferenceSymbol();
1903
+ if (s3 !== peg$FAILED) {
1904
+ while (s3 !== peg$FAILED) {
1905
+ s2.push(s3);
1906
+ s3 = peg$parsereferenceSymbol();
1907
+ }
1908
+ } else {
1909
+ s2 = peg$FAILED;
1910
+ }
1911
+ if (s2 !== peg$FAILED) {
1912
+ peg$savedPos = s0;
1913
+ s0 = peg$f32(s2);
1914
+ } else {
1915
+ peg$currPos = s0;
1916
+ s0 = peg$FAILED;
1917
+ }
1918
+ } else {
1919
+ peg$currPos = s0;
1920
+ s0 = peg$FAILED;
1705
1921
  }
1706
- s0 = s1;
1707
1922
  return s0;
1708
1923
  }
1709
1924
  function peg$parsequotedReference() {
@@ -1723,12 +1938,12 @@ function peg$parse(input, options) {
1723
1938
  s1 = peg$currPos;
1724
1939
  peg$silentFails++;
1725
1940
  if (input.charCodeAt(peg$currPos) === 34) {
1726
- s2 = peg$c3;
1941
+ s2 = peg$c6;
1727
1942
  peg$currPos++;
1728
1943
  } else {
1729
1944
  s2 = peg$FAILED;
1730
1945
  if (peg$silentFails === 0) {
1731
- peg$fail(peg$e6);
1946
+ peg$fail(peg$e11);
1732
1947
  }
1733
1948
  }
1734
1949
  peg$silentFails--;
@@ -1740,7 +1955,7 @@ function peg$parse(input, options) {
1740
1955
  }
1741
1956
  if (s1 !== peg$FAILED) {
1742
1957
  peg$savedPos = peg$currPos;
1743
- s2 = peg$f30();
1958
+ s2 = peg$f33();
1744
1959
  if (s2) {
1745
1960
  s2 = undefined;
1746
1961
  } else {
@@ -1750,7 +1965,7 @@ function peg$parse(input, options) {
1750
1965
  s3 = peg$parseconsumeDouble();
1751
1966
  if (s3 !== peg$FAILED) {
1752
1967
  peg$savedPos = s0;
1753
- s0 = peg$f31(s3);
1968
+ s0 = peg$f34(s3);
1754
1969
  } else {
1755
1970
  peg$currPos = s0;
1756
1971
  s0 = peg$FAILED;
@@ -1785,7 +2000,7 @@ function peg$parse(input, options) {
1785
2000
  s3 = peg$parseconsumeDoubleMore();
1786
2001
  }
1787
2002
  peg$savedPos = s0;
1788
- s0 = peg$f32(s1, s2);
2003
+ s0 = peg$f35(s1, s2);
1789
2004
  } else {
1790
2005
  peg$currPos = s0;
1791
2006
  s0 = peg$FAILED;
@@ -1796,7 +2011,7 @@ function peg$parse(input, options) {
1796
2011
  let s0, s1, s2;
1797
2012
  s0 = peg$currPos;
1798
2013
  peg$savedPos = peg$currPos;
1799
- s1 = peg$f33();
2014
+ s1 = peg$f36();
1800
2015
  if (s1) {
1801
2016
  s1 = undefined;
1802
2017
  } else {
@@ -1814,7 +2029,7 @@ function peg$parse(input, options) {
1814
2029
  }
1815
2030
  if (s2 !== peg$FAILED) {
1816
2031
  peg$savedPos = s0;
1817
- s0 = peg$f34(s2);
2032
+ s0 = peg$f37(s2);
1818
2033
  } else {
1819
2034
  peg$currPos = s0;
1820
2035
  s0 = peg$FAILED;
@@ -1831,12 +2046,12 @@ function peg$parse(input, options) {
1831
2046
  s1 = peg$currPos;
1832
2047
  peg$silentFails++;
1833
2048
  if (input.charCodeAt(peg$currPos) === 39) {
1834
- s2 = peg$c4;
2049
+ s2 = peg$c7;
1835
2050
  peg$currPos++;
1836
2051
  } else {
1837
2052
  s2 = peg$FAILED;
1838
2053
  if (peg$silentFails === 0) {
1839
- peg$fail(peg$e7);
2054
+ peg$fail(peg$e12);
1840
2055
  }
1841
2056
  }
1842
2057
  peg$silentFails--;
@@ -1848,7 +2063,7 @@ function peg$parse(input, options) {
1848
2063
  }
1849
2064
  if (s1 !== peg$FAILED) {
1850
2065
  peg$savedPos = peg$currPos;
1851
- s2 = peg$f35();
2066
+ s2 = peg$f38();
1852
2067
  if (s2) {
1853
2068
  s2 = undefined;
1854
2069
  } else {
@@ -1858,7 +2073,7 @@ function peg$parse(input, options) {
1858
2073
  s3 = peg$parseconsumeSingle();
1859
2074
  if (s3 !== peg$FAILED) {
1860
2075
  peg$savedPos = s0;
1861
- s0 = peg$f36(s3);
2076
+ s0 = peg$f39(s3);
1862
2077
  } else {
1863
2078
  peg$currPos = s0;
1864
2079
  s0 = peg$FAILED;
@@ -1893,7 +2108,7 @@ function peg$parse(input, options) {
1893
2108
  s3 = peg$parseconsumeSingleMore();
1894
2109
  }
1895
2110
  peg$savedPos = s0;
1896
- s0 = peg$f37(s1, s2);
2111
+ s0 = peg$f40(s1, s2);
1897
2112
  } else {
1898
2113
  peg$currPos = s0;
1899
2114
  s0 = peg$FAILED;
@@ -1904,7 +2119,7 @@ function peg$parse(input, options) {
1904
2119
  let s0, s1, s2;
1905
2120
  s0 = peg$currPos;
1906
2121
  peg$savedPos = peg$currPos;
1907
- s1 = peg$f38();
2122
+ s1 = peg$f41();
1908
2123
  if (s1) {
1909
2124
  s1 = undefined;
1910
2125
  } else {
@@ -1922,7 +2137,7 @@ function peg$parse(input, options) {
1922
2137
  }
1923
2138
  if (s2 !== peg$FAILED) {
1924
2139
  peg$savedPos = s0;
1925
- s0 = peg$f39(s2);
2140
+ s0 = peg$f42(s2);
1926
2141
  } else {
1927
2142
  peg$currPos = s0;
1928
2143
  s0 = peg$FAILED;
@@ -1939,12 +2154,12 @@ function peg$parse(input, options) {
1939
2154
  s1 = peg$currPos;
1940
2155
  peg$silentFails++;
1941
2156
  if (input.charCodeAt(peg$currPos) === 96) {
1942
- s2 = peg$c5;
2157
+ s2 = peg$c8;
1943
2158
  peg$currPos++;
1944
2159
  } else {
1945
2160
  s2 = peg$FAILED;
1946
2161
  if (peg$silentFails === 0) {
1947
- peg$fail(peg$e8);
2162
+ peg$fail(peg$e13);
1948
2163
  }
1949
2164
  }
1950
2165
  peg$silentFails--;
@@ -1956,7 +2171,7 @@ function peg$parse(input, options) {
1956
2171
  }
1957
2172
  if (s1 !== peg$FAILED) {
1958
2173
  peg$savedPos = peg$currPos;
1959
- s2 = peg$f40();
2174
+ s2 = peg$f43();
1960
2175
  if (s2) {
1961
2176
  s2 = undefined;
1962
2177
  } else {
@@ -1966,7 +2181,7 @@ function peg$parse(input, options) {
1966
2181
  s3 = peg$parseconsumeBacktick();
1967
2182
  if (s3 !== peg$FAILED) {
1968
2183
  peg$savedPos = s0;
1969
- s0 = peg$f41(s3);
2184
+ s0 = peg$f44(s3);
1970
2185
  } else {
1971
2186
  peg$currPos = s0;
1972
2187
  s0 = peg$FAILED;
@@ -2001,7 +2216,7 @@ function peg$parse(input, options) {
2001
2216
  s3 = peg$parseconsumeBacktickMore();
2002
2217
  }
2003
2218
  peg$savedPos = s0;
2004
- s0 = peg$f42(s1, s2);
2219
+ s0 = peg$f45(s1, s2);
2005
2220
  } else {
2006
2221
  peg$currPos = s0;
2007
2222
  s0 = peg$FAILED;
@@ -2012,7 +2227,7 @@ function peg$parse(input, options) {
2012
2227
  let s0, s1, s2;
2013
2228
  s0 = peg$currPos;
2014
2229
  peg$savedPos = peg$currPos;
2015
- s1 = peg$f43();
2230
+ s1 = peg$f46();
2016
2231
  if (s1) {
2017
2232
  s1 = undefined;
2018
2233
  } else {
@@ -2030,7 +2245,7 @@ function peg$parse(input, options) {
2030
2245
  }
2031
2246
  if (s2 !== peg$FAILED) {
2032
2247
  peg$savedPos = s0;
2033
- s0 = peg$f44(s2);
2248
+ s0 = peg$f47(s2);
2034
2249
  } else {
2035
2250
  peg$currPos = s0;
2036
2251
  s0 = peg$FAILED;
@@ -2046,7 +2261,7 @@ function peg$parse(input, options) {
2046
2261
  s0 = peg$currPos;
2047
2262
  s1 = "";
2048
2263
  peg$savedPos = s0;
2049
- s1 = peg$f45();
2264
+ s1 = peg$f48();
2050
2265
  s0 = s1;
2051
2266
  return s0;
2052
2267
  }
@@ -2055,28 +2270,28 @@ function peg$parse(input, options) {
2055
2270
  s0 = peg$currPos;
2056
2271
  s1 = [];
2057
2272
  if (input.charCodeAt(peg$currPos) === 32) {
2058
- s2 = peg$c6;
2273
+ s2 = peg$c9;
2059
2274
  peg$currPos++;
2060
2275
  } else {
2061
2276
  s2 = peg$FAILED;
2062
2277
  if (peg$silentFails === 0) {
2063
- peg$fail(peg$e9);
2278
+ peg$fail(peg$e14);
2064
2279
  }
2065
2280
  }
2066
2281
  while (s2 !== peg$FAILED) {
2067
2282
  s1.push(s2);
2068
2283
  if (input.charCodeAt(peg$currPos) === 32) {
2069
- s2 = peg$c6;
2284
+ s2 = peg$c9;
2070
2285
  peg$currPos++;
2071
2286
  } else {
2072
2287
  s2 = peg$FAILED;
2073
2288
  if (peg$silentFails === 0) {
2074
- peg$fail(peg$e9);
2289
+ peg$fail(peg$e14);
2075
2290
  }
2076
2291
  }
2077
2292
  }
2078
2293
  peg$savedPos = s0;
2079
- s1 = peg$f46(s1);
2294
+ s1 = peg$f49(s1);
2080
2295
  s0 = s1;
2081
2296
  return s0;
2082
2297
  }
@@ -2085,28 +2300,28 @@ function peg$parse(input, options) {
2085
2300
  s0 = peg$currPos;
2086
2301
  s1 = [];
2087
2302
  if (input.charCodeAt(peg$currPos) === 32) {
2088
- s2 = peg$c6;
2303
+ s2 = peg$c9;
2089
2304
  peg$currPos++;
2090
2305
  } else {
2091
2306
  s2 = peg$FAILED;
2092
2307
  if (peg$silentFails === 0) {
2093
- peg$fail(peg$e9);
2308
+ peg$fail(peg$e14);
2094
2309
  }
2095
2310
  }
2096
2311
  while (s2 !== peg$FAILED) {
2097
2312
  s1.push(s2);
2098
2313
  if (input.charCodeAt(peg$currPos) === 32) {
2099
- s2 = peg$c6;
2314
+ s2 = peg$c9;
2100
2315
  peg$currPos++;
2101
2316
  } else {
2102
2317
  s2 = peg$FAILED;
2103
2318
  if (peg$silentFails === 0) {
2104
- peg$fail(peg$e9);
2319
+ peg$fail(peg$e14);
2105
2320
  }
2106
2321
  }
2107
2322
  }
2108
2323
  peg$savedPos = peg$currPos;
2109
- s2 = peg$f47(s1);
2324
+ s2 = peg$f50(s1);
2110
2325
  if (s2) {
2111
2326
  s2 = undefined;
2112
2327
  } else {
@@ -2114,7 +2329,7 @@ function peg$parse(input, options) {
2114
2329
  }
2115
2330
  if (s2 !== peg$FAILED) {
2116
2331
  peg$savedPos = s0;
2117
- s0 = peg$f48(s1);
2332
+ s0 = peg$f51(s1);
2118
2333
  } else {
2119
2334
  peg$currPos = s0;
2120
2335
  s0 = peg$FAILED;
@@ -2126,28 +2341,28 @@ function peg$parse(input, options) {
2126
2341
  s0 = peg$currPos;
2127
2342
  s1 = [];
2128
2343
  if (input.charCodeAt(peg$currPos) === 32) {
2129
- s2 = peg$c6;
2344
+ s2 = peg$c9;
2130
2345
  peg$currPos++;
2131
2346
  } else {
2132
2347
  s2 = peg$FAILED;
2133
2348
  if (peg$silentFails === 0) {
2134
- peg$fail(peg$e9);
2349
+ peg$fail(peg$e14);
2135
2350
  }
2136
2351
  }
2137
2352
  while (s2 !== peg$FAILED) {
2138
2353
  s1.push(s2);
2139
2354
  if (input.charCodeAt(peg$currPos) === 32) {
2140
- s2 = peg$c6;
2355
+ s2 = peg$c9;
2141
2356
  peg$currPos++;
2142
2357
  } else {
2143
2358
  s2 = peg$FAILED;
2144
2359
  if (peg$silentFails === 0) {
2145
- peg$fail(peg$e9);
2360
+ peg$fail(peg$e14);
2146
2361
  }
2147
2362
  }
2148
2363
  }
2149
2364
  peg$savedPos = peg$currPos;
2150
- s2 = peg$f49(s1);
2365
+ s2 = peg$f52(s1);
2151
2366
  if (s2) {
2152
2367
  s2 = undefined;
2153
2368
  } else {
@@ -2358,7 +2573,7 @@ function peg$parse(input, options) {
2358
2573
  let s0, s1, s2, s3;
2359
2574
  s0 = peg$currPos;
2360
2575
  peg$savedPos = peg$currPos;
2361
- s1 = peg$f50();
2576
+ s1 = peg$f53();
2362
2577
  if (s1) {
2363
2578
  s1 = undefined;
2364
2579
  } else {
@@ -2457,12 +2672,12 @@ function peg$parse(input, options) {
2457
2672
  function peg$parsewhiteSpaceSymbol() {
2458
2673
  let s0;
2459
2674
  s0 = input.charAt(peg$currPos);
2460
- if (peg$r2.test(s0)) {
2675
+ if (peg$r4.test(s0)) {
2461
2676
  peg$currPos++;
2462
2677
  } else {
2463
2678
  s0 = peg$FAILED;
2464
2679
  if (peg$silentFails === 0) {
2465
- peg$fail(peg$e10);
2680
+ peg$fail(peg$e15);
2466
2681
  }
2467
2682
  }
2468
2683
  return s0;
@@ -2470,12 +2685,12 @@ function peg$parse(input, options) {
2470
2685
  function peg$parsereferenceSymbol() {
2471
2686
  let s0;
2472
2687
  s0 = input.charAt(peg$currPos);
2473
- if (peg$r3.test(s0)) {
2688
+ if (peg$r5.test(s0)) {
2474
2689
  peg$currPos++;
2475
2690
  } else {
2476
2691
  s0 = peg$FAILED;
2477
2692
  if (peg$silentFails === 0) {
2478
- peg$fail(peg$e11);
2693
+ peg$fail(peg$e16);
2479
2694
  }
2480
2695
  }
2481
2696
  return s0;
@@ -3484,22 +3699,7 @@ function canonical(node) {
3484
3699
  return new Link(node.id, node.values.map(canonical));
3485
3700
  }
3486
3701
  function formatBinaryReference(text) {
3487
- if (text && !text.startsWith("#") && !/[\p{White_Space}\u001c-\u001f\ufeff():"'`]/u.test(text))
3488
- return text;
3489
- let choice = null;
3490
- for (const quote of ["'", '"', "`"])
3491
- if (!text.startsWith(quote)) {
3492
- let longest = 0, run = 0;
3493
- for (const c of text) {
3494
- run = c === quote ? run + 1 : 0;
3495
- longest = Math.max(longest, run);
3496
- }
3497
- const count = longest + 1 | 1;
3498
- if (choice === null || count < choice.count)
3499
- choice = { quote, count };
3500
- }
3501
- const delimiter = choice.quote.repeat(choice.count);
3502
- return delimiter + text + delimiter;
3702
+ return formatReference(text);
3503
3703
  }
3504
3704
  function formatBinaryDocument(document) {
3505
3705
  function nested(node, top = false) {
@@ -3581,6 +3781,8 @@ export {
3581
3781
  Section,
3582
3782
  StreamParseError,
3583
3783
  StreamParser,
3784
+ decodeReferenceLiteral,
3785
+ encodeReferenceLiteral,
3584
3786
  formatBinaryDocument,
3585
3787
  formatBinaryReference,
3586
3788
  formatLinks,