-
Notifications
You must be signed in to change notification settings - Fork 3
/
authoring.js
executable file
·51 lines (46 loc) · 1.52 KB
/
authoring.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
#!/usr/bin/env node
const path = require('path');
const { readdir, readFile, writeFile } = require('fs').promises;
const argv = require('@jcubic/lily')(process.argv.slice(2));
const { slugify, get } = require('.');
const dir = './recipes';
const cache = {};
function get_user_url(username) {
if (cache[username]) {
return Promise.resolve(cache[username]);
}
let auth;
if (argv.t) {
auth = argv.t;
}
return get(`https://api.github.com/users/${username}`, { auth }).then(user => {
const info = {
username: username,
url: user.blog || user.html_url,
name: user.name || username
};
cache[username] = info;
return info;
});
}
readdir(dir).then(files => {
files = files.filter(file => file.endsWith('.md'));
files.forEach((filename) => {
const fullname = path.join(dir, filename);
readFile(fullname).then(async file => {
file = file.toString();
const re = /@([^\s]+)/g;
let usernames = file.match(re);
if (usernames) {
usernames = usernames.map(name => name.substring(1));
const users = await Promise.all(usernames.map(get_user_url));
users.forEach(user => {
file = file.replace('@' + user.username, function(_) {
return `[${user.name}](${user.url})`;
});
});
writeFile(fullname, file);
}
});
});
});