forked from sindresorhus/generate-github-markdown-css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (86 loc) · 2.95 KB
/
index.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
'use strict';
const got = require('got');
const cheerio = require('cheerio');
const uncss = require('uncss');
const pify = require('pify');
const fs = require('fs');
const path = require('path');
const uncssP = pify(uncss);
const getCSS = () => got('https://github.com').then(response => {
const ret = [];
const $ = cheerio.load(response.body);
$('link[href$=".css"]').each((i, el) => {
ret.push(el.attribs.href);
});
if (ret.length === 0) {
throw new Error('Could not find GitHub stylesheets');
}
return ret;
});
const getRenderedFixture = () => 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')
})
}).then(response => `<div class="markdown-body">\n${response.body}\n</div>`);
const cleanupCss = str => {
const css = require('css');
const style = css.parse(str);
const mdBodyProps = [];
style.stylesheet.rules = style.stylesheet.rules.filter(el => {
if (el.type === 'keyframes' || el.type === 'comment' || el.type === 'font-face') {
return false;
}
if (el.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$/.test(el.selectors[0])) {
return false;
}
// Work around GitHub Markdown API inconsistency #10
if (el.selectors[0] === '.task-list-item-checkbox') {
el.selectors[0] = '.task-list-item input';
}
// Remove `body` from `body, input {}`
if (el.selectors[0] === 'body' && el.selectors[1] === 'input') {
el.selectors.shift();
}
if (el.selectors.length === 1 && /^(?:html|body)$/.test(el.selectors[0])) {
// Remove everything from body/html other than these
el.declarations = el.declarations.filter(x => /^(?:line-height|color)$|text-size-adjust$/.test(x.property));
}
el.selectors = el.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 (el.selectors.length === 1 && el.selectors[0] === '.markdown-body') {
[].push.apply(mdBodyProps, el.declarations);
return false;
}
}
return el.declarations && el.declarations.length !== 0;
});
// Merge `.markdown-body` rules
style.stylesheet.rules.unshift({
type: 'rule',
selectors: ['.markdown-body'],
declarations: mdBodyProps
});
return css.stringify(style);
};
module.exports = () =>
Promise.all([
getRenderedFixture(),
getCSS()
])
.then(x => uncssP(x[0], {stylesheets: x[1], ignore: [/^\.pl|^\.tab-size/]}))
.then(cleanupCss);