-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·123 lines (96 loc) · 3.28 KB
/
index.js
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
115
116
117
118
119
120
121
122
123
#!/usr/bin/env node
const commandLineArgs = require('command-line-args')
const csv = require('csv-parser')
const fs = require('fs')
const clipboardy = require('clipboardy')
const smartquotes = require('smartquotes')
const stripUtm = require('strip-utm')
const quit = (message) => {
console.log(message)
process.exit(9)
}
const cliOptions = [
{name: 'input', defaultOption: true},
{name: 'format', defaultValue: 'markdown'},
{name: 'start', defaultValue: '1970-1-1'},
]
const cli = commandLineArgs(cliOptions)
try {
// Ensure we have an input file to parse
if (typeof cli.input === 'undefined') quit('You must specify an input file.')
if (!fs.existsSync(cli.input)) quit(`Input file ${cli.input} couldn't be found or doesn't exist.`)
// Prepare structured data
const links = {}
// Configure minimum date
const start = Date.parse(cli.start)
if (isNaN(start)) quit(`We couldn't parse ${cli.start} as a date.`)
console.log('Parsing ...')
// Open and parse CSV
fs.createReadStream(cli.input)
.pipe(csv())
.on('data', (data) => {
let item = {}
Object.keys(data).forEach((row) => {
item[row] = data[row]
})
item['Name'] = Object.values(item)[0]
let parsedCreatedDate = Date.parse(item['Created'])
if (isNaN(parsedCreatedDate) || parsedCreatedDate < start) return
if (item['Name'].trim().length === 0) return
// Punctuate title
if (!['.','!','?'].includes(item['Name'].charAt(item['Name'].length - 1))) item['Name'] += '.'
// Smart quotes for description
item['Summary'] = smartquotes(item['Summary'])
if (typeof links[data['Category']] === 'undefined') links[data['Category']] = {}
if (!Array.isArray(links[data['Category']][data['Tags']])) links[data['Category']][data['Tags']] = []
links[data['Category']][data['Tags']].push(item)
})
.on('end', () => {
let output = ''
const categories = Object.keys(links)
categories.sort()
categories.forEach((category) => {
if (category) {
switch (cli.format) {
case 'markdown':
output += `### ${category}\n\n`
break;
case 'html':
output += `<h3>${category}</h3>\n`
break;
}
}
const tags = Object.keys(links[category])
tags.sort()
tags.forEach((tag) => {
if (tag === 'Tags' || !Array.isArray(links[category][tag])) return
if (tag) {
switch (cli.format) {
case 'markdown':
output += `#### ${tag}\n\n`
break
case 'html':
output += `<h4>${tag}</h4>\n`
break
}
}
links[category][tag].forEach((item) => {
item.URL = stripUtm(item.URL)
switch (cli.format) {
case 'markdown':
output += `[${item.Name}](${item.URL}) ${item.Summary.trim()}\n\n`
break
case 'html':
output += `<p><a href="${item.URL}">${item.Name}</a> ${item.Summary.trim()}</p>\n`
break
}
})
clipboardy.writeSync(output)
console.log(output)
})
})
})
console.log('Done')
} catch (err) {
quit(err)
}