github-markdown-css/index.js
2014-09-01 00:41:10 +02:00

143 lines
5.0 KiB
JavaScript

'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) {
cb(err);
return;
}
var ret = [];
var $ = cheerio.load(data);
$('link[href*="assets/github"]').each(function (i, el) {
ret.push(el.attribs.href);
});
if (ret.length === 0) {
cb(new Error('Could not find GitHub stylesheets'));
return;
}
cb(null, ret);
});
}
function getRenderedFixture(cb) {
var url = 'https://github.com/sindresorhus/github-markdown-css/blob/gh-pages/fixture.md';
got(url, function (err, data) {
if (err) {
cb(err);
return;
}
var $ = cheerio.load(data);
var html = $('.markdown-body').parent().html();
cb(null, html);
});
}
function cleanupCss(str) {
var css = require('css');
var style = css.parse(str);
var mdBodyProps = [];
style.stylesheet.rules = style.stylesheet.rules.filter(function (el) {
if (el.type === 'keyframes' || el.type === 'comment' || el.type === 'font-face') {
return false;
}
if (el.type ==='rule') {
if (/::-webkit-validation|:-moz-placeholder|^\.integrations-slide-content|^\.prose-diff|@font-face|^button::|^article$/.test(el.selectors[0])) {
return false;
}
// 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])) {
el.declarations = el.declarations.filter(function (declaration) {
// remove everything from body/html other than these
if (/^font|^(?:line-height|color)$|text-size-adjust$/.test(declaration.property)) {
return true;
}
return false;
});
}
el.selectors = el.selectors.map(function (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;
}
}
if (el.declarations.length === 0) {
return false;
}
return true;
});
// merge `.markdown-body` rules
style.stylesheet.rules.unshift({
type: 'rule',
selectors: ['.markdown-body'],
declarations: mdBodyProps
});
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//wADZ2x5ZgAAAdwAAADRAAABEKyikaNoZWFkAAACsAAAAC0AAAA2AtXoA2hoZWEAAALgAAAAHAAAACQHngNFaG10eAAAAvwAAAAQAAAAEAwAACJsb2NhAAADDAAAAAoAAAAKALIAVG1heHAAAAMYAAAAHwAAACABEAB2bmFtZQAAAzgAAALBAAAFu3I9x/Nwb3N0AAAF/AAAAB0AAAAvaoFvbwAAAAEAAAAAzBdyYwAAAADP2IQvAAAAAM/bz7t4nGNgZGFgnMDAysDB1Ml0hoGBoR9CM75mMGLkYGBgYmBlZsAKAtJcUxgcPsR8iGF2+O/AEMPsznAYKMwIkgMA5REMOXicY2BgYGaAYBkGRgYQsAHyGMF8FgYFIM0ChED+h5j//yEk/3KoSgZGNgYYk4GRCUgwMaACRoZhDwCs7QgGAAAAIgKIAAAAAf//AAJ4nHWMMQrCQBBF/0zWrCCIKUQsTDCL2EXMohYGSSmorScInsRGL2DOYJe0Ntp7BK+gJ1BxF1stZvjz/v8DRghQzEc4kIgKwiAppcA9LtzKLSkdNhKFY3HF4lK69ExKslx7Xa+vPRVS43G98vG1DnkDMIBUgFN0MDXflU8tbaZOUkXUH0+U27RoRpOIyCKjbMCVejwypzJJG4jIwb43rfl6wbwanocrJm9XFYfskuVC5K/TPyczNU7b84CXcbxks1Un6H6tLH9vf2LRnn8Ax7A5WQAAAHicY2BkYGAA4teL1+yI57f5ysDNwgAC529f0kOmWRiYVgEpDgYmEA8AUzEKsQAAAHicY2BkYGB2+O/AEMPCAAJAk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\');\n}\n\n' + ret;
return ret;
}
module.exports = function (cb) {
getRenderedFixture(function (err, html) {
if (err) {
cb(err);
return;
}
getCss(function (err, stylesheets) {
if (err) {
cb(err);
return;
}
uncss(html, {
stylesheets: stylesheets,
ignore: [/^\.highlight/]
}, function (err, css) {
if (err) {
throw err;
}
cb(null, cleanupCss(css));
});
});
});
};