-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.js
More file actions
49 lines (43 loc) · 1.57 KB
/
render.js
File metadata and controls
49 lines (43 loc) · 1.57 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
const fs = require('fs').promises;
const fm = require('front-matter');
const marked = require('marked');
const Handlebars = require('handlebars');
const puppeteer = require('puppeteer');
const CV_SOURCE = './src/cv.md';
const CV_TEMPLATE = './src/template.hb';
const CV_STYLES = './src/styles.css';
const ROOT = 'https://dprgarner.github.io/cv';
async function renderHtml({ livereloadPort = null, isPdf = false } = {}) {
const { attributes, body } = fm(await fs.readFile(CV_SOURCE, 'utf8'));
const markdown = marked(body);
const description = marked(attributes.description);
const context = { ...attributes, markdown, description };
if (livereloadPort) {
const livereloadHost = 'localhost';
context.livereload = `
<script src="http://${livereloadHost}:${livereloadPort}/livereload.js?snipver=1"></script>
`;
}
const template = Handlebars.compile(await fs.readFile(CV_TEMPLATE, 'utf8'));
context.styles = await fs.readFile(CV_STYLES);
context.cvHtmlPath = `${ROOT}`;
context.cvPdfPath = `${ROOT}/cv.pdf`;
context.pdf = isPdf;
const html = template(context);
return html.replace(/–/g, '–').replace(/’/g, '’');
}
exports.renderHtml = renderHtml;
async function renderPdf(html, outputPath) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(html);
await page.evaluateHandle('document.fonts.ready');
await page.emulateMedia('print');
await page.pdf({
path: outputPath,
format: 'A4',
printBackground: true,
});
await browser.close();
}
exports.renderPdf = renderPdf;