@openpeepshq/common 0.32.0 → 0.33.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.
@@ -18,6 +18,11 @@ export type MentionProfileRef = {
18
18
  * Bare `@handle` tokens use the allowed handle charset (letters, digits, `_`, `-`).
19
19
  */
20
20
  export declare const linkProfileMentions: (source: string, mentions?: MentionProfileRef[]) => string;
21
+ /**
22
+ * All mention tokens in the text, deduplicated: local `@handle` tokens plus
23
+ * remote `handle@domain` tokens (domain lowercased).
24
+ */
25
+ export declare const matchFederatedMentionTokens: (text?: string | null) => string[];
21
26
  /**
22
27
  * Pre-process markdown source: convert `@handle` mentions into markdown
23
28
  * profile links. Hashtag conversion is handled per-platform (web regex vs.
@@ -1,4 +1,4 @@
1
- import { mentionHandleRegexBase } from '../types/index.js';
1
+ import { federatedMentionHandleRegexBase, mentionHandleRegexBase, } from '../types/index.js';
2
2
  const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
3
3
  const mentionPrefix = '(^|[\\s(>])';
4
4
  const mentionSuffix = '(?=\\s|$|[.,!?;:])';
@@ -25,8 +25,33 @@ export const linkProfileMentions = (source, mentions = []) => {
25
25
  const pattern = new RegExp(`${mentionPrefix}@(${escapeRegExp(name)})${mentionSuffix}`, 'gi');
26
26
  text = text.replace(pattern, (_full, prefix, mentioned) => `${prefix}[@${mentioned}](/@${handle})`);
27
27
  }
28
+ // Replace bare local `@handle` tokens last; a federated token
29
+ // (`@user@domain`) must not be wrapped in a local profile link. The
30
+ // bare-handle regex's prefix class `(^|[\s(>])` cannot match at the `@`
31
+ // before the domain, so remote mentions stay as plain text here.
28
32
  return text.replace(new RegExp(mentionHandleRegexBase, 'g'), (_full, prefix, handle) => `${prefix}[@${handle}](/@${handle})`);
29
33
  };
34
+ /**
35
+ * All mention tokens in the text, deduplicated: local `@handle` tokens plus
36
+ * remote `handle@domain` tokens (domain lowercased).
37
+ */
38
+ export const matchFederatedMentionTokens = (text) => {
39
+ if (!text)
40
+ return [];
41
+ const tokens = new Set();
42
+ const fedRegex = new RegExp(federatedMentionHandleRegexBase, 'gi');
43
+ for (const match of text.matchAll(fedRegex)) {
44
+ const handle = match[2]?.toLowerCase();
45
+ const domain = match[3]?.toLowerCase();
46
+ if (handle && domain)
47
+ tokens.add(`${handle}@${domain}`);
48
+ }
49
+ for (const match of text.matchAll(new RegExp(mentionHandleRegexBase, 'g'))) {
50
+ if (match[2])
51
+ tokens.add(match[2]);
52
+ }
53
+ return [...tokens];
54
+ };
30
55
  /**
31
56
  * Pre-process markdown source: convert `@handle` mentions into markdown
32
57
  * profile links. Hashtag conversion is handled per-platform (web regex vs.
@@ -3,6 +3,16 @@ import { HANDLE_RESERVED_MESSAGE, additionalForbiddenHandlesSchema, classifyAddi
3
3
  export { HANDLE_RESERVED_MESSAGE, additionalForbiddenHandlesSchema, classifyAdditionalForbiddenHandle, collectAdditionalForbiddenHandles, forbiddenHandles, handleRegex, handleRegexBase, isDefaultForbiddenHandle, isForbiddenHandle, normalizeForbiddenHandle, };
4
4
  /** `@handle` using the allowed handle charset. No `\\b` — hyphens are not word chars. */
5
5
  export declare const mentionHandleRegexBase = "(^|[\\s(>])@([a-zA-Z0-9_-]{1,16})(?=\\s|$|[.,!?;:])";
6
+ /**
7
+ * Federated handle mention: `@user@domain`. The domain is a dotted host
8
+ * (multi-label allowed, e.g. `mastodon.social`). No `\\b` — hyphens are not
9
+ * word chars. Group 2 = local handle, group 3 = remote domain.
10
+ *
11
+ * The trailing lookahead adds `)` so a parenthesised mention `(@a@x.co)`
12
+ * still matches; the bare local regex keeps the shorter class and so does not
13
+ * match a parenthesised `(@a)` (a pre-existing, intentional gap).
14
+ */
15
+ export declare const federatedMentionHandleRegexBase = "(^|[\\s(>])@([a-zA-Z0-9_-]{1,16})@([a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?){1,})(?=\\s|$|[.,!?;):])";
6
16
  export declare const baseSchemaShape: {
7
17
  createdAt: z.ZodISODateTime;
8
18
  updatedAt: z.ZodISODateTime;
@@ -12,6 +12,16 @@ export { HANDLE_RESERVED_MESSAGE, additionalForbiddenHandlesSchema, classifyAddi
12
12
  extendZodWithOpenApi(z);
13
13
  /** `@handle` using the allowed handle charset. No `\\b` — hyphens are not word chars. */
14
14
  export const mentionHandleRegexBase = `(^|[\\s(>])@(${handleRegexBase})(?=\\s|$|[.,!?;:])`;
15
+ /**
16
+ * Federated handle mention: `@user@domain`. The domain is a dotted host
17
+ * (multi-label allowed, e.g. `mastodon.social`). No `\\b` — hyphens are not
18
+ * word chars. Group 2 = local handle, group 3 = remote domain.
19
+ *
20
+ * The trailing lookahead adds `)` so a parenthesised mention `(@a@x.co)`
21
+ * still matches; the bare local regex keeps the shorter class and so does not
22
+ * match a parenthesised `(@a)` (a pre-existing, intentional gap).
23
+ */
24
+ export const federatedMentionHandleRegexBase = `(^|[\\s(>])@(${handleRegexBase})@([a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?){1,})(?=\\s|$|[.,!?;):])`;
15
25
  export const baseSchemaShape = {
16
26
  createdAt: z.iso.datetime(),
17
27
  updatedAt: z.iso.datetime(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpeepshq/common",
3
- "version": "0.32.0",
3
+ "version": "0.33.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -1,4 +1,7 @@
1
- import { mentionHandleRegexBase } from '../types';
1
+ import {
2
+ federatedMentionHandleRegexBase,
3
+ mentionHandleRegexBase,
4
+ } from '../types';
2
5
 
3
6
  export interface MarkdownRenderProps {
4
7
  source?: string;
@@ -52,6 +55,10 @@ export const linkProfileMentions = (
52
55
  );
53
56
  }
54
57
 
58
+ // Replace bare local `@handle` tokens last; a federated token
59
+ // (`@user@domain`) must not be wrapped in a local profile link. The
60
+ // bare-handle regex's prefix class `(^|[\s(>])` cannot match at the `@`
61
+ // before the domain, so remote mentions stay as plain text here.
55
62
  return text.replace(
56
63
  new RegExp(mentionHandleRegexBase, 'g'),
57
64
  (_full, prefix: string, handle: string) =>
@@ -59,6 +66,25 @@ export const linkProfileMentions = (
59
66
  );
60
67
  };
61
68
 
69
+ /**
70
+ * All mention tokens in the text, deduplicated: local `@handle` tokens plus
71
+ * remote `handle@domain` tokens (domain lowercased).
72
+ */
73
+ export const matchFederatedMentionTokens = (text?: string | null): string[] => {
74
+ if (!text) return [];
75
+ const tokens = new Set<string>();
76
+ const fedRegex = new RegExp(federatedMentionHandleRegexBase, 'gi');
77
+ for (const match of text.matchAll(fedRegex)) {
78
+ const handle = match[2]?.toLowerCase();
79
+ const domain = match[3]?.toLowerCase();
80
+ if (handle && domain) tokens.add(`${handle}@${domain}`);
81
+ }
82
+ for (const match of text.matchAll(new RegExp(mentionHandleRegexBase, 'g'))) {
83
+ if (match[2]) tokens.add(match[2]);
84
+ }
85
+ return [...tokens];
86
+ };
87
+
62
88
  /**
63
89
  * Pre-process markdown source: convert `@handle` mentions into markdown
64
90
  * profile links. Hashtag conversion is handled per-platform (web regex vs.
@@ -41,6 +41,17 @@ extendZodWithOpenApi(z);
41
41
  /** `@handle` using the allowed handle charset. No `\\b` — hyphens are not word chars. */
42
42
  export const mentionHandleRegexBase = `(^|[\\s(>])@(${handleRegexBase})(?=\\s|$|[.,!?;:])`;
43
43
 
44
+ /**
45
+ * Federated handle mention: `@user@domain`. The domain is a dotted host
46
+ * (multi-label allowed, e.g. `mastodon.social`). No `\\b` — hyphens are not
47
+ * word chars. Group 2 = local handle, group 3 = remote domain.
48
+ *
49
+ * The trailing lookahead adds `)` so a parenthesised mention `(@a@x.co)`
50
+ * still matches; the bare local regex keeps the shorter class and so does not
51
+ * match a parenthesised `(@a)` (a pre-existing, intentional gap).
52
+ */
53
+ export const federatedMentionHandleRegexBase = `(^|[\\s(>])@(${handleRegexBase})@([a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?){1,})(?=\\s|$|[.,!?;):])`;
54
+
44
55
  export const baseSchemaShape = {
45
56
  createdAt: z.iso.datetime(),
46
57
  updatedAt: z.iso.datetime(),