Skip to content

Commit a5643b1

Browse files
committed
init
1 parent a1e759a commit a5643b1

File tree

8 files changed

+1918
-0
lines changed

8 files changed

+1918
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
11
# markdown2html-cli
22
markdown to html use gulp ,support auto toc , syntax highlight.
3+
4+
## How to use?
5+
move to the root dir of project, rename your md file to index.md, run:
6+
```
7+
npm install
8+
```
9+
then run:
10+
```
11+
gulp tohtml
12+
```
13+
now please see dist/index.html

dist/index.html

+684
Large diffs are not rendered by default.

gulpfile.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
var gulp = require('gulp');
2+
var markdown = require('gulp-markdown');
3+
var livereload = require('gulp-livereload');
4+
var renderer = new markdown.marked.Renderer();
5+
var map = require('map-stream');
6+
var hljs = require('./highlight.min.js');
7+
8+
var tocmodel = [];
9+
var toplevel = null;
10+
11+
function pushLevel(model, level, escapedText) {
12+
if (model[model.length - 1].level == level) {
13+
model.push({ level: level, name: escapedText, sub: [] });
14+
} else {
15+
var parentLevel = model[model.length - 1].level;
16+
var sub = model[model.length - 1].sub;
17+
if (parentLevel + 1 < level && sub.length == 0) {
18+
console.log('不支持跳级,请按层级结构定义!!!');
19+
return;
20+
}
21+
if (sub.length == 0 || sub[sub.length - 1].level == level) { //sub为空或者与sub中元素同级,直接添加。
22+
sub.push({ level: level, name: escapedText, sub: [] });
23+
} else {
24+
pushLevel(sub, level, escapedText);
25+
}
26+
}
27+
}
28+
29+
function fmtToc(model) {
30+
var tmp = '<ol class="order">'
31+
if (model.length > 0) {
32+
model.forEach(function(e, i) {
33+
var tt = '<li class="order"><a href="#' + e.name + '" >' + e.name + '</a>';
34+
if (e.sub.length > 0) {
35+
tt = tt + fmtToc(e.sub) + '</li>';
36+
} else {
37+
tt = tt + '</li>';
38+
}
39+
tmp = tmp + tt;
40+
});
41+
}
42+
return tmp + '</ol>';
43+
}
44+
renderer.heading = function(text, level) {
45+
var escapedText = text.toLowerCase().replace(/[^a-zA-Z\u4e00-\u9fa5]+/g, '-');
46+
if (level > 1) {
47+
//level==1当作题目,忽略
48+
if (!toplevel || tocmodel.length == 0) {
49+
toplevel = level
50+
tocmodel.push({ level: level, name: escapedText, sub: [] });
51+
} else {
52+
pushLevel(tocmodel, level, escapedText);
53+
}
54+
}
55+
return '<h' + level + '><a name="' +
56+
escapedText +
57+
'" class="anchor" href="#' +
58+
escapedText +
59+
'"><span class="header-link"></span></a>' +
60+
text + '</h' + level + '>';
61+
}
62+
63+
var options = {
64+
highlight: function(code) {
65+
return hljs.highlightAuto(code).value;
66+
},
67+
renderer: renderer
68+
}
69+
70+
71+
gulp.task('tohtml', function() {
72+
return gulp.src('index.md')
73+
.pipe(markdown(options))
74+
.pipe(map(function(file, cb) {
75+
var fileContents = file.contents.toString();
76+
fileContents = '<!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title>index</title>' +
77+
'<link rel="stylesheet" href="https://cdn.bootcss.com/highlight.js/9.12.0/styles/vs.min.css"><script src="https://cdn.bootcss.com/highlight.js/9.12.0/highlight.min.js"></script><script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>' +
78+
'<style>.code{padding: 2px 4px;font-size: 90%;color: #c7254e;background-color: #f9f2f4;border-radius: 4px;} .post{ margin: 0 auto;padding-top: 20px;padding-bottom: 60px;width: 960px;}' +
79+
' ol.order{ counter-reset: item } li.order{ display: block } li.order:before { content: counters(item, ".") " "; counter-increment: item } ' +
80+
' .toc{position:fixed;width:400px;left:20px;top:20px;bottom:20px;height 600px;overflow-y:scroll;}' +
81+
'</style>' +
82+
'</head><body><div class="toc"><h3>目录:</h3>' + fmtToc(tocmodel) + '</div><div class="post">' +
83+
fileContents +
84+
'</div><script>hljs.initHighlightingOnLoad();$("li>code,p > code").addClass("code");' +
85+
'</script></body></html>';
86+
file.contents = new Buffer(fileContents);
87+
//清空历史数据
88+
tocmodel = [];
89+
toplevel = null;
90+
91+
cb(null, file);
92+
}))
93+
.pipe(gulp.dest('dist'))
94+
.pipe(livereload());
95+
});
96+
97+
gulp.task('watch', ['tohtml'], function() {
98+
gulp.watch('./*.md', ['tohtml']);
99+
100+
livereload.listen();
101+
/* gulp.watch(['dist/**'], function() {
102+
livereload();
103+
}); */
104+
});

highlight.min.js

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

index.js

Whitespace-only changes.

0 commit comments

Comments
 (0)