Showing
6 changed files
with
73 additions
and
31 deletions
| ... | @@ -15,14 +15,12 @@ program | ... | @@ -15,14 +15,12 @@ program |
| 15 | .alias('e') | 15 | .alias('e') |
| 16 | .description('export markdown to html') | 16 | .description('export markdown to html') |
| 17 | .option('-A --abstract', 'abstract to current path') | 17 | .option('-A --abstract', 'abstract to current path') |
| 18 | - .action(function (md, html, options) { | 18 | + .action(function (md, html) { |
| 19 | //判断目标输出参数 | 19 | //判断目标输出参数 |
| 20 | if (!fs.existsSync(md)) { | 20 | if (!fs.existsSync(md)) { |
| 21 | console.error(chalk.red(md + ' is not found')); | 21 | console.error(chalk.red(md + ' is not found')); |
| 22 | return | 22 | return |
| 23 | } | 23 | } |
| 24 | - let relative_path = ''; | ||
| 25 | - let abstract = options.abstract; | ||
| 26 | let default_name = path.parse(md).name + '.html'; | 24 | let default_name = path.parse(md).name + '.html'; |
| 27 | if (!html) { | 25 | if (!html) { |
| 28 | html = md.replace(/\.md$/g, '') + '.html' | 26 | html = md.replace(/\.md$/g, '') + '.html' |
| ... | @@ -33,11 +31,8 @@ program | ... | @@ -33,11 +31,8 @@ program |
| 33 | } | 31 | } |
| 34 | let output = path.resolve(process.cwd(), html); | 32 | let output = path.resolve(process.cwd(), html); |
| 35 | fs.ensureFileSync(output); | 33 | fs.ensureFileSync(output); |
| 36 | - if (!abstract) { | 34 | + |
| 37 | - // 默认使用相对的资源路径 | 35 | + const viewer = new (require('../lib/view-markdown'))(process.cwd(), {inline: true}); |
| 38 | - relative_path = path.relative(path.dirname(output), 'themes'); | ||
| 39 | - } | ||
| 40 | - const viewer = new (require('../lib/view-markdown'))(process.cwd(), {relative_path: relative_path}); | ||
| 41 | let result = viewer.render(md, 'metro-lake'); | 36 | let result = viewer.render(md, 'metro-lake'); |
| 42 | 37 | ||
| 43 | fs.writeFile(output, result); | 38 | fs.writeFile(output, result); | ... | ... |
| ... | @@ -9,7 +9,8 @@ const _ = require('lodash'), | ... | @@ -9,7 +9,8 @@ const _ = require('lodash'), |
| 9 | path = require('path'), | 9 | path = require('path'), |
| 10 | express = require('express'), | 10 | express = require('express'), |
| 11 | router = express.Router(), | 11 | router = express.Router(), |
| 12 | - config = global.config | 12 | + config = global.config, |
| 13 | + fs = require('fs-extra') | ||
| 13 | ; | 14 | ; |
| 14 | 15 | ||
| 15 | /** | 16 | /** |
| ... | @@ -53,7 +54,9 @@ function MdAction (alias_name, md_path) { | ... | @@ -53,7 +54,9 @@ function MdAction (alias_name, md_path) { |
| 53 | return next(); | 54 | return next(); |
| 54 | } | 55 | } |
| 55 | 56 | ||
| 56 | - res.send(viewer.render(urls[1], query.t)); | 57 | + let html = viewer.render(urls[1], query.t); |
| 58 | + res.send(html) | ||
| 59 | + | ||
| 57 | }); | 60 | }); |
| 58 | //加载文档目录为静态页,别名为空时指向根路径 | 61 | //加载文档目录为静态页,别名为空时指向根路径 |
| 59 | router.use(alias || '/', express.static(doc_path)); | 62 | router.use(alias || '/', express.static(doc_path)); | ... | ... |
| ... | @@ -3,7 +3,10 @@ | ... | @@ -3,7 +3,10 @@ |
| 3 | * Created by lintry on 2018/1/9. | 3 | * Created by lintry on 2018/1/9. |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | -const path = require('path'); | 6 | +const path = require('path'), |
| 7 | + fs = require('fs-extra'), | ||
| 8 | + ejs = require('ejs'), | ||
| 9 | + juice = require('juice'); | ||
| 7 | 10 | ||
| 8 | 11 | ||
| 9 | class ViewMarkdown { | 12 | class ViewMarkdown { |
| ... | @@ -42,29 +45,32 @@ class ViewMarkdown { | ... | @@ -42,29 +45,32 @@ class ViewMarkdown { |
| 42 | let result = this.mdviewer.renderFile(file); | 45 | let result = this.mdviewer.renderFile(file); |
| 43 | let tittle = result.tokens && result.tokens[1] && result.tokens[1].content || filename; | 46 | let tittle = result.tokens && result.tokens[1] && result.tokens[1].content || filename; |
| 44 | theme = theme || options.theme || ''; | 47 | theme = theme || options.theme || ''; |
| 45 | - let relative_path = this.options.relative_path || ''; | ||
| 46 | let content = result.status === 404 ? this.mdviewer.render(` | 48 | let content = result.status === 404 ? this.mdviewer.render(` |
| 47 | # 404 | 49 | # 404 |
| 48 | > **${filename}不存在** | 50 | > **${filename}不存在** |
| 49 | `) | 51 | `) |
| 50 | : result.html; | 52 | : result.html; |
| 51 | - return ` | 53 | + |
| 52 | - <!DOCTYPE html> | 54 | + let inline = this.options.inline; |
| 53 | - <html> | 55 | + const template = fs.readFileSync(path.resolve(process.cwd(), (inline) ? 'src/inline-index.ejs' : 'src/index.ejs'), 'utf8'); |
| 54 | - <head> | 56 | + let html = ejs.render(template, { |
| 55 | - <meta charset=utf-8> | 57 | + tittle, |
| 56 | - <title>${tittle}</title> | 58 | + theme, |
| 57 | - <meta name="viewport" content="width=device-width, initial-scale=1.0"> | 59 | + content |
| 58 | - <link rel="stylesheet" href="${relative_path}/markdown.css"> | 60 | + }, { |
| 59 | - <link rel="stylesheet" href="${relative_path}/${theme}/${theme}.css"> | 61 | + root: process.cwd() |
| 60 | - </head> | 62 | + }); |
| 61 | - <body> | 63 | + |
| 62 | - <div class='markdown ${theme}'> | 64 | + if (inline) { |
| 63 | - ${content} | 65 | + let css = [ |
| 64 | - </div> | 66 | + fs.readFileSync(path.resolve(process.cwd(), 'themes/markdown.css'), 'utf8'), |
| 65 | - </body | 67 | + fs.readFileSync(path.resolve(process.cwd(), 'themes', theme, theme + '.css'), 'utf8') |
| 66 | - </html> | 68 | + ].join('\n'); |
| 67 | -`; | 69 | + html = juice.inlineContent(html, css); |
| 70 | + } | ||
| 71 | + | ||
| 72 | + return html | ||
| 73 | + | ||
| 68 | } | 74 | } |
| 69 | } | 75 | } |
| 70 | 76 | ... | ... |
| 1 | { | 1 | { |
| 2 | "name": "markdown-view", | 2 | "name": "markdown-view", |
| 3 | - "version": "1.1.0", | 3 | + "version": "1.2.0", |
| 4 | "description": "", | 4 | "description": "", |
| 5 | "main": "index.js", | 5 | "main": "index.js", |
| 6 | "scripts": { | 6 | "scripts": { |
| 7 | "start": "node server", | 7 | "start": "node server", |
| 8 | + "build": "cross-env node build/build.js", | ||
| 9 | + "export": "cross-env webpack --config build/webpack.base.conf.js", | ||
| 8 | "test": "echo \"Error: no test specified\" && exit 1" | 10 | "test": "echo \"Error: no test specified\" && exit 1" |
| 9 | }, | 11 | }, |
| 10 | "bin": { | 12 | "bin": { |
| ... | @@ -16,11 +18,15 @@ | ... | @@ -16,11 +18,15 @@ |
| 16 | "dependencies": { | 18 | "dependencies": { |
| 17 | "chalk": "^2.3.1", | 19 | "chalk": "^2.3.1", |
| 18 | "commander": "^2.14.1", | 20 | "commander": "^2.14.1", |
| 21 | + "cross-env": "^5.1.3", | ||
| 22 | + "ejs": "^2.5.7", | ||
| 19 | "express": "^4.16.2", | 23 | "express": "^4.16.2", |
| 20 | "fs-extra": "^5.0.0", | 24 | "fs-extra": "^5.0.0", |
| 21 | "github-markdown-css": "^2.10.0", | 25 | "github-markdown-css": "^2.10.0", |
| 26 | + "juice": "^4.2.2", | ||
| 22 | "kml-customize": "git+ssh://git@gitlab.kmlab.com/comm/customize.git#1.0.0", | 27 | "kml-customize": "git+ssh://git@gitlab.kmlab.com/comm/customize.git#1.0.0", |
| 23 | "markdown-it": "^8.4.0", | 28 | "markdown-it": "^8.4.0", |
| 24 | "markdown-it-github-toc": "^3.2.4" | 29 | "markdown-it-github-toc": "^3.2.4" |
| 25 | - } | 30 | + }, |
| 31 | + "devDependencies": {} | ||
| 26 | } | 32 | } | ... | ... |
src/index.ejs
0 → 100644
| 1 | +<!DOCTYPE html> | ||
| 2 | +<html> | ||
| 3 | +<head> | ||
| 4 | + <meta charset=utf-8> | ||
| 5 | + <title> | ||
| 6 | + <%= tittle %> | ||
| 7 | + </title> | ||
| 8 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| 9 | + <link rel="stylesheet" href="/markdown.css"> | ||
| 10 | + <link rel="stylesheet" href="<%- '/' + theme + '/' + theme + '.css'%>"> | ||
| 11 | +</head> | ||
| 12 | +<body> | ||
| 13 | +<div class='markdown <%= theme %>'> | ||
| 14 | + <%- content %> | ||
| 15 | +</div> | ||
| 16 | +</body> | ||
| 17 | +</html> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
src/inline-index.ejs
0 → 100644
| 1 | +<!DOCTYPE html> | ||
| 2 | +<html> | ||
| 3 | +<head> | ||
| 4 | + <meta charset=utf-8> | ||
| 5 | + <title> | ||
| 6 | + <%= tittle %> | ||
| 7 | + </title> | ||
| 8 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| 9 | +</head> | ||
| 10 | +<body> | ||
| 11 | +<div class='markdown <%= theme %>'> | ||
| 12 | + <%- content %> | ||
| 13 | +</div> | ||
| 14 | +</body> | ||
| 15 | +</html> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment