Skip to content

Commit 0144a8d

Browse files
committed
Switch to remarkable and highlight.js
1 parent 12cf907 commit 0144a8d

File tree

9 files changed

+59
-88
lines changed

9 files changed

+59
-88
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
coverage
2+
coverage
3+
.DS_Store

css/highlight.css

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdf.css css/pdf.css

File renamed without changes.

highlight.css

-61
This file was deleted.

lib/markdown-pdf.js index.js

+31-23
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
var fs = require("fs")
22
, path = require("path")
3+
, childProcess = require("child_process")
34
, through = require("through2")
45
, extend = require("extend")
5-
, marked = require("marked")
6-
, pygments = require('pygmentize-bundled')
6+
, Remarkable = require("remarkable")
7+
, hljs = require("highlight.js")
78
, tmp = require("tmp")
8-
, childProcess = require("child_process")
99
, duplexer = require("duplexer")
1010
, streamft = require("stream-from-to")
1111

@@ -16,8 +16,8 @@ function markdownpdf (opts) {
1616
opts.cwd = opts.cwd ? path.resolve(opts.cwd) : process.cwd()
1717
opts.phantomPath = opts.phantomPath || require("phantomjs").path
1818
opts.runningsPath = opts.runningsPath ? path.resolve(opts.runningsPath) : path.join(__dirname, "runnings.js")
19-
opts.cssPath = opts.cssPath ? path.resolve(opts.cssPath) : path.join(__dirname, "..", "pdf.css")
20-
opts.highlightCssPath = opts.highlightCssPath ? path.resolve(opts.highlightCssPath) : path.join(__dirname, "..", "highlight.css")
19+
opts.cssPath = opts.cssPath ? path.resolve(opts.cssPath) : path.join(__dirname, "css", "pdf.css")
20+
opts.highlightCssPath = opts.highlightCssPath ? path.resolve(opts.highlightCssPath) : path.join(__dirname, "css", "highlight.css")
2121
opts.paperFormat = opts.paperFormat || "A4"
2222
opts.paperOrientation = opts.paperOrientation || "portrait"
2323
opts.paperBorder = opts.paperBorder || "1cm"
@@ -27,29 +27,32 @@ function markdownpdf (opts) {
2727

2828
var md = ""
2929

30-
// Argh! marked, why you no stream?
3130
var mdToHtml = through(
32-
function transform (data, enc, cb) {
33-
md += data
31+
function transform (chunk, enc, cb) {
32+
md += chunk
3433
cb()
3534
},
3635
function flush (cb) {
3736
var self = this
3837

39-
// set options for marked
40-
var markedOptions = {
41-
highlight: function (code, lang, cb) {
42-
pygments({lang: lang, format: "html"}, code, function (er, result) {
43-
cb(null, result ? result.toString() : code)
44-
})
38+
var mdParser = new Remarkable(extend({
39+
highlight: function (str, lang) {
40+
if (lang && hljs.getLanguage(lang)) {
41+
try {
42+
return hljs.highlight(lang, str).value
43+
} catch (er) {}
44+
}
45+
46+
try {
47+
return hljs.highlightAuto(str).value
48+
} catch (er) {}
49+
50+
return ""
4551
}
46-
}
52+
}, opts.remarkable))
4753

48-
marked(md, markedOptions, function (er, html) {
49-
self.push(html)
50-
self.push(null)
51-
cb()
52-
})
54+
self.push(mdParser.render(md))
55+
self.push(null)
5356
}
5457
)
5558

@@ -74,7 +77,7 @@ function markdownpdf (opts) {
7477
htmlToTmpHtmlFile.on("finish", function () {
7578
// Invoke phantom to generate the PDF
7679
var childArgs = [
77-
path.join(__dirname, "..", "lib-phantom", "markdown-pdf.js")
80+
path.join(__dirname, "phantom", "render.js")
7881
, tmpHtmlPath
7982
, tmpPdfPath
8083
, opts.cwd
@@ -87,7 +90,7 @@ function markdownpdf (opts) {
8790
, opts.renderDelay
8891
]
8992

90-
childProcess.execFile(opts.phantomPath, childArgs, function(er, stdout, stderr) {
93+
childProcess.execFile(opts.phantomPath, childArgs, function (er, stdout, stderr) {
9194
//if (stdout) console.log(stdout)
9295
//if (stderr) console.error(stderr)
9396
if (er) return outputStream.emit("error", er)
@@ -96,7 +99,12 @@ function markdownpdf (opts) {
9699
})
97100

98101
// Setup the pipeline
99-
inputStream.pipe(opts.preProcessMd()).pipe(mdToHtml).pipe(opts.preProcessHtml()).pipe(htmlToTmpHtmlFile)
102+
inputStream
103+
.pipe(opts.preProcessMd())
104+
.pipe(mdToHtml)
105+
.pipe(opts.preProcessHtml())
106+
.pipe(htmlToTmpHtmlFile)
107+
100108
inputStream.resume()
101109
})
102110
})

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "markdown-pdf",
33
"version": "4.0.1",
44
"description": "Markdown to PDF converter",
5-
"main": "lib/markdown-pdf.js",
5+
"main": "index.js",
66
"scripts": {
77
"test": "istanbul cover node_modules/.bin/tape test/*.js",
88
"coveralls": "cat ./coverage/lcov.info | coveralls"
@@ -29,9 +29,9 @@
2929
"commander": "^2.2.0",
3030
"duplexer": "^0.1.1",
3131
"extend": "^2.0.0",
32-
"marked": "^0.3.2",
32+
"highlight.js": "^8.4.0",
3333
"phantomjs": "^1.9.13",
34-
"pygmentize-bundled": "^2.2.0",
34+
"remarkable": "^1.6.0",
3535
"stream-from-to": "^1.4.0",
3636
"through2": "^0.6.3",
3737
"tmp": "0.0.24"
File renamed without changes.

lib/runnings.js runnings.js

File renamed without changes.

test/fixtures/ipsum.md

+22
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,28 @@ This is a image
5959
And more. Sugar plum lemon drops chupa chups chocolate pastry. Faworki applicake applicake brownie topping liquorice chocolate liquorice icing. Cake pudding pudding cake candy sugar plum soufflé.`
6060

6161

62+
```js
63+
var Remarkable = require('remarkable');
64+
var hljs = require('highlight.js') // https://highlightjs.org/
65+
66+
// Actual default values
67+
var md = new Remarkable({
68+
highlight: function (str, lang) {
69+
if (lang && hljs.getLanguage(lang)) {
70+
try {
71+
return hljs.highlight(lang, str).value;
72+
} catch (err) {}
73+
}
74+
75+
try {
76+
return hljs.highlightAuto(str).value;
77+
} catch (err) {}
78+
79+
return ''; // use external default escaping
80+
}
81+
});
82+
```
83+
6284

6385
And that's all ...
6486
This is an **extremely simple and basic** guide. To understand a lot better this kind of dark magic called markdown, logically I recommend the official wiki of the creator. Go, run, jump to [the site of John Gruber](http://daringfireball.net/projects/markdown/).

0 commit comments

Comments
 (0)