lintry

封装显示markdown的模块

...@@ -12,7 +12,8 @@ const configure = customize({ ...@@ -12,7 +12,8 @@ const configure = customize({
12 "md": { 12 "md": {
13 "base_path": "md", 13 "base_path": "md",
14 "default_md": "readme.md", 14 "default_md": "readme.md",
15 - "theme": "metro-lake" 15 + "theme": "metro-lake",
16 + "toc": true
16 } 17 }
17 }); 18 });
18 19
......
1 +/**
2 + * view-markdown
3 + * Created by lintry on 2018/1/9.
4 + */
5 +
6 +const path = require('path');
7 +
8 +class ViewMarkdown {
9 + constructor (options) {
10 + this.options = options || {};
11 + if (!this.options.base_path) {
12 + throw new Error('no base path for markdown')
13 + }
14 + }
15 +
16 + render (filename, theme) {
17 + const options = this.options;
18 + //默认md
19 + if (!filename || filename.match(/.+\/$/)) {
20 + filename = (filename || '') + options.default_md || 'readme.md'
21 + }
22 + let file = path.resolve(options.base_path, filename);
23 + if (!path.parse(file).ext) {
24 + //追加后缀
25 + file += '.md'
26 + }
27 + const mdview = new (require('./mdview'))({parseTokens: true, toc: options.toc});
28 + let result = mdview.renderFile(file);
29 + let tittle = result.tokens && result.tokens[1] && result.tokens[1].content || filename;
30 + theme = theme || options.theme;
31 + return `
32 + <!DOCTYPE html>
33 + <html>
34 + <head>
35 + <meta charset=utf-8>
36 + <title>${tittle}</title>
37 + <meta name="viewport" content="width=device-width, initial-scale=1.0">
38 + <link rel="stylesheet" href="/markdown.css">
39 + <link rel="stylesheet" href="/${theme}/${theme}.css">
40 + </head>
41 + <body>
42 + <div class='markdown ${theme}'>
43 + ${result.html}
44 + </div>
45 + </body
46 + </html>
47 +`;
48 + }
49 +}
50 +
51 +module.exports = ViewMarkdown
...\ No newline at end of file ...\ No newline at end of file
...@@ -3,54 +3,24 @@ const app = express(); ...@@ -3,54 +3,24 @@ const app = express();
3 3
4 const config = require('./init/config'); 4 const config = require('./init/config');
5 5
6 -const mdview = new (require('./lib/mdview'))({parseTokens: true, toc: true}), 6 +const path = require('path');
7 - path = require('path');
8 7
9 const MARKDOWN_FILE_BASE = path.resolve(process.cwd(), config.md.base_path); 8 const MARKDOWN_FILE_BASE = path.resolve(process.cwd(), config.md.base_path);
10 9
11 app.use(express.static(__dirname + '/themes')); 10 app.use(express.static(__dirname + '/themes'));
12 app.use(express.static(MARKDOWN_FILE_BASE)); 11 app.use(express.static(MARKDOWN_FILE_BASE));
13 12
14 -function sendTemplate (filename, theme) { 13 +function viewMarkdown (req, res, next) {
15 - //默认md
16 - if (!filename || filename.match(/.+\/$/)) {
17 - filename = (filename || '') + config.md.default_md
18 - }
19 - let file = path.resolve(MARKDOWN_FILE_BASE, filename);
20 - if (!path.parse(file).ext) {
21 - //追加后缀
22 - file += '.md'
23 - }
24 - let result = mdview.renderFile(file);
25 - let tittle = result.tokens && result.tokens[1] && result.tokens[1].content || filename;
26 - theme = theme || config.md.theme;
27 - return `
28 - <!DOCTYPE html>
29 - <html>
30 - <head>
31 - <meta charset=utf-8>
32 - <title>${tittle}</title>
33 - <meta name="viewport" content="width=device-width, initial-scale=1.0">
34 - <link rel="stylesheet" href="/markdown.css">
35 - <link rel="stylesheet" href="/${theme}/${theme}.css">
36 - </head>
37 - <body>
38 - <div class='markdown ${theme}'>
39 - ${result.html}
40 - </div>
41 - </body
42 - </html>
43 -`;
44 -}
45 -
46 -app.get('/**', function (req, res, next) {
47 let url = req.url, urls = url.match(/^\/([\/\w]+)/) || [], query = req.query; 14 let url = req.url, urls = url.match(/^\/([\/\w]+)/) || [], query = req.query;
48 let ext = path.parse(url).ext; 15 let ext = path.parse(url).ext;
49 if (ext && ext !== 'md') { 16 if (ext && ext !== 'md') {
50 return next(); 17 return next();
51 } 18 }
52 - res.send(sendTemplate(urls[1], query.t)); 19 + const viewer = new (require('./lib/view-markdown'))(config.md);
53 -}) 20 + res.send(viewer.render(urls[1], query.t));
21 +}
22 +
23 +app.get('/*', viewMarkdown);
54 24
55 const PORT = config.system.bind_port; 25 const PORT = config.system.bind_port;
56 app.listen(PORT); 26 app.listen(PORT);
......