2021-11-05 18:39:36 +01:00
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
import { fileURLToPath } from 'url'
|
|
|
|
import { icons } from 'feather-icons'
|
|
|
|
import Handlebars from 'handlebars'
|
|
|
|
import micromark from 'micromark'
|
|
|
|
import striptags from 'striptags'
|
2018-09-23 20:28:05 +02:00
|
|
|
|
2021-11-14 15:47:22 +01:00
|
|
|
const dirname =
|
|
|
|
typeof __dirname === 'string'
|
|
|
|
? __dirname
|
|
|
|
: path.dirname(fileURLToPath(import.meta.url))
|
2020-07-18 22:44:17 +02:00
|
|
|
const extname = '.hbs'
|
2021-11-14 15:47:22 +01:00
|
|
|
const partialsDir = path.join(dirname, 'partials')
|
2020-07-18 22:44:17 +02:00
|
|
|
|
|
|
|
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),
|
|
|
|
)
|
|
|
|
|
2021-03-03 19:42:34 +01:00
|
|
|
Handlebars.registerHelper('formatCountry', countryCode =>
|
|
|
|
Intl.DisplayNames
|
|
|
|
? new Intl.DisplayNames(['en'], { type: 'region' }).of(countryCode)
|
|
|
|
: countryCode,
|
|
|
|
)
|
|
|
|
|
2020-07-05 14:54:30 +02:00
|
|
|
Handlebars.registerHelper('formatDate', dateString =>
|
|
|
|
new Date(dateString).toLocaleDateString('en', {
|
2020-07-05 14:19:26 +02:00
|
|
|
month: 'short',
|
|
|
|
year: 'numeric',
|
2022-09-26 01:14:17 +02:00
|
|
|
timeZone: 'UTC',
|
2020-07-05 14:54:30 +02:00
|
|
|
}),
|
|
|
|
)
|
2018-09-23 20:28:05 +02:00
|
|
|
|
2021-03-03 19:42:34 +01:00
|
|
|
Handlebars.registerHelper('formatPhone', phone =>
|
|
|
|
phone.replace(/[^\d|+]+/g, ''),
|
|
|
|
)
|
|
|
|
|
2020-10-31 14:20:25 +01:00
|
|
|
Handlebars.registerHelper('formatURL', url =>
|
|
|
|
url.replace(/^(https?:|)\/\//, '').replace(/\/$/, ''),
|
|
|
|
)
|
|
|
|
|
2021-03-05 18:56:04 +01:00
|
|
|
Handlebars.registerHelper('icon', (name, fallback) =>
|
|
|
|
(icons[name.toLowerCase()] || icons[fallback.toLowerCase()]).toSvg({
|
|
|
|
width: 16,
|
|
|
|
height: 16,
|
|
|
|
}),
|
2021-03-05 18:50:45 +01:00
|
|
|
)
|
|
|
|
|
2021-10-04 21:05:51 +02:00
|
|
|
Handlebars.registerHelper('join', arr =>
|
|
|
|
Intl.ListFormat ? new Intl.ListFormat('en').format(arr) : arr.join(', '),
|
2020-07-05 14:54:30 +02:00
|
|
|
)
|
|
|
|
|
2020-11-01 15:03:16 +01:00
|
|
|
Handlebars.registerHelper('markdown', doc => micromark(doc))
|
|
|
|
|
2021-02-14 15:46:32 +01:00
|
|
|
Handlebars.registerHelper('stripTags', html => striptags(html))
|
|
|
|
|
2021-11-05 18:39:36 +01:00
|
|
|
export const pdfRenderOptions = { mediaType: 'print' }
|
2020-11-03 17:44:00 +01:00
|
|
|
|
2021-11-05 18:39:36 +01:00
|
|
|
export const render = resume => {
|
2021-11-14 15:47:22 +01:00
|
|
|
const template = fs.readFileSync(path.resolve(dirname, 'resume.hbs'), 'utf-8')
|
|
|
|
const css = fs.readFileSync(path.resolve(dirname, 'style.css'), 'utf-8')
|
2020-07-05 14:54:30 +02:00
|
|
|
|
|
|
|
return Handlebars.compile(template)({ css, resume })
|
|
|
|
}
|