@critical-path/core 0.20.2 → 0.21.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/CHANGELOG.md +10 -0
- package/README.md +41 -0
- package/dist/domain/task-duration.test.js +32 -0
- package/dist/domain/task-duration.test.js.map +1 -1
- package/dist/engine/index.d.ts.map +1 -1
- package/dist/engine/index.js +40 -9
- package/dist/engine/index.js.map +1 -1
- package/dist/status.test.js +97 -1
- package/dist/status.test.js.map +1 -1
- package/dist/store/sqlite.d.ts.map +1 -1
- package/dist/store/sqlite.js +14 -5
- package/dist/store/sqlite.js.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/utils/fractional-index.d.ts +50 -0
- package/dist/utils/fractional-index.d.ts.map +1 -0
- package/dist/utils/fractional-index.js +230 -0
- package/dist/utils/fractional-index.js.map +1 -0
- package/dist/utils/fractional-index.test.d.ts +2 -0
- package/dist/utils/fractional-index.test.d.ts.map +1 -0
- package/dist/utils/fractional-index.test.js +151 -0
- package/dist/utils/fractional-index.test.js.map +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/key.d.ts +5 -0
- package/dist/utils/key.d.ts.map +1 -1
- package/dist/utils/key.js +10 -0
- package/dist/utils/key.js.map +1 -1
- package/dist/utils/key.test.js +18 -1
- package/dist/utils/key.test.js.map +1 -1
- package/dist/utils/mentions.d.ts +13 -5
- package/dist/utils/mentions.d.ts.map +1 -1
- package/dist/utils/mentions.js +52 -16
- package/dist/utils/mentions.js.map +1 -1
- package/dist/utils/mentions.test.js +32 -1
- package/dist/utils/mentions.test.js.map +1 -1
- package/dist/utils/status.d.ts +80 -1
- package/dist/utils/status.d.ts.map +1 -1
- package/dist/utils/status.js +194 -0
- package/dist/utils/status.js.map +1 -1
- package/package.json +1 -1
- package/src/domain/task-duration.test.ts +36 -0
- package/src/engine/index.ts +39 -9
- package/src/status.test.ts +130 -2
- package/src/store/sqlite.ts +14 -3
- package/src/types/index.ts +1 -0
- package/src/utils/fractional-index.test.ts +191 -0
- package/src/utils/fractional-index.ts +260 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/key.test.ts +22 -1
- package/src/utils/key.ts +11 -0
- package/src/utils/mentions.test.ts +37 -1
- package/src/utils/mentions.ts +58 -16
- package/src/utils/status.ts +247 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fractional Lexical Indexing Utility
|
|
3
|
+
*
|
|
4
|
+
* Deterministic Base-62 fractional indexing for ordered lists and swimlane task sequences.
|
|
5
|
+
* Produces lexicographically sortable strings (`orderIndex`) that allow infinite insertion
|
|
6
|
+
* between any two items without floating-point precision loss.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const BASE_62 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Encodes a non-negative integer into Base-62.
|
|
13
|
+
*/
|
|
14
|
+
function toBase62(n: number): string {
|
|
15
|
+
if (n === 0) return '0';
|
|
16
|
+
let s = '';
|
|
17
|
+
let curr = n;
|
|
18
|
+
while (curr > 0) {
|
|
19
|
+
s = BASE_62[curr % 62] + s;
|
|
20
|
+
curr = Math.floor(curr / 62);
|
|
21
|
+
}
|
|
22
|
+
return s;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Decodes a Base-62 string into a non-negative integer.
|
|
27
|
+
*/
|
|
28
|
+
function fromBase62(s: string): number {
|
|
29
|
+
let n = 0;
|
|
30
|
+
for (let i = 0; i < s.length; i++) {
|
|
31
|
+
const idx = BASE_62.indexOf(s[i]);
|
|
32
|
+
if (idx === -1) return 0;
|
|
33
|
+
n = n * 62 + idx;
|
|
34
|
+
}
|
|
35
|
+
return n;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Encodes an integer into a prefix-encoded string such that standard
|
|
40
|
+
* string comparison strictly matches integer comparison.
|
|
41
|
+
*
|
|
42
|
+
* Non-negative integers (0, 1, 2, ...):
|
|
43
|
+
* Prefix 'a'-'z' specifies the digit count (1 to 26 digits).
|
|
44
|
+
* 0 -> "a0", 1 -> "a1", 61 -> "az", 62 -> "b10", ...
|
|
45
|
+
*
|
|
46
|
+
* Negative integers (-1, -2, ...):
|
|
47
|
+
* Prefix 'Z'-'A' specifies inverted digit count with inverted digits.
|
|
48
|
+
* -1 -> "Zz", -2 -> "Zy", ...
|
|
49
|
+
*/
|
|
50
|
+
export function encodeInteger(n: number): string {
|
|
51
|
+
if (n >= 0) {
|
|
52
|
+
const digits = toBase62(n);
|
|
53
|
+
const prefixCode = 97 + digits.length - 1; // 97 is 'a'
|
|
54
|
+
const prefix = String.fromCharCode(Math.min(prefixCode, 122)); // clamp to 'z'
|
|
55
|
+
return prefix + digits;
|
|
56
|
+
} else {
|
|
57
|
+
const pos = -n - 1;
|
|
58
|
+
const digits = toBase62(pos);
|
|
59
|
+
let inverted = '';
|
|
60
|
+
for (let i = 0; i < digits.length; i++) {
|
|
61
|
+
const idx = BASE_62.indexOf(digits[i]);
|
|
62
|
+
inverted += BASE_62[61 - idx];
|
|
63
|
+
}
|
|
64
|
+
const prefixCode = 90 - (digits.length - 1); // 90 is 'Z'
|
|
65
|
+
const prefix = String.fromCharCode(Math.max(prefixCode, 65)); // clamp to 'A'
|
|
66
|
+
return prefix + inverted;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Decodes a prefix-encoded integer string back into a number.
|
|
72
|
+
*/
|
|
73
|
+
export function decodeInteger(s: string): number {
|
|
74
|
+
if (!s || s.length < 2) return 0;
|
|
75
|
+
const prefix = s[0];
|
|
76
|
+
if (prefix >= 'a' && prefix <= 'z') {
|
|
77
|
+
const len = prefix.charCodeAt(0) - 97 + 1;
|
|
78
|
+
const digits = s.slice(1, 1 + len);
|
|
79
|
+
return fromBase62(digits);
|
|
80
|
+
} else if (prefix >= 'A' && prefix <= 'Z') {
|
|
81
|
+
const len = 90 - prefix.charCodeAt(0) + 1;
|
|
82
|
+
const inverted = s.slice(1, 1 + len);
|
|
83
|
+
let digits = '';
|
|
84
|
+
for (let i = 0; i < inverted.length; i++) {
|
|
85
|
+
const idx = BASE_62.indexOf(inverted[i]);
|
|
86
|
+
digits += idx !== -1 ? BASE_62[61 - idx] : '0';
|
|
87
|
+
}
|
|
88
|
+
const pos = fromBase62(digits);
|
|
89
|
+
return -pos - 1;
|
|
90
|
+
}
|
|
91
|
+
return 0;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Extracts the integer component from an order key.
|
|
96
|
+
*/
|
|
97
|
+
export function getIntegerPart(key: string): string {
|
|
98
|
+
if (!key || key.length < 2) return 'a0';
|
|
99
|
+
const prefix = key[0];
|
|
100
|
+
if (prefix >= 'a' && prefix <= 'z') {
|
|
101
|
+
const len = prefix.charCodeAt(0) - 97 + 1;
|
|
102
|
+
return key.slice(0, Math.min(key.length, 1 + len));
|
|
103
|
+
} else if (prefix >= 'A' && prefix <= 'Z') {
|
|
104
|
+
const len = 90 - prefix.charCodeAt(0) + 1;
|
|
105
|
+
return key.slice(0, Math.min(key.length, 1 + len));
|
|
106
|
+
}
|
|
107
|
+
return 'a0';
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Computes a fractional midpoint suffix between suffixA and suffixB.
|
|
112
|
+
*/
|
|
113
|
+
function midpointSuffix(a: string, b: string | null): string {
|
|
114
|
+
let i = 0;
|
|
115
|
+
while (i < a.length && b !== null && i < b.length && a[i] === b[i]) {
|
|
116
|
+
i++;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const common = a.slice(0, i);
|
|
120
|
+
const charA = i < a.length ? a[i] : null;
|
|
121
|
+
const charB = b !== null && i < b.length ? b[i] : null;
|
|
122
|
+
|
|
123
|
+
const digitA = charA !== null ? BASE_62.indexOf(charA) : 0;
|
|
124
|
+
const digitB = charB !== null ? BASE_62.indexOf(charB) : BASE_62.length;
|
|
125
|
+
|
|
126
|
+
if (digitB - digitA > 1) {
|
|
127
|
+
const mid = Math.floor((digitA + digitB) / 2);
|
|
128
|
+
return common + BASE_62[mid];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (charA !== null) {
|
|
132
|
+
const restA = a.slice(i + 1);
|
|
133
|
+
const restB = b !== null && i < b.length && charA === charB ? b.slice(i + 1) : null;
|
|
134
|
+
return common + charA + midpointSuffix(restA, restB);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (b !== null && i < b.length) {
|
|
138
|
+
if (digitB === 0) {
|
|
139
|
+
const restB = b.slice(i + 1);
|
|
140
|
+
return common + '0' + midpointSuffix('', restB);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return common + 'V';
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Generates a default initial order key for an index with optional spacing.
|
|
149
|
+
* E.g. index 0 -> "a0", index 1 (spacing 2) -> "a2", index 2 -> "a4"
|
|
150
|
+
*/
|
|
151
|
+
export function getInitialKey(index: number, spacing: number = 2): string {
|
|
152
|
+
return encodeInteger(Math.max(0, index) * Math.max(1, spacing));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Generates a single key strictly between keys `a` and `b` (a < key < b).
|
|
157
|
+
* Handles null/undefined for lower and upper bounds.
|
|
158
|
+
*/
|
|
159
|
+
export function generateKeyBetween(
|
|
160
|
+
a: string | null | undefined,
|
|
161
|
+
b: string | null | undefined
|
|
162
|
+
): string {
|
|
163
|
+
const hasA = a !== null && a !== undefined && a !== '';
|
|
164
|
+
const hasB = b !== null && b !== undefined && b !== '';
|
|
165
|
+
|
|
166
|
+
if (!hasA && !hasB) {
|
|
167
|
+
return 'a0';
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (!hasA && hasB) {
|
|
171
|
+
const intB = getIntegerPart(b!);
|
|
172
|
+
if (intB === b) {
|
|
173
|
+
const valB = decodeInteger(intB);
|
|
174
|
+
return encodeInteger(valB - 1);
|
|
175
|
+
}
|
|
176
|
+
// If b has fractional suffix (e.g. "a0V"), integer part is already strictly smaller than b
|
|
177
|
+
return intB;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (hasA && !hasB) {
|
|
181
|
+
const intA = getIntegerPart(a!);
|
|
182
|
+
const valA = decodeInteger(intA);
|
|
183
|
+
return encodeInteger(valA + 1);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Both a and b are present
|
|
187
|
+
if (a! >= b!) {
|
|
188
|
+
// Graceful fallback for inverted or equal keys
|
|
189
|
+
return a! + 'V';
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const intA = getIntegerPart(a!);
|
|
193
|
+
const intB = getIntegerPart(b!);
|
|
194
|
+
|
|
195
|
+
if (intA !== intB) {
|
|
196
|
+
const valA = decodeInteger(intA);
|
|
197
|
+
const valB = decodeInteger(intB);
|
|
198
|
+
|
|
199
|
+
if (valB - valA > 1) {
|
|
200
|
+
const midVal = Math.floor((valA + valB) / 2);
|
|
201
|
+
return encodeInteger(midVal);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (valB - valA === 1) {
|
|
205
|
+
const suffixA = a!.slice(intA.length);
|
|
206
|
+
return intA + midpointSuffix(suffixA, null);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Same integer part or fractional subdivision
|
|
211
|
+
const suffixA = a!.slice(intA.length);
|
|
212
|
+
const suffixB = b!.slice(intB.length);
|
|
213
|
+
return intA + midpointSuffix(suffixA, suffixB);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Generates `count` keys in strictly ascending order between keys `a` and `b`.
|
|
218
|
+
* Uses a balanced divide-and-conquer partition to guarantee even spacing.
|
|
219
|
+
*/
|
|
220
|
+
export function generateNKeysBetween(
|
|
221
|
+
a: string | null | undefined,
|
|
222
|
+
b: string | null | undefined,
|
|
223
|
+
count: number
|
|
224
|
+
): string[] {
|
|
225
|
+
if (count <= 0) return [];
|
|
226
|
+
if (count === 1) return [generateKeyBetween(a, b)];
|
|
227
|
+
|
|
228
|
+
const result: string[] = new Array(count);
|
|
229
|
+
|
|
230
|
+
function fill(
|
|
231
|
+
leftKey: string | null | undefined,
|
|
232
|
+
rightKey: string | null | undefined,
|
|
233
|
+
startIdx: number,
|
|
234
|
+
endIdx: number
|
|
235
|
+
) {
|
|
236
|
+
if (startIdx > endIdx) return;
|
|
237
|
+
const midIdx = Math.floor((startIdx + endIdx) / 2);
|
|
238
|
+
const midKey = generateKeyBetween(leftKey, rightKey);
|
|
239
|
+
result[midIdx] = midKey;
|
|
240
|
+
fill(leftKey, midKey, startIdx, midIdx - 1);
|
|
241
|
+
fill(midKey, rightKey, midIdx + 1, endIdx);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
fill(a, b, 0, count - 1);
|
|
245
|
+
return result;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Standard comparator for fractional order indices.
|
|
250
|
+
* Uses strict code-point comparison matching Firestore, SQLite, and JavaScript array.sort().
|
|
251
|
+
*/
|
|
252
|
+
export function compareOrderIndices(
|
|
253
|
+
a: string | null | undefined,
|
|
254
|
+
b: string | null | undefined
|
|
255
|
+
): number {
|
|
256
|
+
if (!a && !b) return 0;
|
|
257
|
+
if (!a) return 1;
|
|
258
|
+
if (!b) return -1;
|
|
259
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
260
|
+
}
|
package/src/utils/index.ts
CHANGED
package/src/utils/key.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { generateProjectKey, validateProjectKey, formatTaskKey } from './key.js';
|
|
2
|
+
import { generateProjectKey, validateProjectKey, formatTaskKey, isTempTaskId } from './key.js';
|
|
3
3
|
|
|
4
4
|
describe('Project Key Utilities', () => {
|
|
5
5
|
describe('generateProjectKey', () => {
|
|
@@ -52,4 +52,25 @@ describe('Project Key Utilities', () => {
|
|
|
52
52
|
expect(formatTaskKey('', 10)).toBe('TASK-10');
|
|
53
53
|
});
|
|
54
54
|
});
|
|
55
|
+
|
|
56
|
+
describe('isTempTaskId', () => {
|
|
57
|
+
it('detects temp_ prefixed IDs', () => {
|
|
58
|
+
expect(isTempTaskId('temp_123456_abc')).toBe(true);
|
|
59
|
+
expect(isTempTaskId('#temp_123456_abc')).toBe(true);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('detects temp- prefixed IDs', () => {
|
|
63
|
+
expect(isTempTaskId('temp-task-1')).toBe(true);
|
|
64
|
+
expect(isTempTaskId('#temp-task-1')).toBe(true);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('returns false for permanent or invalid IDs', () => {
|
|
68
|
+
expect(isTempTaskId('task_123')).toBe(false);
|
|
69
|
+
expect(isTempTaskId('PROJ-1')).toBe(false);
|
|
70
|
+
expect(isTempTaskId(null)).toBe(false);
|
|
71
|
+
expect(isTempTaskId(undefined)).toBe(false);
|
|
72
|
+
expect(isTempTaskId('')).toBe(false);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
55
75
|
});
|
|
76
|
+
|
package/src/utils/key.ts
CHANGED
|
@@ -49,3 +49,14 @@ export function formatTaskKey(projectKey: string, taskNumber: number): string {
|
|
|
49
49
|
const prefix = projectKey ? projectKey.toUpperCase() : 'TASK';
|
|
50
50
|
return `${prefix}-${taskNumber}`;
|
|
51
51
|
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Checks whether a given task ID represents a temporary/optimistic task
|
|
55
|
+
* (e.g. temp_1790... or temp-task-...) while it is actively being created.
|
|
56
|
+
*/
|
|
57
|
+
export function isTempTaskId(id?: string | null): boolean {
|
|
58
|
+
if (!id || typeof id !== 'string') return false;
|
|
59
|
+
const clean = id.trim().replace(/^#/, '');
|
|
60
|
+
return clean.startsWith('temp_') || clean.startsWith('temp-');
|
|
61
|
+
}
|
|
62
|
+
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { extractMentions, parseMentionSegments } from './mentions.js';
|
|
2
|
+
import { extractMentions, parseMentionSegments, stripMarkdownCode } from './mentions.js';
|
|
3
3
|
|
|
4
4
|
describe('mentions utility', () => {
|
|
5
5
|
describe('extractMentions', () => {
|
|
@@ -37,6 +37,42 @@ describe('mentions utility', () => {
|
|
|
37
37
|
const text = 'Check with @turquoise. Then ping @john.doe! Also ask @coordinator, and @"Jane Doe".';
|
|
38
38
|
expect(extractMentions(text)).toEqual(['turquoise', 'john.doe', 'coordinator', 'Jane Doe']);
|
|
39
39
|
});
|
|
40
|
+
|
|
41
|
+
it('suppresses mentions inside fenced code blocks and inline backticks (UCH-128, UCH-138)', () => {
|
|
42
|
+
const markdown = `
|
|
43
|
+
Hey @alice, please review this test output:
|
|
44
|
+
\`\`\`ts
|
|
45
|
+
import { describe } from '@vitest/runner';
|
|
46
|
+
// mention in code comment: @bob
|
|
47
|
+
const handler = () => '@charlie';
|
|
48
|
+
\`\`\`
|
|
49
|
+
Also do not match \`@david\` in inline backticks or ~~~@eva~~~ in tilde fences.
|
|
50
|
+
Only real mentions like @frank should match.
|
|
51
|
+
`;
|
|
52
|
+
expect(extractMentions(markdown)).toEqual(['alice', 'frank']);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('suppresses mentions inside HTML <pre> and <code> blocks', () => {
|
|
56
|
+
const html = 'Hello @alice! Check <pre>npm i @types/node</pre> and <code>@bob</code>. Thanks @charlie!';
|
|
57
|
+
expect(extractMentions(html)).toEqual(['alice', 'charlie']);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('suppresses handles inside URLs and markdown link destinations', () => {
|
|
61
|
+
const text = 'Visit https://github.com/@org/repo and [profile](https://example.com/@jack) then ping @jack';
|
|
62
|
+
expect(extractMentions(text)).toEqual(['jack']);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('preserves rich text HTML mention nodes while ignoring attributes', () => {
|
|
66
|
+
const html = '<p>Assigned to <span data-type="mention" data-label="Jane Doe" data-mention-suggestion-char="@">@Jane Doe</span> and ping @coordinator</p>';
|
|
67
|
+
expect(extractMentions(html)).toEqual(['Jane Doe', 'coordinator']);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('stripMarkdownCode', () => {
|
|
72
|
+
it('strips code blocks, tilde blocks, inline backticks, pre and code tags', () => {
|
|
73
|
+
const raw = 'Before ```code @foo``` and `inline @bar` and <pre>pre @baz</pre> and <code>c @qux</code> after @real';
|
|
74
|
+
expect(stripMarkdownCode(raw)).toBe('Before and and and after @real');
|
|
75
|
+
});
|
|
40
76
|
});
|
|
41
77
|
|
|
42
78
|
describe('parseMentionSegments', () => {
|
package/src/utils/mentions.ts
CHANGED
|
@@ -6,29 +6,72 @@ export interface MentionSegment {
|
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Regex matching mentions:
|
|
9
|
-
* - Quoted mentions: @"Jane Doe"
|
|
10
|
-
* - Standard
|
|
11
|
-
*
|
|
9
|
+
* - Quoted mentions: @"Jane Doe", "Jane Doe"
|
|
10
|
+
* - Standard handles: @planner, @user-1, @john.smith
|
|
11
|
+
* Unquoted handles exclude trailing punctuation (e.g. @turquoise. matches handle "turquoise" without the dot).
|
|
12
12
|
*/
|
|
13
|
-
export const MENTION_REGEX =
|
|
13
|
+
export const MENTION_REGEX =
|
|
14
|
+
/(?:^|[\s>(*_"';\\/[\]{}~`:]|"|>|<|&)@(?:"([^"]+)"|"([^&]+)"|([a-zA-Z0-9_-]+(?:\.[a-zA-Z0-9_-]+)*))/g;
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
17
|
+
* Strips code blocks and inline code from markdown or HTML content:
|
|
18
|
+
* - Fenced code blocks: ```...``` and ~~~...~~~
|
|
19
|
+
* - Inline backtick code: `...`
|
|
20
|
+
* - HTML code/pre tags: <pre>...</pre>, <code>...</code>
|
|
21
|
+
* Used to avoid false-positive mention parsing within code snippets, terminal logs, or stack traces.
|
|
22
|
+
*/
|
|
23
|
+
export function stripMarkdownCode(content: string): string {
|
|
24
|
+
if (!content || typeof content !== 'string') return '';
|
|
25
|
+
return content
|
|
26
|
+
.replace(/```[\s\S]*?```/g, '')
|
|
27
|
+
.replace(/~~~[\s\S]*?~~~/g, '')
|
|
28
|
+
.replace(/<pre[\s\S]*?<\/pre>/gi, '')
|
|
29
|
+
.replace(/<code[\s\S]*?<\/code>/gi, '')
|
|
30
|
+
.replace(/`[^`\n]*`/g, '');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Extracts unique mention handles from a text string, ignoring trailing punctuation,
|
|
35
|
+
* code blocks or inline code snippets, URLs, markdown link destinations, and pre-existing HTML mention nodes.
|
|
18
36
|
* Preserves the order of their first appearance.
|
|
19
37
|
*/
|
|
20
38
|
export function extractMentions(content: string): string[] {
|
|
21
39
|
if (!content || typeof content !== 'string') return [];
|
|
22
40
|
|
|
41
|
+
// Normalize rich text mention nodes (e.g. from TipTap or custom mention spans)
|
|
42
|
+
const normalizedMentionNodes = content.replace(
|
|
43
|
+
/<span\b[^>]*\b(?:data-type=["']mention["']|class=["'][^"']*pixerate-mention-node[^"']*["'])[^>]*>([\s\S]*?)<\/span>/gi,
|
|
44
|
+
(match, inner) => {
|
|
45
|
+
const labelMatch = match.match(/\bdata-label=["']([^"']+)["']/i);
|
|
46
|
+
if (labelMatch) {
|
|
47
|
+
const val = labelMatch[1].trim();
|
|
48
|
+
return val.includes(' ') ? ` @"${val}" ` : ` @${val} `;
|
|
49
|
+
}
|
|
50
|
+
const idMatch = match.match(/\bdata-id=["']([^"']+)["']/i);
|
|
51
|
+
if (idMatch) {
|
|
52
|
+
const val = idMatch[1].trim();
|
|
53
|
+
return val.includes(' ') ? ` @"${val}" ` : ` @${val} `;
|
|
54
|
+
}
|
|
55
|
+
return ` ${inner} `;
|
|
56
|
+
}
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const cleanContent = stripMarkdownCode(normalizedMentionNodes);
|
|
60
|
+
// Strip URLs and markdown link destinations so @package or @handles in URLs are not extracted as mentions
|
|
61
|
+
const withoutUrls = cleanContent
|
|
62
|
+
.replace(/(?:https?|ftp|file):\/\/[^\s<>()]+/gi, '')
|
|
63
|
+
.replace(/\]\([^)]*\)/g, ']');
|
|
64
|
+
// Strip remaining HTML tags
|
|
65
|
+
const textOnly = withoutUrls.replace(/<[^>]+>/g, ' ');
|
|
66
|
+
|
|
23
67
|
const mentions: string[] = [];
|
|
24
68
|
const seen = new Set<string>();
|
|
25
69
|
|
|
26
|
-
// Reset regex lastIndex
|
|
27
70
|
const regex = new RegExp(MENTION_REGEX.source, 'g');
|
|
28
71
|
let match: RegExpExecArray | null;
|
|
29
72
|
|
|
30
|
-
while ((match = regex.exec(
|
|
31
|
-
const handle = match[1] ?? match[2];
|
|
73
|
+
while ((match = regex.exec(textOnly)) !== null) {
|
|
74
|
+
const handle = (match[1] ?? match[2] ?? match[3] ?? '').trim();
|
|
32
75
|
if (handle && !seen.has(handle)) {
|
|
33
76
|
seen.add(handle);
|
|
34
77
|
mentions.push(handle);
|
|
@@ -52,12 +95,11 @@ export function parseMentionSegments(content: string): MentionSegment[] {
|
|
|
52
95
|
|
|
53
96
|
while ((match = regex.exec(content)) !== null) {
|
|
54
97
|
const fullMatch = match[0];
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
const
|
|
60
|
-
const matchStart = match.index + leadingWhitespace.length;
|
|
98
|
+
const atIndex = fullMatch.indexOf('@');
|
|
99
|
+
const prefix = atIndex > 0 ? fullMatch.slice(0, atIndex) : '';
|
|
100
|
+
const mentionToken = fullMatch.slice(atIndex);
|
|
101
|
+
const handle = match[1] ?? match[2] ?? match[3];
|
|
102
|
+
const matchStart = match.index + prefix.length;
|
|
61
103
|
|
|
62
104
|
// Push text before this mention if any
|
|
63
105
|
if (matchStart > lastIndex) {
|
|
@@ -74,7 +116,7 @@ export function parseMentionSegments(content: string): MentionSegment[] {
|
|
|
74
116
|
handle
|
|
75
117
|
});
|
|
76
118
|
|
|
77
|
-
lastIndex =
|
|
119
|
+
lastIndex = matchStart + mentionToken.length;
|
|
78
120
|
}
|
|
79
121
|
|
|
80
122
|
// Push any remaining text after the last match
|