Repository navigation
Expand file tree
/
Copy pathCheckMarkdown.js
More file actions
236 lines (211 loc) · 7.61 KB
/
Copy pathCheckMarkdown.js
File metadata and controls
236 lines (211 loc) · 7.61 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const fs = require('fs');
const fsp = require('fs').promises;
const path = require('path');
const debug = require('debug')('check-md');
const debugCge = require('debug')('check-get-elf');
const elfUtils = require('elven-code').elfUtils;
//const utils = require('./lib/utils');
const matter = require('gray-matter');
async function ls01(path) {
const dir = await fs.promises.opendir(path)
for await (const dirent of dir) {
console.log(dirent);
console.log(dirent.name);
console.log('Directory', dirent.isDirectory());
console.log('File', dirent.isFile());
console.log('BlockDevice', dirent.isBlockDevice());
console.log('isCharacterDevice', dirent.isCharacterDevice());
console.log('isSymbolicLink', dirent.isSymbolicLink());
console.log('isFIFO', dirent.isFIFO());
console.log('isSocket', dirent.isSocket());
}
}
async function ls02(path) {
const dir = await fs.promises.opendir(path)
for await (const dirent of dir) {
console.log(dirent);
console.log(dirent.name);
if (dirent.isDirectory()) {
console.log('isDirectory');
} else if (dirent.isFile()) {
console.log('File', dirent.isFile());
} else if (dirent.isBlockDevice()) {
console.log('BlockDevice', dirent.isBlockDevice());
} else if (dirent.isCharacterDevice()) {
console.log('isCharacterDevice', dirent.isCharacterDevice());
} else if (dirent.isSymbolicLink()) {
console.log('isSymbolicLink', dirent.isSymbolicLink());
} else if (dirent.isFIFO()) {
console.log('isFIFO', dirent.isFIFO());
} else if (dirent.isSocket()) {
console.log('isSocket', dirent.isSocket());
};
}
}
async function lsDirs(path) {
const dir = await fs.promises.opendir(path)
for await (const dirent of dir) {
if (dirent.isDirectory()) {
console.log(dirent);
console.log(dirent.name);
lsDirs(dirent.name).catch(console.error)
}
}
}
var walk = function (dir, done) {
var results = [];
fs.readdir(dir, function (err, list) {
if (err) return done(err);
var i = 0;
(function next() {
var file = list[i++];
if (!file) return done(null, results);
file = path.resolve(dir, file);
fs.stat(file, function (err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function (err, res) {
results = results.concat(res);
next();
});
} else {
results.push(file);
next();
}
});
})();
});
};
/*
* The function* (function keyword plus asterisk) is a
* generator function, that returns a Generator object.
* - see mdn (https://mzl.la/3MbWmiM)
* yield can be treated like an array, you can iterate
* over it one item at a time. Use next() or for loop.
*/
async function* walker(dir) {
for await (const d of await fs.promises.opendir(dir)) {
const entry = path.join(dir, d.name);
if (d.isDirectory()) {
yield* walker(entry);
}
else if (d.isFile() && elfUtils.getExtension(d.name) === 'md') {
yield entry;
}
}
}
var ep = require("./exec-process.js");
async function getElfCode(fileName) {
let result = {};
const regexToc = /(?:<!-- toc(?:\s*stop)? -->)/g;
//const regexElf = /(?:<!-- bar(?:\s*stop)? -->)/g;
const regexElf = /^---/
let markdown = await elfUtils.readFileAsync(fileName);
debug(markdown);
if (regexToc.test(markdown)) {
debugCge('Has TOC code');
result.hasTocCode = true;
} else {
debugCge('No TOC code');
result.hasTocCode = false;
//result.markdown = markdown;
}
if (regexElf.test(markdown)) {
debugCge('Has ELF code');
result.hasElfCode = true;
} else {
debugCge('No ELF code');
result.hasElfCode = false;
// if (!result.markdown) result.markdown = markdown;
}
result.markdown = markdown;
return result;
}
const getTitleFromPath = function (fileName) {
const str = elfUtils.getEndFromCharacter(fileName, '/')
debug(str);
var result = str.replace( /([A-Z])/g, "$1" );
const finalResult = result.charAt(0).toUpperCase() + result.slice(1);
return elfUtils.stripExtension(finalResult);
};
async function addElfCode(fileName, elfCodes) {
//const elfStr = `\n<!-- bar -->\n<!-- barstop -->`;
const title = getTitleFromPath(fileName);
const elfStr = `\n---\nfullPath=${fileName}\ntitle=${title}\n---`;
const tocStr = `\n\n<!-- toc -->\n<!-- tocstop -->`;
if (elfCodes.hasElfCode + elfCodes.hasTocCode === 0) {
debug('aec Has no code');
elfCodes.markdown = elfStr + tocStr + '\n\n' + elfCodes.markdown;
} else if (!elfCodes.hasElfCode) {
debug('aec has no ELF code');
elfCodes.markdown = elfStr + '\n\n' + elfCodes.markdown;
} else if (!elfCodes.hasTocCode) {
debug('aec has no TOC code');
//elfCodes.markdown = tocStr + '\n\n' + elfCodes.markdown;
obj = matter(elfCodes.markdown);
obj.data.fullPath = fileName;
if (!obj.data.title) obj.data.title = title;
let margie = '';
for (const property in obj.data) {
margie += `${property}: ${obj.data[property]}\n`;
}
elfCodes.markdown = `\n---\n${margie}---` + tocStr + obj.content;
} else {
debug('aec has both but checking ELF code');
// elfCodes.markdown = tocStr + '\n\n' + elfCodes.markdown;
obj = matter(elfCodes.markdown);
obj.data.fullPath = fileName;
if (!obj.data.title) obj.data.title = title;
let margie = '';
for (const property in obj.data) {
margie += `${property}: ${obj.data[property]}\n`;
}
//const margie = JSON.stringify(obj.data);
elfCodes.markdown = `\n---\n${margie}---\n` + obj.content;
}
debug('aec final markdown', elfCodes.markdown);
}
function getDocBySlug(slug) {
const realSlug = slug.replace(/\.md$/, '')
const fullPath = join(docsDirectory, `${realSlug}.md`)
const fileContents = fs.readFileSync(fullPath, 'utf8')
const { data, content } = matter(fileContents)
return { slug: realSlug, meta: data, content }
}
// Then, use it with a simple async for loop
async function main() {
for await (const p of walker('elvenware')) {
const fileName = process.env.HOME + '/Git/CloudNotes/' + p
debug(getTitleFromPath(fileName));
const stats = await fsp.stat(fileName);
debug(stats);
// execProcess();
const command = "sh git-call.sh " + fileName;
//const result = await ep.callExec(command);
const result = await ep.result(command);
debug('cm result:', result);
const elfCodes = await getElfCode(fileName);
await addElfCode(fileName, elfCodes);
// debug('health codes', elfCodes);
/* if ((elfCodes.hasElfCode) + (elfCodes.hasTocCode) <= 1) {
debug('needs work');
await addElfCode(fileName, elfCodes);
} */
/* {
if(!err){
debug(response);
}else {
debug(err);
}
}); */
return;
}
}
//ls01('.').catch(console.error)
//ls02('.').catch(console.error)
//lsDirs('.').catch(console.error)
/* walk('elvenware', (err,b) => {
if (err) throw err;
debug(b)
}); */
//main().catch(console.error);
exports.getElfCode = getElfCode;