Showing
4 changed files
with
82 additions
and
21 deletions
| 1 | #!/usr/bin/env node | 1 | #!/usr/bin/env node |
| 2 | 2 | ||
| 3 | "use strict"; | 3 | "use strict"; |
| 4 | +const PKG = require('../package.json'), | ||
| 5 | + program = require('commander'), | ||
| 6 | + chalk = require('chalk'), | ||
| 7 | + path = require('path'), | ||
| 8 | + fs = require('fs-extra'); | ||
| 4 | 9 | ||
| 5 | -var target = process.argv[2]; | 10 | +program |
| 11 | + .version(PKG.version); | ||
| 6 | 12 | ||
| 7 | -if (!target) { | 13 | +program |
| 8 | - console.error('target file is not found!'); | 14 | + .command('export <markdown_file> [html_file]') |
| 9 | - process.exit(1); | 15 | + .alias('e') |
| 10 | -} | 16 | + .description('export markdown to html') |
| 17 | + .option('-A --abstract', 'abstract to current path') | ||
| 18 | + .action(function (md, html, options) { | ||
| 19 | + //判断目标输出参数 | ||
| 20 | + if (!fs.existsSync(md)) { | ||
| 21 | + console.error(chalk.red(md + ' is not found')); | ||
| 22 | + return | ||
| 23 | + } | ||
| 24 | + let relative_path = ''; | ||
| 25 | + let abstract = options.abstract; | ||
| 26 | + let default_name = path.parse(md).name + '.html'; | ||
| 27 | + if (!html) { | ||
| 28 | + html = md.replace(/\.md$/g, '') + '.html' | ||
| 29 | + } | ||
| 30 | + let target = path.parse(html); | ||
| 31 | + if (!target.ext) { //目标没有后缀作为目录,添加md的同名html | ||
| 32 | + html = path.join(html, default_name) | ||
| 33 | + } | ||
| 34 | + let output = path.resolve(process.cwd(), html); | ||
| 35 | + fs.ensureFileSync(output); | ||
| 36 | + if (!abstract) { | ||
| 37 | + // 默认使用相对的资源路径 | ||
| 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'); | ||
| 42 | + | ||
| 43 | + fs.writeFile(output, result); | ||
| 44 | + console.log('html is exported to', chalk.green(output)) | ||
| 45 | + }); | ||
| 11 | 46 | ||
| 12 | -const mdview = new (require('../lib/mdviewer')), | 47 | +program |
| 13 | - path = require('path'); | 48 | + .parse(process.argv); |
| 14 | 49 | ||
| 15 | -const filename = path.resolve(process.cwd(), target); | 50 | +if (process.argv.length === 2) { |
| 16 | -var result = mdview.renderFile(filename); | 51 | + program.outputHelp(); |
| 52 | +} | ||
| 17 | 53 | ||
| 18 | -console.log(result.html); | ... | ... |
| ... | @@ -7,6 +7,11 @@ const path = require('path'); | ... | @@ -7,6 +7,11 @@ const path = require('path'); |
| 7 | 7 | ||
| 8 | 8 | ||
| 9 | class ViewMarkdown { | 9 | class ViewMarkdown { |
| 10 | + /** | ||
| 11 | + * 初始化markdown | ||
| 12 | + * @param doc_path 文档所在路径 | ||
| 13 | + * @param options toc: 是否显示TOC default_md: 默认markdown文件名 theme: 默认主题 relative_path: css的相对路径(便于输出html) | ||
| 14 | + */ | ||
| 10 | constructor (doc_path, options) { | 15 | constructor (doc_path, options) { |
| 11 | this.options = options || {}; | 16 | this.options = options || {}; |
| 12 | if (!doc_path) { | 17 | if (!doc_path) { |
| ... | @@ -36,7 +41,8 @@ class ViewMarkdown { | ... | @@ -36,7 +41,8 @@ class ViewMarkdown { |
| 36 | } | 41 | } |
| 37 | let result = this.mdviewer.renderFile(file); | 42 | let result = this.mdviewer.renderFile(file); |
| 38 | let tittle = result.tokens && result.tokens[1] && result.tokens[1].content || filename; | 43 | let tittle = result.tokens && result.tokens[1] && result.tokens[1].content || filename; |
| 39 | - theme = theme || options.theme; | 44 | + theme = theme || options.theme || ''; |
| 45 | + let relative_path = this.options.relative_path || ''; | ||
| 40 | let content = result.status === 404 ? this.mdviewer.render(` | 46 | let content = result.status === 404 ? this.mdviewer.render(` |
| 41 | # 404 | 47 | # 404 |
| 42 | > **${filename}不存在** | 48 | > **${filename}不存在** |
| ... | @@ -49,8 +55,8 @@ class ViewMarkdown { | ... | @@ -49,8 +55,8 @@ class ViewMarkdown { |
| 49 | <meta charset=utf-8> | 55 | <meta charset=utf-8> |
| 50 | <title>${tittle}</title> | 56 | <title>${tittle}</title> |
| 51 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> | 57 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 52 | - <link rel="stylesheet" href="/markdown.css"> | 58 | + <link rel="stylesheet" href="${relative_path}/markdown.css"> |
| 53 | - <link rel="stylesheet" href="/${theme}/${theme}.css"> | 59 | + <link rel="stylesheet" href="${relative_path}/${theme}/${theme}.css"> |
| 54 | </head> | 60 | </head> |
| 55 | <body> | 61 | <body> |
| 56 | <div class='markdown ${theme}'> | 62 | <div class='markdown ${theme}'> | ... | ... |
| 1 | { | 1 | { |
| 2 | "name": "markdown-view", | 2 | "name": "markdown-view", |
| 3 | - "version": "1.0.0", | 3 | + "version": "1.1.0", |
| 4 | "description": "", | 4 | "description": "", |
| 5 | "main": "index.js", | 5 | "main": "index.js", |
| 6 | "scripts": { | 6 | "scripts": { |
| ... | @@ -14,7 +14,10 @@ | ... | @@ -14,7 +14,10 @@ |
| 14 | "author": "lintry <shenlin00@gmail.com>", | 14 | "author": "lintry <shenlin00@gmail.com>", |
| 15 | "license": "MIT", | 15 | "license": "MIT", |
| 16 | "dependencies": { | 16 | "dependencies": { |
| 17 | + "chalk": "^2.3.1", | ||
| 18 | + "commander": "^2.14.1", | ||
| 17 | "express": "^4.16.2", | 19 | "express": "^4.16.2", |
| 20 | + "fs-extra": "^5.0.0", | ||
| 18 | "github-markdown-css": "^2.10.0", | 21 | "github-markdown-css": "^2.10.0", |
| 19 | "kml-customize": "git+ssh://git@gitlab.kmlab.com/comm/customize.git#1.0.0", | 22 | "kml-customize": "git+ssh://git@gitlab.kmlab.com/comm/customize.git#1.0.0", |
| 20 | "markdown-it": "^8.4.0", | 23 | "markdown-it": "^8.4.0", | ... | ... |
| ... | @@ -3,6 +3,7 @@ | ... | @@ -3,6 +3,7 @@ |
| 3 | ## 安装 | 3 | ## 安装 |
| 4 | 4 | ||
| 5 | ```sh | 5 | ```sh |
| 6 | +git clone git@gitlab.kmlab.com:lintry/markdown-view.git | ||
| 6 | npm install | 7 | npm install |
| 7 | 8 | ||
| 8 | ``` | 9 | ``` |
| ... | @@ -29,17 +30,32 @@ npm install | ... | @@ -29,17 +30,32 @@ npm install |
| 29 | #### 指定markdown文件生成html文件 | 30 | #### 指定markdown文件生成html文件 |
| 30 | 31 | ||
| 31 | ```javascript | 32 | ```javascript |
| 32 | -var mdview = require('./lib/mdview')(); | 33 | +var mdview = require('./lib/mdviewer')(); |
| 33 | var path = require('path'); | 34 | var path = require('path'); |
| 34 | var result = mdview.renderFile(path.resolve(process.cwd(), 'readme.md')); | 35 | var result = mdview.renderFile(path.resolve(process.cwd(), 'readme.md')); |
| 35 | ``` | 36 | ``` |
| 36 | 37 | ||
| 37 | 38 | ||
| 38 | 39 | ||
| 39 | -####命令输出html内容 | 40 | +####命令行生成html内容 |
| 40 | 41 | ||
| 41 | ```sh | 42 | ```sh |
| 42 | -bin/mdview readme.md | 43 | +bin/mdview readme.md readme.html |
| 44 | + | ||
| 45 | + | ||
| 46 | + Usage: mdview [options] [command] | ||
| 47 | + | ||
| 48 | + | ||
| 49 | + Options: | ||
| 50 | + | ||
| 51 | + -V, --version output the version number | ||
| 52 | + -h, --help output usage information | ||
| 53 | + | ||
| 54 | + | ||
| 55 | + Commands: | ||
| 56 | + | ||
| 57 | + export|e [markdown_file] <html_file> export markdown to html, default to the same path of markdown_file | ||
| 58 | + | ||
| 43 | ``` | 59 | ``` |
| 44 | 60 | ||
| 45 | 61 | ||
| ... | @@ -50,11 +66,11 @@ bin/mdview readme.md | ... | @@ -50,11 +66,11 @@ bin/mdview readme.md |
| 50 | npm start | 66 | npm start |
| 51 | ``` | 67 | ``` |
| 52 | 68 | ||
| 53 | -> 打开[首页](http://localhost:3000)查看readme.md的显示效果👇 | 69 | +> 打开[首页](http://localhost:8200)查看readme.md的显示效果👇 |
| 54 | > | 70 | > |
| 55 | -> - 首页:http://loalhost:3000 | 71 | +> - 首页:http://loalhost:8200 |
| 56 | -> - 指定md文件: http://localhost:3000/md/sample.md | 72 | +> - 指定md文件: http://localhost:8200/md/sample.md |
| 57 | -> - 选择样式主题:http://localhost:3000/?t=github-markdown | 73 | +> - 选择样式主题:http://localhost:8200/?t=github-markdown |
| 58 | 74 | ||
| 59 | 75 | ||
| 60 | 76 | ||
| ... | @@ -67,6 +83,7 @@ npm start | ... | @@ -67,6 +83,7 @@ npm start |
| 67 | - github | 83 | - github |
| 68 | - github-rhio | 84 | - github-rhio |
| 69 | - haroopad | 85 | - haroopad |
| 86 | +- metro-lake (***修改于metro-vibes***) | ||
| 70 | - metro-vibes | 87 | - metro-vibes |
| 71 | - metro-vibes-dark | 88 | - metro-vibes-dark |
| 72 | - node-dark | 89 | - node-dark | ... | ... |
-
Please register or login to post a comment