-
Notifications
You must be signed in to change notification settings - Fork 0
/
refinery.js
136 lines (124 loc) · 3.07 KB
/
refinery.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
/*
Refinery
Post-processing of Github json files
*/
const yaml = require('js-yaml')
const mkd = require('markdown-it')({ html: true })
/**
* Convert a base64 in utf8.
*
* @param {string} base64 - string encode in base64.
* @return {String} utf8 - utf8 content.
*/
const base64ToUtf8 = base64 =>
Buffer.from(base64, 'base64').toString('utf8')
/**
* Check if file has valids markdown extension.
*
* @param {String} filepath - path and filename.
* @return {boolean} - is valid or not.
*/
const isMkdExt = filepath =>
filepath.match(/(.markdown||.mdown||.mkdn||.mkd||.md)$/)[0] !== ''
/**
* Remove metas.
*
* @param {string} mkd - mkd content.
* @return {String} mkd - mkd without meta.
*/
const removeMetas = mkd => {
try {
return mkd.match(/---\n[\s\S]*?\n---\n([\s\S]*)/)[1]
} catch (e) {
return mkd
}
}
/**
* Convert a base64 markdown in html.
*
* @param {string} base64Mkd - string encode in base64.
* @return {String} utf8 - utf8 content.
*/
const contentFromMkdBase64 = base64Mkd => mkd.render(removeMetas(base64ToUtf8(base64Mkd)))
/**
* Get Github yaml metas from Github ressource.
*
* @param {string} mdBase64 - markdown encode in base64.
* @return {String} metas - metas in yaml format.
*/
const metasFromMkdBase64 = base64Mkd => {
try {
return yaml.load(base64ToUtf8(base64Mkd).match(/---\n([\s\S]*?)\n---/)[1])
} catch (e) {
return undefined
}
}
/**
* Refine raw Github json file.
*
* @param {Object} ghMkdFile - Github json file.
* @return {Object} jsonFile - markdown file refined.
*/
const ghMkd = ghMkdFile =>
({
name: ghMkdFile.name,
url: ghMkdFile.url,
type: ghMkdFile.type,
meta: metasFromMkdBase64(ghMkdFile.content),
body: contentFromMkdBase64(ghMkdFile.content),
full_name: ghMkdFile.html_url.replace('https://github.com/', '')
})
/**
* Refine raw Github json folder.
*
* @param {Object} ghMkdFolder - Github json folder.
* @return {Object} jsonFolder - folder refined.
*/
const ghFolder = ghFolder =>
({
name: ghFolder.name,
url: ghFolder.url,
type: ghFolder.type,
full_name: ghFolder.html_url.replace('https://github.com/', '')
})
/**
* Breadcrumb data for user.
*
* @param {String} filepath - path and filename.
* @return {boolean} - is valid or not.
*/
const breadcrumbOwner = path =>
([{ link: path.owner, title: path.owner }])
/**
* Breadcrumb data for repo.
*
* @param {String} filepath - path and filename.
* @return {boolean} - is valid or not.
*/
const breadcrumbRepo = path =>
breadcrumbOwner(path).concat([
{ link: `${path.owner}/${path.repo}/${path.branch}`, title: path.repo }
])
/**
* Breadcrumb data for tree.
*
* @param {String} filepath - path and filename.
* @return {boolean} - is valid or not.
*/
const breadcrumbTree = path =>
breadcrumbRepo(path).concat([
{ link: `${path.owner}/${path.repo}/tree/${path.branch}/${path.path}`, title: path.path }
])
module.exports = {
base64ToUtf8,
removeMetas,
contentFromMkdBase64,
metasFromMkdBase64,
// public
isMkdExt,
ghMkd,
ghFolder,
breadcrumbOwner,
breadcrumbRepo,
breadcrumbTree
}