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') {
|
2014-08-31 23:04:59 +08:00
|
|
|
if (/::-webkit-validation|:-moz-placeholder|^\.integrations-slide-content|^\.prose-diff|@font-face|^button::|^article$/.test(el.selectors[0])) {
|
2014-06-23 01:10:18 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-08 02:02:46 +08:00
|
|
|
|
2014-09-01 06:41:10 +08:00
|
|
|
// remove `body` from `body, input {}`
|
|
|
|
if (el.selectors[0] === 'body' && el.selectors[1] === 'input') {
|
|
|
|
el.selectors.shift();
|
|
|
|
}
|
|
|
|
|
2014-09-01 07:13:09 +08:00
|
|
|
// rename the .octoicons font
|
|
|
|
if (el.selectors[0] === '.octicon') {
|
|
|
|
el.declarations = el.declarations.map(function (el) {
|
|
|
|
if (el.property === 'font') {
|
|
|
|
el.value += '-anchor';
|
|
|
|
}
|
|
|
|
|
|
|
|
return el;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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) {
|
2014-09-01 06:41:10 +08:00
|
|
|
// remove everything from body/html other than these
|
|
|
|
if (/^font|^(?:line-height|color)$|text-size-adjust$/.test(declaration.property)) {
|
|
|
|
return true;
|
2014-06-23 01:10:18 +08:00
|
|
|
}
|
2014-06-21 12:58:51 +08:00
|
|
|
|
2014-09-01 06:41:10 +08:00
|
|
|
return false;
|
2014-06-23 01:10:18 +08:00
|
|
|
});
|
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-08-31 23:04:59 +08:00
|
|
|
var ret = css.stringify(style);
|
|
|
|
|
|
|
|
ret = '@font-face {\n font-family: octicons-anchor;\n src: url(data:font/woff;charset=utf-8;base64,d09GRgABAAAAAAYcAA0AAAAACjQAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABGRlRNAAABMAAAABwAAAAca8vGTk9TLzIAAAFMAAAARAAAAFZG1VHVY21hcAAAAZAAAAA+AAABQgAP9AdjdnQgAAAB0AAAAAQAAAAEACICiGdhc3AAAAHUAAAACAAAAAj//wADZ2x5ZgAAAdwAAADRAAABEKyikaNoZWFkAAACsAAAAC0AAAA2A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XAS7QaLwSc0Gn8NuAVWou/gFmnjLrEaEh9GmDdDGgL3B4JsrRPDU2hTOiMSuJUIdKQQayiAth69r6akSSFqIJuA19TrzCIaY8sIoxyrNIrL//pw7A2iMygkX5vDj+G+kuoLdX4GlGK/8Lnlz6/h9MpmoO9rafrz7ILXEHHaAx95s9lsI7AHNMBWEZHULnfAXwG9/ZqdzLI08iuwRloXE8kfhXYAvE23+23DU3t626rbs8/8adv+9DWknsHp3E17oCf+Z48rvEQNZ78paYM38qfk3v/u3l3u3GXN2Dmvmvpf1Srwk3pB/VSsp512bA/GG5i2WJ7wu430yQ5K3nFGiOqgtmSB5pJVSizwaacmUZzZhXLlZTq8qGGFY2YcSkqbth6aW1tRmlaCFs2016m5qn36SbJrqosG4uMV4aP2PHBmB3tjtmgN2izkGQyLWprekbIntJFing32a5rKWCN/SdSoga45EJykyQ7asZvHQ8PTm6cslIpwyeyjbVltNikc2HTR7YKh9LBl9DADC0U/jLcBZDKrMhUBfQBvXRzLtFtjU9eNHKin0x5InTqb8lNpfKv1s1xHzTXRqgKzek/mb7nB8RZTCDhGEX3kK/8Q75AmUM/eLkfA+0Hi908Kx4eNsMgudg5GLdRD7a84npi+YxNr5i5KIbW5izXas7cHXIMAau1OueZhfj+cOcP3P8MNIWLyYOBuxL6DRylJ4cAAAB4nGNgYoAALjDJyIAOWMCiTIxMLDmZedkABtIBygAAAA==) format(\'woff\');\n}\n\n' + ret;
|
|
|
|
|
|
|
|
return ret;
|
2014-05-08 02:02:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|