-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (77 loc) · 2.5 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
const fs = require('fs')
const path = require('path')
const { icons } = require('feather-icons')
const Handlebars = require('handlebars')
const micromark = require('micromark')
const striptags = require('striptags')
const postcss = require('postcss')
const inlineBase64 = require('postcss-inline-base64')
const extname = '.hbs'
const partialsDir = path.join(__dirname, 'partials')
fs.readdirSync(partialsDir)
.filter(filename => path.extname(filename) === extname)
.map(filename => [
filename,
fs.readFileSync(path.join(partialsDir, filename), 'utf8')
])
.forEach(([filename, template]) =>
Handlebars.registerPartial(path.basename(filename, extname), template)
)
Handlebars.registerHelper('join', arr =>
arr.join(', ')
)
Handlebars.registerHelper('levelToNumber', level => {
// anything less than 5 shouldn't be listed as a skill. more of a passing
// curiosity
if (level === 'Master') {
return 10
} else if (level === 'Senior') {
return 9
} else if (level === 'Junior') {
return 8
} else if (level === 'Novice') {
return 6
} else {
return 5
}
})
function regionNameToEnglish (countryCode) {
return Intl.DisplayNames
? new Intl.DisplayNames(['en'], { type: 'region' }).of(countryCode)
: countryCode
}
Handlebars.registerHelper('formatRegion', (region, countryCode) =>
countryCode === 'US'
? region
: regionNameToEnglish(countryCode)
)
Handlebars.registerHelper('formatDate', dateString =>
new Date(dateString).toLocaleDateString('en', {
month: 'short',
year: 'numeric'
})
)
Handlebars.registerHelper('formatPhone', phone =>
phone.replace(/[^\d|+]+/g, '')
)
Handlebars.registerHelper('formatURL', url =>
url.replace(/^(https?:|)\/\//, '').replace(/\/$/, '')
)
Handlebars.registerHelper('icon', (name, fallback) =>
(icons[name.toLowerCase()] || icons[fallback.toLowerCase()]).toSvg({
width: 16,
height: 16
})
)
Handlebars.registerHelper('join', (arr, separator) =>
arr.join(typeof separator === 'string' ? separator : ', ')
)
Handlebars.registerHelper('markdown', doc => micromark(doc))
Handlebars.registerHelper('stripTags', html => striptags(html))
module.exports.pdfRenderOptions = { mediaType: 'print' }
module.exports.render = async (resume) => {
const template = fs.readFileSync(path.join(__dirname, 'resume.hbs'), 'utf-8')
let css = fs.readFileSync(path.join(__dirname, 'style.css'), 'utf-8')
css = await postcss([inlineBase64({ baseDir: __dirname })]).process(css)
return Handlebars.compile(template)({ css, resume })
}