forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown.ts
More file actions
114 lines (99 loc) · 2.92 KB
/
markdown.ts
File metadata and controls
114 lines (99 loc) · 2.92 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* Copyright (c) 2023 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { groupBy } from "@util/array"
import { formatPeriodCommon } from "@util/time"
export const CLIENT_FILE_NAME = "clients_no_modify.md"
const CLIENT_FIELDS: MarkdownTableField<timer.backup.Client>[] = [
{
name: "Client Id",
formatter: r => r.id,
}, {
name: "Client Name",
formatter: r => r.name,
}, {
name: "Earliest Date",
formatter: r => r.minDate ?? '',
}, {
name: "Latest Date",
formatter: r => r.maxDate ?? '',
}
]
type Meta = {
version: number
ts: number
}
const CURRENT_VER = 1
function genMetaLine(): string {
const meta: Meta = { version: CURRENT_VER, ts: Date.now() }
return genJsonLine(meta)
}
function genJsonLine(data: any): string {
return `<!-- ${JSON.stringify(data)} -->`
}
export function convertClients2Markdown(clients: timer.backup.Client[]): string {
return genMarkdownTable(clients, CLIENT_FIELDS)
}
type CellFormatter<T> = string | number | ((row: T) => string | number)
type MarkdownTableField<T> = {
name: string
formatter: CellFormatter<T>
}
function genTableRow<T>(t: T, formatters: CellFormatter<T>[]): string {
const cells = formatters?.map(f => {
const str = typeof f === 'function'
? f?.(t)?.toString?.()?.replace('|', '|')
: f?.toString?.()
return str ?? '-'
})
return `|${cells.join('|')}|`
}
function genMarkdownTable<T>(list: T[], fields: MarkdownTableField<T>[]): string {
const lines: string[] = [
genMetaLine(),
genJsonLine(list),
]
list = list || []
fields = fields || []
if (fields.length) {
// header
lines.push(genTableRow(null, fields.map(f => f.name)))
lines.push(genTableRow(null, fields.map(_ => '----')))
const cellFormatters = fields.map(f => f.formatter)
lines.push(
...list.map(row => genTableRow(row, cellFormatters))
)
}
return lines.join('\n')
}
const ROW_FIELDS: MarkdownTableField<timer.core.Row>[] = [
{
name: "Date",
formatter: r => r.date,
}, {
name: "Domain/URL",
formatter: r => r.host,
}, {
name: "Focus Time",
formatter: r => formatPeriodCommon(r.focus || 0)
}, {
name: "Visit Count",
formatter: r => r.time || 0,
},
]
export function divideByDate(rows: timer.core.Row[]): { [date: string]: string } {
return groupBy(rows, row => row.date, list => genMarkdownTable(list, ROW_FIELDS))
}
export function parseData<T>(markdown: string | undefined | null): T | undefined {
if (!markdown) return undefined
let line2 = markdown?.split('\n')?.[1]
if (!line2) {
return
}
line2 = line2?.replace("<!-- ", "").replace("-->", "").trim()
if (!line2) return
return JSON.parse(line2)
}