-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilename-gen.ts
More file actions
42 lines (34 loc) · 1.19 KB
/
Copy pathfilename-gen.ts
File metadata and controls
42 lines (34 loc) · 1.19 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
const MAX_FILENAME_LENGTH = 100;
function toKebabCase(title: string): string {
return title
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/-{2,}/g, '-')
.replace(/^-+|-+$/g, '');
}
function truncateAtHyphen(kebab: string, maxLength: number): string {
if (kebab.length <= maxLength) {
return kebab;
}
const truncated = kebab.slice(0, maxLength);
const lastHyphen = truncated.lastIndexOf('-');
if (lastHyphen > 0) {
return truncated.slice(0, lastHyphen);
}
return truncated.replace(/-+$/, '');
}
export function generateFileName(title: string, type: 'entity' | 'concept' | 'source'): string {
const kebab = toKebabCase(title);
if (type === 'source') {
const now = new Date();
const yyyy = now.getFullYear();
const mm = String(now.getMonth() + 1).padStart(2, '0');
const dd = String(now.getDate()).padStart(2, '0');
const dateSuffix = `${yyyy}-${mm}-${dd}`;
const maxKebabLength = MAX_FILENAME_LENGTH - 18;
const truncatedKebab = truncateAtHyphen(kebab, maxKebabLength);
return `source-${truncatedKebab}-${dateSuffix}.md`;
}
const truncatedKebab = truncateAtHyphen(kebab, MAX_FILENAME_LENGTH);
return `${truncatedKebab}.md`;
}