@zigai/pi-mention-project 0.2.1 → 0.2.2
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 +1 -1
- package/src/expand-mentions.ts +23 -81
package/package.json
CHANGED
package/src/expand-mentions.ts
CHANGED
|
@@ -7,31 +7,6 @@ import {
|
|
|
7
7
|
} from "./mention-syntax.ts";
|
|
8
8
|
import type { ProjectDirectory } from "./types.ts";
|
|
9
9
|
|
|
10
|
-
function escapeXmlText(value: string): string {
|
|
11
|
-
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function escapeXmlAttribute(value: string): string {
|
|
15
|
-
return escapeXmlText(value).replace(/"/g, """);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function formatProjectBlock(project: ProjectDirectory): string {
|
|
19
|
-
const name = escapeXmlAttribute(project.name);
|
|
20
|
-
const projectPath = escapeXmlAttribute(project.path);
|
|
21
|
-
const root = escapeXmlAttribute(project.root);
|
|
22
|
-
return `<project name="${name}" path="${projectPath}" root="${root}">\nDirectory: ${escapeXmlText(project.path)}\n</project>`;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function formatCombinedProjectBlock(projects: ProjectDirectory[]): string {
|
|
26
|
-
if (projects.length === 1) {
|
|
27
|
-
const project = projects[0];
|
|
28
|
-
if (project !== undefined) return formatProjectBlock(project);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const content = projects.map((project) => formatProjectBlock(project)).join("\n\n");
|
|
32
|
-
return `<projects>\n${content}\n</projects>`;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
10
|
function projectMap(projects: ProjectDirectory[]): Map<string, ProjectDirectory> {
|
|
36
11
|
const byName = new Map<string, ProjectDirectory>();
|
|
37
12
|
for (const project of projects) {
|
|
@@ -41,69 +16,36 @@ function projectMap(projects: ProjectDirectory[]): Map<string, ProjectDirectory>
|
|
|
41
16
|
return byName;
|
|
42
17
|
}
|
|
43
18
|
|
|
44
|
-
function mentionedProjectNames(
|
|
45
|
-
text: string,
|
|
46
|
-
projects: ProjectDirectory[],
|
|
47
|
-
trigger: string,
|
|
48
|
-
): Set<string> {
|
|
49
|
-
const names = new Set<string>();
|
|
50
|
-
const knownNames = projectNameSet(projects);
|
|
51
|
-
|
|
52
|
-
for (const match of text.matchAll(projectMentionPattern(trigger))) {
|
|
53
|
-
const parsed = parseProjectMentionName(match[2], match[3], knownNames);
|
|
54
|
-
if (parsed !== undefined) {
|
|
55
|
-
names.add(parsed.name);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return names;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function removeProjectMentionSigils(
|
|
63
|
-
text: string,
|
|
64
|
-
projects: ProjectDirectory[],
|
|
65
|
-
trigger: string,
|
|
66
|
-
): string {
|
|
67
|
-
const knownNames = projectNameSet(projects);
|
|
68
|
-
return text
|
|
69
|
-
.replace(
|
|
70
|
-
projectMentionPattern(trigger),
|
|
71
|
-
(
|
|
72
|
-
match: string,
|
|
73
|
-
leading: string,
|
|
74
|
-
quotedName: string | undefined,
|
|
75
|
-
unquotedName: string | undefined,
|
|
76
|
-
) => {
|
|
77
|
-
const parsed = parseProjectMentionName(quotedName, unquotedName, knownNames);
|
|
78
|
-
if (parsed === undefined) return match;
|
|
79
|
-
return `${leading}${parsed.name}${parsed.suffix}`;
|
|
80
|
-
},
|
|
81
|
-
)
|
|
82
|
-
.trim();
|
|
83
|
-
}
|
|
84
|
-
|
|
85
19
|
export function expandProjectMentions(
|
|
86
20
|
text: string,
|
|
87
21
|
projects: ProjectDirectory[],
|
|
88
22
|
trigger: string,
|
|
89
23
|
): string {
|
|
90
24
|
const byName = projectMap(projects);
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const loaded: ProjectDirectory[] = [];
|
|
95
|
-
for (const name of names) {
|
|
96
|
-
const project = byName.get(name);
|
|
97
|
-
if (project !== undefined) {
|
|
98
|
-
loaded.push(project);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
if (loaded.length === 0) return text;
|
|
25
|
+
const knownNames = projectNameSet(projects);
|
|
26
|
+
let changed = false;
|
|
102
27
|
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
28
|
+
const expanded = text.replace(
|
|
29
|
+
projectMentionPattern(trigger),
|
|
30
|
+
(
|
|
31
|
+
match: string,
|
|
32
|
+
leading: string,
|
|
33
|
+
quotedName: string | undefined,
|
|
34
|
+
unquotedName: string | undefined,
|
|
35
|
+
) => {
|
|
36
|
+
const parsed = parseProjectMentionName(quotedName, unquotedName, knownNames);
|
|
37
|
+
if (parsed === undefined) return match;
|
|
38
|
+
|
|
39
|
+
const project = byName.get(parsed.name);
|
|
40
|
+
if (project === undefined) return match;
|
|
41
|
+
|
|
42
|
+
changed = true;
|
|
43
|
+
return `${leading}${project.path}${parsed.suffix}`;
|
|
44
|
+
},
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
if (!changed) return text;
|
|
48
|
+
return expanded;
|
|
107
49
|
}
|
|
108
50
|
|
|
109
51
|
type ContextMessage = ContextEvent["messages"][number];
|