forked from sindresorhus/generate-github-markdown-css
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
182 lines (150 loc) · 5.29 KB
/
index.js
File metadata and controls
182 lines (150 loc) · 5.29 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
'use strict';
const fs = require('fs');
const path = require('path');
const got = require('got');
const cheerio = require('cheerio');
const uncss = require('uncss');
const pify = require('pify');
const uncssP = pify(uncss);
const manuallyAddedStyle = `
.markdown-body .octicon {
display: inline-block;
fill: currentColor;
vertical-align: text-bottom;
}
.markdown-body .anchor {
float: left;
line-height: 1;
margin-left: -20px;
padding-right: 4px;
}
.markdown-body .anchor:focus {
outline: none;
}
.markdown-body h1 .octicon-link,
.markdown-body h2 .octicon-link,
.markdown-body h3 .octicon-link,
.markdown-body h4 .octicon-link,
.markdown-body h5 .octicon-link,
.markdown-body h6 .octicon-link {
color: #1b1f23;
vertical-align: middle;
visibility: hidden;
}
.markdown-body h1:hover .anchor,
.markdown-body h2:hover .anchor,
.markdown-body h3:hover .anchor,
.markdown-body h4:hover .anchor,
.markdown-body h5:hover .anchor,
.markdown-body h6:hover .anchor {
text-decoration: none;
}
.markdown-body h1:hover .anchor .octicon-link,
.markdown-body h2:hover .anchor .octicon-link,
.markdown-body h3:hover .anchor .octicon-link,
.markdown-body h4:hover .anchor .octicon-link,
.markdown-body h5:hover .anchor .octicon-link,
.markdown-body h6:hover .anchor .octicon-link {
visibility: visible;
}
.markdown-body h1:hover .anchor .octicon-link:before,
.markdown-body h2:hover .anchor .octicon-link:before,
.markdown-body h3:hover .anchor .octicon-link:before,
.markdown-body h4:hover .anchor .octicon-link:before,
.markdown-body h5:hover .anchor .octicon-link:before,
.markdown-body h6:hover .anchor .octicon-link:before {
width: 16px;
height: 16px;
content: ' ';
display: inline-block;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' version='1.1' width='16' height='16' aria-hidden='true'%3E%3Cpath fill-rule='evenodd' d='M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z'%3E%3C/path%3E%3C/svg%3E");
}
`.trim();
const getCSS = async () => {
const {body} = await got('https://github.com');
const $ = cheerio.load(body);
const ret = [];
$('link[href$=".css"]').each((index, element) => {
ret.push(element.attribs.href);
});
if (ret.length === 0) {
throw new Error('Could not find GitHub stylesheets');
}
return ret;
};
const getRenderedFixture = async () => {
const {body} = await got.post('https://api.github.com/markdown', {
headers: {
'content-type': 'application/json',
'user-agent': 'generate-github-markdown-css'
},
body: JSON.stringify({
mode: 'gfm',
context: 'sindresorhus/generate-github-markdown-css',
text: fs.readFileSync(path.join(__dirname, 'fixture.md'), 'utf8')
})
});
return `<div class="markdown-body">\n${body}\n</div>`;
};
const cleanupCss = str => {
const css = require('css');
const style = css.parse(str);
const markdownBodyProperties = [];
style.stylesheet.rules = style.stylesheet.rules.filter(element => {
if (element.type === 'keyframes' || element.type === 'comment' || element.type === 'font-face') {
return false;
}
if (element.type === 'rule') {
if (/::-webkit-validation|[:-]placeholder$|^\.placeholder-box$|^\.integrations-slide-content|^\.prose-diff|@font-face|^button::|^article$|^\.plan-|^\.plans-|^\.repo-config-option|\.site-search|^::-webkit-file-upload-button$|^input::-webkit-outer-spin-button$|^\.select-menu-item|^\.hx_SelectMenu-item--input/.test(element.selectors[0])) {
return false;
}
// Work around GitHub Markdown API inconsistency #10
if (element.selectors[0] === '.task-list-item-checkbox') {
element.selectors[0] = '.task-list-item input';
}
// Remove `body` from `body, input {}`
if (element.selectors[0] === 'body' && element.selectors[1] === 'input') {
element.selectors.shift();
}
if (element.selectors.length === 1 && /^(?:html|body)$/.test(element.selectors[0])) {
// Remove everything from body/html other than these
element.declarations = element.declarations.filter(x => /^(?:line-height|color)$|text-size-adjust$/.test(x.property));
}
element.selectors = element.selectors.map(selector => {
if (/^(?:body|html)$/.test(selector)) {
selector = '.markdown-body';
}
if (!/\.markdown-body/.test(selector)) {
selector = `.markdown-body ${selector}`;
}
return selector;
});
// Collect `.markdown-body` rules
if (element.selectors.length === 1 && element.selectors[0] === '.markdown-body') {
[].push.apply(markdownBodyProperties, element.declarations);
return false;
}
}
return element.declarations && element.declarations.length !== 0;
});
// Merge `.markdown-body` rules
style.stylesheet.rules.unshift({
type: 'rule',
selectors: ['.markdown-body'],
declarations: markdownBodyProperties
});
return css.stringify(style);
};
module.exports = async () => {
const [fixture, cssString] = await Promise.all([
getRenderedFixture(),
getCSS()
]);
const css = await uncssP(fixture, {
stylesheets: cssString,
ignore: [
/^\.pl|^\.tab-size/
]
});
return manuallyAddedStyle + cleanupCss(css);
};