-
Notifications
You must be signed in to change notification settings - Fork 12
/
makepdfs.js
143 lines (128 loc) · 3.69 KB
/
makepdfs.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env node
const markdown_dir = process.env.INPUT_MARKDOWN_DIR;
const output_dir = process.env.INPUT_OUTPUT_DIR;
const media_dir = process.env.INPUT_MEDIA_DIR;
'use strict';
var fs = require( 'fs' );
var mddir = '/github/workspace/' + markdown_dir;
var dir = '/github/workspace/' + output_dir + '/';
var meddir = '/github/workspace' + media_dir + '/';
/*
* Show an error message
*/
function showErrorMessage(msg, error) {
console.log('ERROR: ' + msg);
if (error) {
console.log(error);
}
}
/*
* make html
*/
function makeHtml(data) {
try {
// read files that make up template
var style = fs.readFileSync("/styles/markdown.css", ).toString('utf-8') + fs.readFileSync("/styles/markdown-pdf.css", ).toString('utf-8');
var template = fs.readFileSync("/template/template.html").toString('utf-8');
// compile template
var mustache = require('mustache');
var view = {
style: style,
content: data
};
return mustache.render(template, view);
} catch (error) {
showErrorMessage('makeHtml()', error);
}
}
/*
* make PDF
*/
function makePdf(data,file) {
try {
file = file.replace('.md','');
const puppeteer = require('puppeteer');
const express = require('express');
(async () => {
//Create server for images
const app = express();
app.use(express.static(meddir));
server = app.listen(3000);
const browser = await puppeteer.launch( {
executablePath:'/node_modules/puppeteer/.local-chromium/linux-782078/chrome-linux/chrome',
args: [
'--headless',
'--no-sandbox',
'--disable-setuid-sandbox'
]
} )
const page = await browser.newPage();
await page.goto(data, {waitUntil: 'networkidle2'});
await page.pdf({
path: dir + file + '.pdf',
format: 'A4',
scale: .9,
displayHeaderFooter: true,
margin: {top: 100, bottom:100, right: '50', left: '50'},
footerTemplate: '<div class="pageNumber" style="font-size:8px;width:100%;text-align:center;"></div>',
headerTemplate: '<div class="date" style="font-size:8px;width:100%;text-align:center;"></div>'
});
await browser.close();
await server.close();
})();
} catch (error) {
showErrorMessage('makeHtml()', error);
}
}
function Slug(string) {
try {
var stg = encodeURI(string.trim()
.toLowerCase()
.replace(/[\]\[\!\"\#\$\%\&\'\(\)\*\+\,\.\/\:\;\<\=\>\?\@\\\^\_\{\|\}\~\`]/g, '')
.replace(/\s+/g, '-')
.replace(/^\-+/, '')
.replace(/\-+$/, ''));
return stg;
} catch (error) {
showErrorMessage('Slug()', error);
}
}
var path = require('path');
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
if (!fs.existsSync(meddir)){
fs.mkdirSync(meddir);
}
fs.readdir (mddir,function(err, files) {
for (let file of files) {
if (path.extname(file) == '.md') {
var text = fs.readFileSync(mddir + '/' + file).toString('utf-8');
var md = require('markdown-it')({
html: true,
breaks: true,
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
str = hljs.highlight(lang, str, true).value;
} catch (error) {
str = md.utils.escapeHtml(str);
showErrorMessage('markdown-it:highlight', error);
}
} else {
str = md.utils.escapeHtml(str);
}
return '<pre class="hljs"><code><div>' + str + '</div></code></pre>';
}
}).use(require('markdown-it-highlightjs'), { inline: true });
var options = {
slugify: Slug
}
md.use(require('markdown-it-named-headers'), options);
md.use(require("markdown-it-anchor"));
md.use(require("markdown-it-table-of-contents"));
var body = md.render(text);
makePdf('data:text/html;,' + encodeURIComponent(makeHtml(body)),file);
}
}
});