forked from jqk6/wilddog-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raneto.js
executable file
·194 lines (167 loc) · 4.9 KB
/
raneto.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var path = require('path'),
fs = require('fs'),
glob = require('glob'),
_ = require('underscore'),
_s = require('underscore.string'),
marked = require('./marked'),
lunr = require('lunr');
var idx=null;
var raneto = {
metaRegex: /^\/\*([\s\S]*?)\*\//i,
cleanString: function(str, underscore){
var u = underscore || false;
if(u){
return _s.underscored(str);
} else {
return _s.dasherize(str);
}
},
processMeta: function(markdownContent) {
var metaArr = markdownContent.match(raneto.metaRegex),
meta = {};
var metaString = metaArr ? metaArr[1].trim() : '';
if(metaString){
var metas = metaString.match(/(.*): (.*)/ig);
metas.forEach(function(item){
var parts = item.split(': ');
if(parts[0] && parts[1]){
meta[raneto.cleanString(parts[0].trim(), true)] = parts[1].trim();
}
});
}
return meta;
},
stripMeta: function(markdownContent) {
return markdownContent.replace(raneto.metaRegex, '').trim();
},
processVars: function(markdownContent, config) {
if(typeof config.base_url !== 'undefined') markdownContent = markdownContent.replace(/\%base_url\%/g, config.base_url);
return markdownContent;
},
/**
* 获得一个页面.如果出现index.md,那么就显示index.md
**/
getPage: function(path, config) {
try {
var file = fs.readFileSync(path),
slug = path.replace(__dirname +'/content/', '').trim();
if(slug.indexOf('index.md') > -1){
slug = slug.replace('index.md', '');
}
slug = slug.replace('.md', '').trim();
var meta = raneto.processMeta(file.toString('utf-8')),
content = raneto.stripMeta(file.toString('utf-8'));
content = raneto.processVars(content, config);
var html = marked(content);
return {
'slug': slug,
'title': meta.title ? meta.title : 'Untitled',
'body': html,
'excerpt': _s.prune(_s.stripTags(_s.unescapeHTML(html)), config.excerpt_length)
};
}
catch(e){}
return null;
},
/**
* 获得页面列表
*/
getPages: function(activeSlug,config) {
// body...
var subDir=function(currentDir,level){
var filesProcessed=[];
var category_sort = config.category_sort || false;
var files = glob.sync(currentDir+"/*");
var filesProcessed=[];
files.forEach(function(filePath){
//var shortPath = filePath.replace(__dirname +'/content/', '').trim(),
var dir = __dirname.replace(/\\/g, "/") + '/content/';
var shortPath = filePath.replace(dir, '').trim(),
stat = fs.lstatSync(filePath);
if(stat.isDirectory()){
var sort = 0;
var title=_s.titleize(_s.humanize(path.basename(shortPath)));
var slug=shortPath;
try {
var metaFile = fs.readFileSync(__dirname +'/content/'+ shortPath +'/meta');
var meta = raneto.processMeta(metaFile.toString('utf-8'));
if(meta["sort"])
sort = parseInt(meta["sort"], 10);
if(meta["title"])
title=meta["title"];
if(meta["slug"])
slug=meta["slug"];
}
catch(e){
console.log(e);
}
var dirObj={
is_dir:true,
slug: slug,
path: filePath,
title: title,
is_index: false,
class: 'category-'+ (level+1),
sort: sort,
active:(activeSlug.trim().indexOf( '/'+ shortPath)==0),
files:subDir(filePath,level+1)
};
filesProcessed.push(dirObj);
}
else if(stat.isFile()&& path.extname(shortPath) == '.md'){
var file = fs.readFileSync(filePath),
slug = shortPath,
pageSort = 0;
var isIndex=false;
if(shortPath.indexOf('index.md') > -1){
slug = slug.replace('index.md', '');
isIndex=true;
}
slug = slug.replace('.md', '').trim();
var dir = path.dirname(shortPath),
meta = raneto.processMeta(file.toString('utf-8'));
if(meta["sort"]) pageSort = parseInt(meta["sort"], 10);
filesProcessed.push({
is_md:true,
is_index:isIndex,
slug:slug,
title:meta.title ? meta.title : 'Untitled',
active: (activeSlug.trim().indexOf( '/'+ slug)==0),
sort: pageSort
});
}
});
filesProcessed=_.sortBy(filesProcessed, function(file){ return file.sort; });
return filesProcessed;
}
return subDir(__dirname+"/content",0,activeSlug);
},
search: function(query) {
if(idx == null){
var files = glob.sync(__dirname +'/content/**/*.md');
idx = lunr(function(){
this.field('title', { boost: 10 });
this.field('body');
});
files.forEach(function(filePath){
try {
//skip index
if(filePath.indexOf("index.md")>=0){
return;
};
var shortPath = filePath.replace(__dirname +'/content/', '').trim(),
file = fs.readFileSync(filePath);
var meta = raneto.processMeta(file.toString('utf-8'));
idx.add({
'id': shortPath,
'title': meta.title ? meta.title : 'Untitled',
'body': file.toString('utf-8')
});
}
catch(e){}
});
}
return idx.search(query);
}
};
module.exports = raneto;