consonance
Version:
Fix your typographic scale
82 lines (72 loc) • 1.62 kB
JavaScript
const fs = require('fs');
const R = require('ramda');
const cssstats = require('cssstats');
const getcss = require('get-css');
const fixtures = [
"http://github.com"
];
const options = {
timeout: 5000
};
const saveResponse = response => {
console.log(R.keys(response));
}
const fontSizeToPx = value => {
var raw;
if (typeof value !== 'string') {
value = value.toString();
}
raw = parseFloat(value, 10);
if (value.match(/px$/)) {
return raw;
}
if (value.match(/em$/)) {
return raw * 16;
}
if (value.match(/%$/)) {
return raw * .16;
}
switch (value) {
case 'inherit':
return 16;
case 'xx-small':
return 9;
case 'x-small':
return 10;
case 'small':
return 13;
case 'medium':
return 16;
case 'large':
return 18;
case 'x-large':
return 24;
case 'xx-large':
return 32;
case 'small':
return 13;
case 'larger':
return 19;
default:
return raw;
}
}
const catchError = error => {
console.error(`Can't get css because ${error}`);
}
const processFixture = url => {
const domain = url.replace(/.*?:\/\//g, '');
return getcss(url, options)
.then(R.prop('css'))
.then(cssstats)
.then((stats) => stats.declarations.getAllFontSizes())
.then(R.map(fontSizeToPx))
.then(R.uniq)
.then(R.sort((a, b) => a - b))
.then((response) => `module.exports = ${JSON.stringify(response, null, 2)}`)
.then((response) => {
fs.writeFileSync(`./docs/data/${domain}.js`, response);
})
.catch(catchError);
}
R.map(processFixture, fixtures);