2014-05-08 02:02:46 +08:00
|
|
|
'use strict';
|
|
|
|
var got = require('got');
|
|
|
|
var cheerio = require('cheerio');
|
|
|
|
var uncss = require('uncss');
|
|
|
|
|
|
|
|
function getCss(cb) {
|
|
|
|
got('https://github.com', function (err, data) {
|
|
|
|
if (err) {
|
2014-08-19 07:14:43 +08:00
|
|
|
cb(err);
|
|
|
|
return;
|
2014-05-08 02:02:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var ret = [];
|
|
|
|
var $ = cheerio.load(data);
|
|
|
|
|
|
|
|
$('link[href*="assets/github"]').each(function (i, el) {
|
|
|
|
ret.push(el.attribs.href);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (ret.length === 0) {
|
2014-08-19 07:14:43 +08:00
|
|
|
cb(new Error('Could not find GitHub stylesheets'));
|
|
|
|
return;
|
2014-05-08 02:02:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, ret);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRenderedFixture(cb) {
|
2014-08-08 16:00:19 +08:00
|
|
|
var url = 'https://github.com/sindresorhus/github-markdown-css/blob/gh-pages/fixture.md';
|
|
|
|
|
|
|
|
got(url, function (err, data) {
|
2014-05-08 02:02:46 +08:00
|
|
|
if (err) {
|
2014-08-19 07:14:43 +08:00
|
|
|
cb(err);
|
|
|
|
return;
|
2014-05-08 02:02:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var $ = cheerio.load(data);
|
2014-08-08 16:00:19 +08:00
|
|
|
var html = $('.markdown-body').parent().html();
|
2014-05-08 02:02:46 +08:00
|
|
|
|
2014-08-08 16:00:19 +08:00
|
|
|
cb(null, html);
|
2014-05-08 02:02:46 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanupCss(str) {
|
|
|
|
var css = require('css');
|
|
|
|
var style = css.parse(str);
|
2014-06-23 00:56:54 +08:00
|
|
|
var mdBodyProps = [];
|
2014-05-08 02:02:46 +08:00
|
|
|
|
|
|
|
style.stylesheet.rules = style.stylesheet.rules.filter(function (el) {
|
2014-06-23 01:10:18 +08:00
|
|
|
if (el.type === 'keyframes' || el.type === 'comment' || el.type === 'font-face') {
|
2014-05-08 02:02:46 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-23 01:10:18 +08:00
|
|
|
if (el.type ==='rule') {
|
|
|
|
if (/::-webkit-validation|:-moz-placeholder|^\.integrations-slide-content|^\.prose-diff|@font-face|^\.octicon|^button::|^\.markdown-body .+(:hover|\.octicon)|^article$/.test(el.selectors[0])) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-08 02:02:46 +08:00
|
|
|
|
2014-06-23 01:10:18 +08:00
|
|
|
if (el.selectors.length === 1 && /^(?:html|body)$/.test(el.selectors[0])) {
|
|
|
|
el.declarations = el.declarations.filter(function (declaration) {
|
|
|
|
if (!/^font|^(?:line-height|color)$|text-size-adjust$/.test(declaration.property)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-21 12:58:51 +08:00
|
|
|
|
2014-06-23 01:10:18 +08:00
|
|
|
return true;
|
|
|
|
});
|
2014-06-21 12:58:51 +08:00
|
|
|
}
|
|
|
|
|
2014-06-23 01:10:18 +08:00
|
|
|
el.selectors = el.selectors.map(function (selector) {
|
|
|
|
if (/^(?:body|html)$/.test(selector)) {
|
|
|
|
selector = '.markdown-body';
|
|
|
|
}
|
2014-06-21 12:58:51 +08:00
|
|
|
|
2014-06-23 01:10:18 +08:00
|
|
|
if (!/\.markdown-body/.test(selector)) {
|
|
|
|
selector = '.markdown-body ' + selector;
|
|
|
|
}
|
2014-06-21 12:58:51 +08:00
|
|
|
|
2014-06-23 01:10:18 +08:00
|
|
|
return selector;
|
|
|
|
});
|
|
|
|
|
|
|
|
// collect `.markdown-body` rules
|
|
|
|
if (el.selectors.length === 1 && el.selectors[0] === '.markdown-body') {
|
|
|
|
[].push.apply(mdBodyProps, el.declarations);
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-23 00:56:54 +08:00
|
|
|
}
|
|
|
|
|
2014-06-23 01:10:18 +08:00
|
|
|
if (el.declarations.length === 0) {
|
2014-06-23 00:56:54 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-08 02:02:46 +08:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2014-06-23 00:56:54 +08:00
|
|
|
// merge `.markdown-body` rules
|
|
|
|
style.stylesheet.rules.unshift({
|
|
|
|
type: 'rule',
|
|
|
|
selectors: ['.markdown-body'],
|
|
|
|
declarations: mdBodyProps
|
|
|
|
});
|
|
|
|
|
2014-05-08 02:02:46 +08:00
|
|
|
return css.stringify(style);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = function (cb) {
|
2014-08-08 16:00:19 +08:00
|
|
|
getRenderedFixture(function (err, html) {
|
2014-05-08 02:02:46 +08:00
|
|
|
if (err) {
|
2014-08-19 07:14:43 +08:00
|
|
|
cb(err);
|
|
|
|
return;
|
2014-05-08 02:02:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getCss(function (err, stylesheets) {
|
|
|
|
if (err) {
|
2014-08-19 07:14:43 +08:00
|
|
|
cb(err);
|
|
|
|
return;
|
2014-05-08 02:02:46 +08:00
|
|
|
}
|
|
|
|
|
2014-08-08 16:00:19 +08:00
|
|
|
uncss(html, {
|
2014-05-08 02:02:46 +08:00
|
|
|
stylesheets: stylesheets,
|
|
|
|
ignore: [/^\.highlight/]
|
|
|
|
}, function (err, css) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, cleanupCss(css));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|