-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (67 loc) · 1.93 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
const fs = require('fs')
const path = require('path')
const parseCSV = require('csv-parse/lib/sync')
const ejs = require('ejs')
const { toScrapbox, parse } = require('html2sb-compiler')
const csvPath = process.argv[2]
if (!csvPath) {
console.log('Requires csv path name. Call: node index.js xxx.csv')
process.exit(1)
}
const projectName = path.basename(csvPath, '.csv')
console.log('Got projectName:', projectName)
const raw = fs.readFileSync(csvPath)
const rows = parseCSV(raw)
const template = fs.readFileSync(`${projectName}-template.ejs`, 'utf-8')
const scrapboxPages = []
const columnNames = rows.shift().map(c => c.trim())
console.log('Columns:', columnNames)
let rowNum = 0
let skipped = 0
let added = 0
for (const row of rows) {
const columns = {}
for (let i = 0; i < row.length; i++) {
let text = row[i]
const isHtml = /<\w+.*>/.test(text)
if (isHtml) {
try {
const parsed = parse(text)[0]
text = toScrapbox(parsed).lines.join('\n')
} catch (err) {
console.warn(err)
}
}
columns[columnNames[i]] = text
}
const text = ejs.render(template, columns)
const title = text.split('\n', 1)[0] // use first line
rowNum++
if (!title) {
console.warn(rowNum, 'skip: empty title')
skipped++
continue
}
added++
console.log(rowNum, title)
const page = { title, text }
scrapboxPages.push(page)
}
console.log('added', added, ', skipped', skipped)
saveScrapboxProject({ projectName, scrapboxPages })
function saveScrapboxProject ({ projectName, scrapboxPages }) {
const scrapboxProject = {
name: projectName,
displayName: projectName,
pages: scrapboxPages.map(({ title, text }) => {
return {
title: title,
lines: text.split('\n')
}
})
}
const json = JSON.stringify(scrapboxProject, null, 2)
const path = `${projectName}-scrapbox.json`
fs.writeFileSync(path, json)
console.log('wrote scrapbox json file at', path)
}