-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocumentTitle.ts
More file actions
49 lines (44 loc) · 1.05 KB
/
Copy pathdocumentTitle.ts
File metadata and controls
49 lines (44 loc) · 1.05 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
const UNREAD_PREFIX = /^\(\*\)\s+/
const LEGACY_MENTION_PREFIX = /^\(\d+\s*@\)\s+/
const COMBINED_PREFIX = /^\(\d+\s*·\s*\d+\s*@\)\s+/
const COUNT_PREFIX = /^\(\d+\)\s+/
export function stripDocumentTitlePrefix(
title: string,
fallback = 'Decentra',
): string {
let cleaned = title.trim()
if (!cleaned) {
return fallback
}
let changed = true
while (changed) {
changed = false
for (const pattern of [
UNREAD_PREFIX,
COMBINED_PREFIX,
LEGACY_MENTION_PREFIX,
COUNT_PREFIX,
]) {
const next = cleaned.replace(pattern, '')
if (next !== cleaned) {
cleaned = next
changed = true
}
}
}
const result = cleaned.trim()
return result || fallback
}
export function buildDocumentTitlePrefix(hasUnread: boolean): string | null {
return hasUnread ? '(*)' : null
}
export function formatDocumentTitle(
baseTitle: string,
hasUnread: boolean,
): string {
const prefix = buildDocumentTitlePrefix(hasUnread)
if (!prefix) {
return baseTitle
}
return `${prefix} ${baseTitle}`
}