1 | const fs = require('fs-extra')
|
2 | const logger = require('./logger');
|
3 | const dom = require('./parse/dom.js');
|
4 | const url = require('url');
|
5 | const path = require('path');
|
6 |
|
7 | exports.defaultDocsPath = './docs';
|
8 | exports.defaultBuildPath = './_site'
|
9 | exports.projectPath = process.cwd();
|
10 | exports.defaultTplHookPrefix = 'tpl:'
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | exports.extend = function extend(obj, props) {
|
19 | for (let i in props) {
|
20 | if (props.hasOwnProperty(i)) {
|
21 | obj[i] = props[i];
|
22 | }
|
23 | }
|
24 | return obj;
|
25 | }
|
26 |
|
27 | exports.distinct = function(arr, fn){
|
28 | let data = [], existValue = []
|
29 | arr.forEach(item=>{
|
30 | let value = fn(item)
|
31 | if(existValue.indexOf(value) === -1){
|
32 | existValue.push(value)
|
33 | data.push(item)
|
34 | }
|
35 | })
|
36 | return data;
|
37 | }
|
38 |
|
39 |
|
40 | exports.clearArray = function clearArray(a) {
|
41 | return a.splice(0, a.length);
|
42 | }
|
43 |
|
44 | function fileExist(filePath){
|
45 | try {
|
46 | return fs.statSync(filePath).isFile();
|
47 | } catch (err) {
|
48 | return false;
|
49 | }
|
50 | }
|
51 | exports.fileExist = fileExist;
|
52 |
|
53 |
|
54 | function dirExist(filePath){
|
55 | try {
|
56 | return fs.statSync(filePath).isDirectory();
|
57 | } catch (err) {
|
58 | return false;
|
59 | }
|
60 | }
|
61 | exports.dirExist = dirExist;
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | exports.log = new logger('info');
|
72 |
|
73 | exports.hashEncode = function hashEncode(text){
|
74 | return text.replace(/[\~\:\#\@\/\(\)]/g, '').replace(/\s+/g, '-');
|
75 | }
|
76 |
|
77 | exports.handleMdUrl = (findTransactionBySrcPath) => (content, filepath) => {
|
78 | if(!/\.md/.test(content)){
|
79 | return content;
|
80 | }
|
81 | let $ = dom.parse(content);
|
82 | let urls = $('a');
|
83 | urls.each(function(){
|
84 | let item = $(this);
|
85 | let href = item.attr('href');
|
86 | if(!href) return;
|
87 | let urlObj = url.parse(href);
|
88 |
|
89 | if(urlObj.hostname){
|
90 | return;
|
91 | }
|
92 | if(!urlObj.path){
|
93 | return;
|
94 | }
|
95 |
|
96 | if(path.extname(urlObj.pathname) === '.md'){
|
97 | let srcPath = path.resolve(filepath, urlObj.pathname);
|
98 | let findTransaction = findTransactionBySrcPath(srcPath);
|
99 | if(findTransaction){
|
100 | item.attr('href', href.replace('.md', '.html'))
|
101 | }
|
102 | }
|
103 | })
|
104 | return $.html()
|
105 | }
|
106 |
|
107 | exports.getConfigPath = function getConfigPath(dirname){
|
108 | let allowFilename = ['ydoc.json', 'ydoc.js'];
|
109 | for(let i =0; i< allowFilename.length; i++){
|
110 | let configFilepath = path.resolve(dirname, allowFilename[i]);
|
111 | if(fileExist(configFilepath)) return configFilepath;
|
112 | }
|
113 | return null;
|
114 | }
|
115 |
|
116 | exports.getConfig = function getConfig(filepath){
|
117 | if(fileExist(filepath)){
|
118 | return require(filepath)
|
119 | }
|
120 | return {};
|
121 | }
|
122 |
|
123 | exports.isUrl = function(url){
|
124 | return /^https?:\/\//.test(url)
|
125 | }
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | function mergeCopyFiles(src, dist){
|
133 | let files = fs.readdirSync(src);
|
134 | files.forEach(item=>{
|
135 | let distPath = path.resolve(dist, item);
|
136 | let srcPath = path.resolve(src, item)
|
137 | if(fileExist(srcPath)){
|
138 | fs.copySync(srcPath, distPath)
|
139 | }else if(dirExist(srcPath)){
|
140 | mergeCopyFiles(srcPath, distPath)
|
141 | }
|
142 | })
|
143 | }
|
144 |
|
145 | exports.mergeCopyFiles = mergeCopyFiles; |
\ | No newline at end of file |