akanjs 3.1.0-rc.16 → 3.1.0-rc.18

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "3.1.0-rc.16",
3
+ "version": "3.1.0-rc.18",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -31,13 +31,18 @@ export declare class Reference {
31
31
  *
32
32
  * The text is the source of truth for which references a message carries — deleting the token is how somebody
33
33
  * takes one back — so the token has to say everything the pointer does. The value is looked up beside it by key.
34
+ * A label's `\`, `[` and `]` are backslash-escaped, as in a markdown link: titles like "[Ops] Minutes" are common.
34
35
  */
35
36
  static token(reference: MessageReference): string;
36
37
  /**
37
- * Greedy on the label alone: a label may hold anything but `]` or an `@[`, where a token starts over, and every
38
- * other part is a name, an id or a dotted path, none of which holds a bracket.
38
+ * Greedy on the label alone: a label may hold an escaped `\\`, `\[` or `\]`, a backslash escaping nothing (a token
39
+ * written before labels were escaped), and anything else but `]` or an `@[`, where a token starts over. No two
40
+ * alternatives match at the same place, so a long run cannot backtrack. Every other part is a name, an id or a
41
+ * dotted path, none of which holds a bracket.
39
42
  */
40
43
  static readonly pattern: RegExp;
44
+ /** The pointer one match of `pattern` names, its label unescaped. */
45
+ static pointerOf([, label, refName, refId, path]: RegExpMatchArray): MessageReference;
41
46
  /**
42
47
  * The pointers a draft names, in the order it names them. Pointers only — the value lives in `staged` and is
43
48
  * joined on by key, so reading the text can never resurrect a value somebody deleted the token for.
@@ -32,10 +32,9 @@ export class MentionDraft {
32
32
 
33
33
  const pattern = new RegExp(Reference.pattern.source, "g");
34
34
  for (let match = pattern.exec(text); match; match = pattern.exec(text)) {
35
- const [token, label, refName, refId, path] = match;
36
35
  if (match.index > at) nodes.push(...MentionDraft.#plain(text.slice(at, match.index)));
37
- nodes.push($createMentionNode({ refName, refId, label, ...(path ? { path } : {}) }));
38
- at = match.index + token.length;
36
+ nodes.push($createMentionNode(Reference.pointerOf(match)));
37
+ at = match.index + match[0].length;
39
38
  }
40
39
  if (at < text.length) nodes.push(...MentionDraft.#plain(text.slice(at)));
41
40
  return nodes;
@@ -38,28 +38,32 @@ export class Reference {
38
38
  *
39
39
  * The text is the source of truth for which references a message carries — deleting the token is how somebody
40
40
  * takes one back — so the token has to say everything the pointer does. The value is looked up beside it by key.
41
+ * A label's `\`, `[` and `]` are backslash-escaped, as in a markdown link: titles like "[Ops] Minutes" are common.
41
42
  */
42
43
  static token(reference: MessageReference): string {
43
- return `@[${reference.label}](mention:${Reference.keyOf(reference)})`;
44
+ return `@[${reference.label.replace(/[\\[\]]/g, "\\$&")}](mention:${Reference.keyOf(reference)})`;
44
45
  }
45
46
 
46
47
  /**
47
- * Greedy on the label alone: a label may hold anything but `]` or an `@[`, where a token starts over, and every
48
- * other part is a name, an id or a dotted path, none of which holds a bracket.
48
+ * Greedy on the label alone: a label may hold an escaped `\\`, `\[` or `\]`, a backslash escaping nothing (a token
49
+ * written before labels were escaped), and anything else but `]` or an `@[`, where a token starts over. No two
50
+ * alternatives match at the same place, so a long run cannot backtrack. Every other part is a name, an id or a
51
+ * dotted path, none of which holds a bracket.
49
52
  */
50
- static readonly pattern = /@\[((?:(?!@\[)[^\]])*)\]\(mention:([^/)[\]]+)\/([^#)[\]]+)(?:#([^)[\]]*))?\)/g;
53
+ static readonly pattern =
54
+ /@\[((?:\\[\\[\]]|\\(?![\\[\]])|(?!@\[)[^\]\\])*)\]\(mention:([^/)[\]]+)\/([^#)[\]]+)(?:#([^)[\]]*))?\)/g;
55
+
56
+ /** The pointer one match of `pattern` names, its label unescaped. */
57
+ static pointerOf([, label, refName, refId, path]: RegExpMatchArray): MessageReference {
58
+ return { refName, refId, label: label.replace(/\\([\\[\]])/g, "$1"), ...(path ? { path } : {}) };
59
+ }
51
60
 
52
61
  /**
53
62
  * The pointers a draft names, in the order it names them. Pointers only — the value lives in `staged` and is
54
63
  * joined on by key, so reading the text can never resurrect a value somebody deleted the token for.
55
64
  */
56
65
  static parse(text: string): MessageReference[] {
57
- return [...text.matchAll(Reference.pattern)].map(([, label, refName, refId, path]) => ({
58
- refName,
59
- refId,
60
- label,
61
- ...(path ? { path } : {}),
62
- }));
66
+ return [...text.matchAll(Reference.pattern)].map((match) => Reference.pointerOf(match));
63
67
  }
64
68
 
65
69
  /**