Skip to content
This repository has been archived by the owner on May 7, 2022. It is now read-only.

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
xuexb committed Aug 28, 2016
1 parent 9ddbd34 commit 3bb1f0e
Show file tree
Hide file tree
Showing 48 changed files with 2,091 additions and 1,446 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@
"art-template": "^3.0.3",
"babel-runtime": "^5.8.20",
"express": "^4.13.3",
"fs-grep": "*",
"highlight.js": "^8.9.1",
"key-cache": "0.x.x",
"marked": "^0.3.5",
"serve-static": "^1.10.0",
"transliteration": "^0.1.1"
"serve-index": "*"
},
"devDependencies": {
"babel-cli": "6.x",
Expand Down
141 changes: 88 additions & 53 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
* @email fe.xiaowu@gmail.com
*/

'use strict';

import template from 'art-template/node/template-native';
import {slugify} from 'transliteration';

import express from 'express';
import serve_static from 'serve-static';
import serve_index from 'serve-index';

import {existsSync, readdirSync, statSync, readFileSync} from 'fs';
import {parse, format} from 'url';
Expand All @@ -23,35 +22,59 @@ import highlight from 'highlight.js';
import OPTIONS from './options';

export default class Mdjs {

/**
* 构造器
*
* @param {Object} options 配置参数
*/
constructor(options = {}) {
let package_options;
// 先把启动参数缓存,以后覆盖配置使用
this.__options = options;

// 缓存当前运行的目录
this.__dirname = dirname(__dirname);

this.reset();

// 缓存express
this.express = express();

// 初始化express
this._init_express();
}

/**
* 重置配置参数
*
* @param {Object} options 配置参数
*
* @return {Object} this
*/
reset(options) {
let pkg;
try {
package_options = require(resolve('./package.json')).mdjs;
// 先删除缓存
delete require.cache[resolve('./package.json')];

// 加载package模块
pkg = require(resolve('./package.json')).mdjs;
}
catch (e) {
package_options = {};
pkg = {};
}

let useOptions = this.__options;

// 合并默认配置
// 合并的顺序是: 参数 > package.mdjs > 默认 (由左向右合并)
options = this.options = {...OPTIONS, ...package_options, ...options};
options = this.options = {...OPTIONS, ...pkg, useOptions, ...options};

// 重新赋值
options.root = resolve(options.root);
options.cache_path = resolve(options.cache_path);

// 缓存当前运行的目录
this.__dirname = dirname(__dirname);

// 缓存express
this.express = express();

// 初始化express
this._init_express();
return this.clear_cache();
}

/**
Expand All @@ -61,60 +84,72 @@ export default class Mdjs {
*
* @return {string} html代码
*/
get_render_nav(id) {
get_render_nav(uri) {
let data = this.get_list();
let str = '';

if (!data || !data.length) {
return str;
}

if (id) {
id = decodeURIComponent(id);
if (uri) {
uri = decodeURIComponent(uri);
}

let filter = (filepath, type) => {
if (!id) {
if (!uri) {
return false;
}

if (type === 'dir') {
return id.indexOf(filepath + '/') === 0;
return uri.indexOf(filepath + '/') === 0;
}
return id === filepath;
return uri === filepath;
};

console.log(id)

let fn = (res) => {
res.forEach((val) => {
val.icon = false;
val.uri = slugify(val.text, {separator: ''});
let fn = res => {
let html = '';

res.forEach(val => {
if (!val.children || !val.children.length) {
if (filter(val.id, 'file')) {
val.state = {
// opened: true,
selected: true
}
if (filter(val.uri, 'file')) {
html += `<li class="nav-tree-file nav-tree-current">`;
}
else {
html += `<li class="nav-tree-file">`;
}
html += `
<div class="nav-tree-text">
<a href="${val.uri}" class="nav-tree-file-a" data-uri="${val.uri}" title="${val.text}">
${val.text}
</a>
</div>
</li>
`;
}
else {
if (filter(val.id, 'dir')) {
val.state = {
opened: true
}
if (filter(val.uri, 'dir')) {
html += `<li class="nav-tree-dir nav-tree-dir-open">`;
}

fn(val.children);
else {
html += `<li class="nav-tree-dir">`;
}
html += `
<div class="nav-tree-text">
<a href="#" class="nav-tree-dir-a" data-uri="${val.uri}" title="${val.text}">
${val.text}
</a>
</div>
${fn(val.children)}
</li>
`;
}
});
};

return '<ul>' + html + '</ul>';
};

fn(data);

return data;
return fn(data);
}

/**
Expand Down Expand Up @@ -214,25 +249,21 @@ export default class Mdjs {

// 渲染代码
renderer.code = (data, lang) => {
let html;

data = highlight.highlightAuto(data).value;

// 有语言时
if (lang) {

// 超过3行有提示
if (data.split(/\n/).length >= 3) {
html = `<pre><code class="hljs lang-${lang}" data-lang="${lang}">${data}</code></pre>`;
let html = `<pre><code class="hljs lang-${lang}"><span class="hljs-lang-tips">${lang}</span>`;
return html + `${data}</code></pre>`;
}

html = `<pre><code class="hljs lang-${lang}">${data}</code></pre>`;
}
else {
html = `<pre><code class="hljs">${data}</code></pre>`;
return `<pre><code class="hljs lang-${lang}">${data}</code></pre>`;
}

return html;
return `<pre><code class="hljs">${data}</code></pre>`;
};

// md => html
Expand All @@ -256,6 +287,10 @@ export default class Mdjs {
* @return {Object} this
*/
run() {
// 委托目录浏览
this.express.use('/', serve_index(this.options.root, {
icons: true
}));
this.express.listen(this.options.port);
return this;
}
Expand Down Expand Up @@ -378,7 +413,7 @@ export default class Mdjs {
let file_basename = basename(dir);

result = {
id: dir.replace(options.root, '') || '/',
uri: dir.replace(options.root, '') || '/',
children: [],
text: options.dir_alias[file_basename] || file_basename
};
Expand Down Expand Up @@ -430,7 +465,7 @@ export default class Mdjs {
else {
result.children.push({
text: this.get_markdown_title(file.filepath),
id: file.filepath.replace(options.root, '').split(sep).join('\/')
uri: file.filepath.replace(options.root, '').split(sep).join('\/')
});
}
});
Expand Down Expand Up @@ -475,7 +510,7 @@ export default class Mdjs {

// 渲染md
return res.render('markdown', {
nav_data: JSON.stringify(this.get_render_nav(parseUrl.pathname)),
nav_data: this.get_render_nav(parseUrl.pathname),
markdown_data,
title: `${this.get_markdown_title(filepath)} - ${this.options.name}`
});
Expand Down
Binary file removed static/font/icomoon.eot
Binary file not shown.
Loading

0 comments on commit 3bb1f0e

Please sign in to comment.