Repository navigation
Expand file tree
/
Copy pathcodexFileCitations.ts
More file actions
56 lines (47 loc) · 1.89 KB
/
Copy pathcodexFileCitations.ts
File metadata and controls
56 lines (47 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
export interface CodexFileCitationLink {
readonly path: string;
readonly href: string;
readonly label: string;
readonly lineRangeStart?: number;
}
export type CodexFileCitationAttributes = Readonly<Record<string, string | null | undefined>>;
function positiveInteger(value: string | null | undefined): number | undefined {
if (value === null || value === undefined || value.trim().length === 0) return undefined;
const parsed = Number(value);
return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : undefined;
}
function citationLabel(path: string): string {
const normalized = path.replaceAll("\\", "/").replace(/\/+$/, "");
return normalized.slice(normalized.lastIndexOf("/") + 1) || normalized || "File";
}
function markdownDestinationPath(path: string): string {
return path.replaceAll("%", "%25").replaceAll("#", "%23").replaceAll("?", "%3F");
}
export function resolveCodexFileCitationLink(
attributes: CodexFileCitationAttributes | null | undefined,
): CodexFileCitationLink | null {
const path = attributes?.path?.trim();
if (!path) return null;
const lineRangeStart = positiveInteger(attributes?.line_range_start);
const destinationPath = markdownDestinationPath(path);
return {
path,
href: lineRangeStart === undefined ? destinationPath : `${destinationPath}#L${lineRangeStart}`,
label: citationLabel(path),
...(lineRangeStart === undefined ? {} : { lineRangeStart }),
};
}
function markdownLabel(value: string): string {
return value.replace(/[\\[\]*_`<&]/g, "\\$&");
}
function markdownDestination(value: string): string {
return value
.replaceAll("\\", "%5C")
.replaceAll("<", "%3C")
.replaceAll(">", "%3E")
.replaceAll("\r", "%0D")
.replaceAll("\n", "%0A");
}
export function codexFileCitationMarkdown(citation: CodexFileCitationLink): string {
return `[${markdownLabel(citation.label)}](<${markdownDestination(citation.href)}>)`;
}