Showing
4 changed files
with
30 additions
and
11 deletions
| ... | @@ -15,4 +15,4 @@ const mdview = new (require('../lib/mdview')), | ... | @@ -15,4 +15,4 @@ const mdview = new (require('../lib/mdview')), |
| 15 | const filename = path.resolve(process.cwd(), target); | 15 | const filename = path.resolve(process.cwd(), target); |
| 16 | var result = mdview.renderFile(filename); | 16 | var result = mdview.renderFile(filename); |
| 17 | 17 | ||
| 18 | -console.log(result); | 18 | +console.log(result.html); | ... | ... |
| ... | @@ -7,25 +7,38 @@ function MarkdownIt (opts) { | ... | @@ -7,25 +7,38 @@ function MarkdownIt (opts) { |
| 7 | } | 7 | } |
| 8 | 8 | ||
| 9 | opts = opts || {}; | 9 | opts = opts || {}; |
| 10 | + let plugin = require('markdown-it-github-toc').default; | ||
| 10 | 11 | ||
| 11 | - const md = require('markdown-it')(opts.options); | 12 | + const md = require('markdown-it')(opts.options) |
| 13 | + .use(plugin, { | ||
| 14 | + anchorLinkSymbol: '¶' | ||
| 15 | + }); | ||
| 12 | 16 | ||
| 13 | this.renderFile = function (filename) { | 17 | this.renderFile = function (filename) { |
| 14 | if (!filename) { | 18 | if (!filename) { |
| 15 | throw new Error('filename can not be null!') | 19 | throw new Error('filename can not be null!') |
| 16 | } | 20 | } |
| 17 | 21 | ||
| 18 | - let result; | 22 | + let result = {}, content; |
| 19 | try { | 23 | try { |
| 20 | - const content = fs.readFileSync(filename).toString(); | 24 | + content = fs.readFileSync(filename).toString(); |
| 21 | - result = md.render(content); | 25 | + result.html = md.render(content); |
| 22 | } catch (e) { | 26 | } catch (e) { |
| 23 | - result = md.render(` | 27 | + console.error('convert md to html error', e); |
| 28 | + result.html = md.render(` | ||
| 24 | # 404 | 29 | # 404 |
| 25 | *文件找不到!* | 30 | *文件找不到!* |
| 26 | `) | 31 | `) |
| 27 | } | 32 | } |
| 28 | 33 | ||
| 34 | + try { | ||
| 35 | + if (opts.parseTokens) { | ||
| 36 | + result.tokens = md.parse(content, {}); | ||
| 37 | + } | ||
| 38 | + } catch (e) { | ||
| 39 | + console.error('parse md error', e) | ||
| 40 | + } | ||
| 41 | + | ||
| 29 | return result; | 42 | return result; |
| 30 | } | 43 | } |
| 31 | } | 44 | } | ... | ... |
| ... | @@ -17,6 +17,7 @@ | ... | @@ -17,6 +17,7 @@ |
| 17 | "express": "^4.16.2", | 17 | "express": "^4.16.2", |
| 18 | "github-markdown-css": "^2.10.0", | 18 | "github-markdown-css": "^2.10.0", |
| 19 | "kml-customize": "git+ssh://git@gitlab.kmlab.com/comm/customize.git#1.0.0", | 19 | "kml-customize": "git+ssh://git@gitlab.kmlab.com/comm/customize.git#1.0.0", |
| 20 | - "markdown-it": "^8.4.0" | 20 | + "markdown-it": "^8.4.0", |
| 21 | + "markdown-it-github-toc": "^3.2.4" | ||
| 21 | } | 22 | } |
| 22 | } | 23 | } | ... | ... |
| ... | @@ -3,7 +3,7 @@ const app = express(); | ... | @@ -3,7 +3,7 @@ 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')), | 6 | +const mdview = new (require('./lib/mdview'))({parseTokens: true}), |
| 7 | path = require('path'); | 7 | path = require('path'); |
| 8 | 8 | ||
| 9 | const MARKDOWN_FILE_BASE = path.resolve(process.cwd(), config.md.base_path); | 9 | const MARKDOWN_FILE_BASE = path.resolve(process.cwd(), config.md.base_path); |
| ... | @@ -22,28 +22,33 @@ function sendTemplate (filename, theme) { | ... | @@ -22,28 +22,33 @@ function sendTemplate (filename, theme) { |
| 22 | file += '.md' | 22 | file += '.md' |
| 23 | } | 23 | } |
| 24 | let result = mdview.renderFile(file); | 24 | let result = mdview.renderFile(file); |
| 25 | + let tittle = result.tokens && result.tokens[1] && result.tokens[1].content || filename; | ||
| 25 | theme = theme || config.md.theme; | 26 | theme = theme || config.md.theme; |
| 26 | return ` | 27 | return ` |
| 27 | <!DOCTYPE html> | 28 | <!DOCTYPE html> |
| 28 | <html> | 29 | <html> |
| 29 | <head> | 30 | <head> |
| 30 | <meta charset=utf-8> | 31 | <meta charset=utf-8> |
| 31 | - <title>less theme for standard markdown</title> | 32 | + <title>${tittle}</title> |
| 32 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> | 33 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 33 | <link rel="stylesheet" href="/markdown.css"> | 34 | <link rel="stylesheet" href="/markdown.css"> |
| 34 | <link rel="stylesheet" href="/${theme}/${theme}.css"> | 35 | <link rel="stylesheet" href="/${theme}/${theme}.css"> |
| 35 | </head> | 36 | </head> |
| 36 | <body> | 37 | <body> |
| 37 | <div class='markdown ${theme}'> | 38 | <div class='markdown ${theme}'> |
| 38 | - ${result} | 39 | + ${result.html} |
| 39 | </div> | 40 | </div> |
| 40 | </body | 41 | </body |
| 41 | </html> | 42 | </html> |
| 42 | `; | 43 | `; |
| 43 | } | 44 | } |
| 44 | 45 | ||
| 45 | -app.get('/**', function (req, res) { | 46 | +app.get('/**', function (req, res, next) { |
| 46 | let url = req.url, urls = url.match(/^\/([\/\w]+)/) || [], query = req.query; | 47 | let url = req.url, urls = url.match(/^\/([\/\w]+)/) || [], query = req.query; |
| 48 | + let ext = path.parse(url).ext; | ||
| 49 | + if (ext && ext !== 'md') { | ||
| 50 | + return next(); | ||
| 51 | + } | ||
| 47 | res.send(sendTemplate(urls[1], query.t)); | 52 | res.send(sendTemplate(urls[1], query.t)); |
| 48 | }) | 53 | }) |
| 49 | 54 | ... | ... |
-
Please register or login to post a comment