lintry

命令行生成hml文件

#!/usr/bin/env node
"use strict";
const PKG = require('../package.json'),
program = require('commander'),
chalk = require('chalk'),
path = require('path'),
fs = require('fs-extra');
var target = process.argv[2];
program
.version(PKG.version);
if (!target) {
console.error('target file is not found!');
process.exit(1);
}
program
.command('export <markdown_file> [html_file]')
.alias('e')
.description('export markdown to html')
.option('-A --abstract', 'abstract to current path')
.action(function (md, html, options) {
//判断目标输出参数
if (!fs.existsSync(md)) {
console.error(chalk.red(md + ' is not found'));
return
}
let relative_path = '';
let abstract = options.abstract;
let default_name = path.parse(md).name + '.html';
if (!html) {
html = md.replace(/\.md$/g, '') + '.html'
}
let target = path.parse(html);
if (!target.ext) { //目标没有后缀作为目录,添加md的同名html
html = path.join(html, default_name)
}
let output = path.resolve(process.cwd(), html);
fs.ensureFileSync(output);
if (!abstract) {
// 默认使用相对的资源路径
relative_path = path.relative(path.dirname(output), 'themes');
}
const viewer = new (require('../lib/view-markdown'))(process.cwd(), {relative_path: relative_path});
let result = viewer.render(md, 'metro-lake');
fs.writeFile(output, result);
console.log('html is exported to', chalk.green(output))
});
const mdview = new (require('../lib/mdviewer')),
path = require('path');
program
.parse(process.argv);
const filename = path.resolve(process.cwd(), target);
var result = mdview.renderFile(filename);
if (process.argv.length === 2) {
program.outputHelp();
}
console.log(result.html);
......
......@@ -7,6 +7,11 @@ const path = require('path');
class ViewMarkdown {
/**
* 初始化markdown
* @param doc_path 文档所在路径
* @param options toc: 是否显示TOC default_md: 默认markdown文件名 theme: 默认主题 relative_path: css的相对路径(便于输出html)
*/
constructor (doc_path, options) {
this.options = options || {};
if (!doc_path) {
......@@ -36,7 +41,8 @@ class ViewMarkdown {
}
let result = this.mdviewer.renderFile(file);
let tittle = result.tokens && result.tokens[1] && result.tokens[1].content || filename;
theme = theme || options.theme;
theme = theme || options.theme || '';
let relative_path = this.options.relative_path || '';
let content = result.status === 404 ? this.mdviewer.render(`
# 404
> **${filename}不存在**
......@@ -49,8 +55,8 @@ class ViewMarkdown {
<meta charset=utf-8>
<title>${tittle}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/markdown.css">
<link rel="stylesheet" href="/${theme}/${theme}.css">
<link rel="stylesheet" href="${relative_path}/markdown.css">
<link rel="stylesheet" href="${relative_path}/${theme}/${theme}.css">
</head>
<body>
<div class='markdown ${theme}'>
......
{
"name": "markdown-view",
"version": "1.0.0",
"version": "1.1.0",
"description": "",
"main": "index.js",
"scripts": {
......@@ -14,7 +14,10 @@
"author": "lintry <shenlin00@gmail.com>",
"license": "MIT",
"dependencies": {
"chalk": "^2.3.1",
"commander": "^2.14.1",
"express": "^4.16.2",
"fs-extra": "^5.0.0",
"github-markdown-css": "^2.10.0",
"kml-customize": "git+ssh://git@gitlab.kmlab.com/comm/customize.git#1.0.0",
"markdown-it": "^8.4.0",
......
......@@ -3,6 +3,7 @@
## 安装
```sh
git clone git@gitlab.kmlab.com:lintry/markdown-view.git
npm install
```
......@@ -29,17 +30,32 @@ npm install
#### 指定markdown文件生成html文件
```javascript
var mdview = require('./lib/mdview')();
var mdview = require('./lib/mdviewer')();
var path = require('path');
var result = mdview.renderFile(path.resolve(process.cwd(), 'readme.md'));
```
####命令输出html内容
####命令行生成html内容
```sh
bin/mdview readme.md
bin/mdview readme.md readme.html
Usage: mdview [options] [command]
Options:
-V, --version output the version number
-h, --help output usage information
Commands:
export|e [markdown_file] <html_file> export markdown to html, default to the same path of markdown_file
```
......@@ -50,11 +66,11 @@ bin/mdview readme.md
npm start
```
> 打开[首页](http://localhost:3000)查看readme.md的显示效果👇
> 打开[首页](http://localhost:8200)查看readme.md的显示效果👇
>
> - 首页:http://loalhost:3000
> - 指定md文件: http://localhost:3000/md/sample.md
> - 选择样式主题:http://localhost:3000/?t=github-markdown
> - 首页:http://loalhost:8200
> - 指定md文件: http://localhost:8200/md/sample.md
> - 选择样式主题:http://localhost:8200/?t=github-markdown
......@@ -67,6 +83,7 @@ npm start
- github
- github-rhio
- haroopad
- metro-lake (***修改于metro-vibes***)
- metro-vibes
- metro-vibes-dark
- node-dark
......