Skip to content

Commit a0d1a59

Browse files
committed
Added minification of CSS/HTML and using in RAM handling of HTML if not stored to disk
1 parent 557ff98 commit a0d1a59

File tree

4 files changed

+103
-17
lines changed

4 files changed

+103
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@ This tools uses [markdown-toc](https://github.com/jonschlinkert/markdown-toc) in
4949

5050
Convert markdown to PDF (and also HTML) with optional table of content.
5151

52-
Inspired by [MDPDF](https://www.npmjs.com/package/mdpdf) and [markdown-pdf](https://www.npmjs.com/package/markdown-pdf). With the improvement that you can generate a table of content (ToC) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc), and the ToC document links works correctly in the PDF even if non-latin-letter like åäö are used.
52+
Inspired by [MDPDF](https://www.npmjs.com/package/mdpdf) and [markdown-pdf](https://www.npmjs.com/package/markdown-pdf). With the improvement that you can generate a table of content (ToC) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc), and the ToC document links works correctly in the PDF even if non-latin-letter like åäö are used. Another improvement that is made compared to MDPDF is that the intermediate temporary HTML is never stored as a file unless you want it to be saved. If you choose to store the HTML it is minified before stored to disk.

bin/markdown-to-pdf.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const cli = meow(
3232
--toc Add table of content where a "toc" html comment is.
3333
--maxdepth=<depth> TOC: Use headings whose depth is at most maxdepth (default: 6)
3434
35-
--keep-html=<file> Keep intermediate HTML to file. if <file> is empty it follows the
35+
--save-html=<file> Save intermediate HTML to file. if no filename is given use <source>.pdf
3636
3737
--help Display this menu
3838
--version Display the application version
@@ -53,7 +53,7 @@ const cli = meow(
5353
type: 'string',
5454
default: '20mm',
5555
},
56-
keepHtml: {
56+
saveHtml: {
5757
type: 'string',
5858
},
5959
format: {
@@ -135,9 +135,9 @@ if (!/^(A[0-6]|Legal|Ledger|Letter|Tabloid)$/.test(format)) {
135135

136136
const addToc = cli.flags.toc
137137

138-
const keepHtml = cli.flags.keepHtml !== undefined
138+
const saveHtml = cli.flags.saveHtml !== undefined
139139

140-
const htmlFile = cli.flags.keepHtml || destination.replace(/\.pdf/, '.html')
140+
const htmlFile = cli.flags.saveHtml || destination.replace(/\.pdf/, '.html')
141141

142142
console.log(htmlFile)
143143

@@ -153,7 +153,7 @@ const cliOptions = {
153153
margin,
154154
format,
155155
addToc,
156-
keepHtml,
156+
saveHtml,
157157
htmlFile,
158158
}
159159

@@ -209,7 +209,7 @@ function markdownToHtml(markdown) {
209209
.render(markdown)
210210
}
211211

212-
function createPdf(htmlPath, pdfPath, options) {
212+
function createPdf(html, pdfPath, options) {
213213
// Write html to a temp file
214214
let browser
215215
let page
@@ -222,7 +222,7 @@ function createPdf(htmlPath, pdfPath, options) {
222222
})
223223
.then(p => {
224224
page = p
225-
return page.goto('file:' + path.resolve(htmlPath), {
225+
return page.setContent(html, {
226226
waitUntil: 'networkidle2',
227227
})
228228
})
@@ -246,7 +246,6 @@ function createPdf(htmlPath, pdfPath, options) {
246246

247247
let markdown = fs.readFileSync(source, 'utf-8')
248248

249-
// TODO: compress CSS
250249
const cssStyleFiles = [
251250
path.join(__dirname, '../styles/github-markdown-css.css'),
252251
path.join(__dirname, '../styles/default.css'),
@@ -266,7 +265,7 @@ if (addToc) {
266265

267266
const htmlContent = markdownToHtml(markdown)
268267

269-
const html = `
268+
let html = `
270269
<!DOCTYPE html>
271270
<html>
272271
<head>
@@ -279,14 +278,19 @@ const html = `
279278
</html>
280279
`
281280

282-
// TODO: html in memory if possible?
283-
fs.writeFileSync(htmlFile, html)
281+
if (saveHtml) {
282+
// Make sure that the HTML is as small ass possible before saving it
283+
const minify = require('html-minifier').minify
284284

285-
createPdf(htmlFile, destination, {
285+
html = minify(html, {
286+
removeAttributeQuotes: true,
287+
minifyCSS: true,
288+
})
289+
290+
fs.writeFileSync(htmlFile, html)
291+
}
292+
293+
createPdf(html, destination, {
286294
margin,
287295
format,
288-
}).then(() => {
289-
if (!keepHtml) {
290-
fs.unlinkSync(htmlFile)
291-
}
292296
})

package-lock.json

Lines changed: 81 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"homepage": "https://github.com/hacker112/markdown-utilities#readme",
1919
"dependencies": {
2020
"highlight.js": "^9.16.2",
21+
"html-minifier": "^4.0.0",
2122
"markdown-toc": "^1.2.0",
2223
"meow": "^5.0.0",
2324
"puppeteer": "^2.0.0",

0 commit comments

Comments
 (0)