UNPKG

3.51 kBJavaScriptView Raw
1const path = require('path');
2const output = require('./output.js');
3const _ = require('underscore');
4const batch = [];
5const utils = require('./utils.js');
6
7const fs = require('fs-extra');
8
9const parseMarkdown = require('./parse/markdown.js').parseMarkdown;
10const parseHtml = require('./parse/html');
11const parsePage = require('./parse/page');
12const parseJsx = require('./parse/jsx');
13const emitHook = require('./plugin.js').emitHook;
14
15function insertToBatch(transaction){
16 batch.push(transaction);
17}
18
19function findTransactionBySrcPath(path){
20 return _.find(batch, {srcPath: path})
21}
22
23function handleTitle(subTitle, title) {
24 return subTitle === title ? subTitle : (subTitle + '-' + title)
25}
26
27const handleMdUrl = utils.handleMdUrl(findTransactionBySrcPath);
28
29exports.getBatch = function(){
30 return batch;
31}
32
33exports.runBatch = async function runBatch(){
34 if(batch.length === 0) return;
35 for(let index=0, l = batch.length; index<l; index++){
36 let transaction = batch[index];
37 let context = transaction.context;
38 context.name = context.title;
39 let page = context.page;
40 let _p, parseJsxInst;
41 if(!utils.fileExist(page.srcPath)) continue;
42 switch(page.type){
43 case 'md' :
44 _p = parsePage(parseMarkdown(page.srcPath), true);
45 break;
46 case 'jsx' :
47 parseJsxInst = parseJsx(page.srcPath);
48 parseJsxInst.data = parseJsxInst.data && typeof parseJsxInst.data === 'object' ? parseJsxInst.data : {};
49 _p = {
50 title: parseJsxInst.data.title || '',
51 description: parseJsxInst.data.description || '',
52 content: parseJsxInst.render(Object.assign({}, context, parseJsxInst.data))
53 }
54 break;
55 default : _p = {
56 content: parseHtml(page.srcPath)
57 }
58 }
59
60 _p.content = handleMdUrl(_p.content, path.dirname(page.srcPath))
61 utils.extend(page, _p);
62 if(page.title){
63 context.title = handleTitle(page.title, context.title)
64 }
65 try{
66 await emitHook('page:before', page, context);
67 await output(transaction.context);
68 //避免内存占用太大,使用完立即释放
69 transaction.context.page.content = undefined;
70 }catch(err){
71 throw err;
72 }
73 }
74 //batch 任务执行完成后,删除源文件
75 batch.forEach(transaction=>{
76 if(utils.fileExist(transaction.context.page.srcPath) && transaction.context.page.srcPath !== transaction.context.page.distPath){
77 fs.unlinkSync(transaction.context.page.srcPath);
78 }
79 })
80 utils.clearArray(batch);
81}
82
83function getType(filepath){
84 return path.extname(filepath).substr(1).toLowerCase();
85}
86
87exports.generatePage = function generatePage(bookpath){
88 let prevPage = null;
89
90 return function _generatePage(context){
91 const page = context.page;
92 if(findTransactionBySrcPath(page.srcPath))return;
93 let releativePath = page.distPath;
94 page.type = getType(page.srcPath);
95 page.releativePath = releativePath;
96 page.distPath = path.resolve(bookpath, releativePath);
97 page.next = null;
98 if(prevPage === null){
99 page.prev = null;
100 }else{
101 page.prev = {
102 title: prevPage.title,
103 releativePath: prevPage.releativePath,
104 distPath: prevPage.distPath
105 };
106 prevPage.next = {
107 title: page.title,
108 releativePath: page.releativePath,
109 distPath: page.distPath
110 };
111 }
112 prevPage = page;
113 insertToBatch({
114 srcPath: page.srcPath,
115 context: context
116 })
117
118 };
119
120}
121