├── .gitignore ├── cli.js ├── index.js ├── package.json └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var fs = require('fs') 4 | var inliner = require('./') 5 | var inlined = inliner(fs.readFileSync(process.argv[2])).toString() 6 | console.log(inlined) -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var cheerio = require('cheerio') 2 | var path = require('path') 3 | var fs = require('fs') 4 | var url = require('url') 5 | var inliner = require('imageinliner') 6 | 7 | module.exports = function(html, base) { 8 | base = base || process.cwd() 9 | var dom = cheerio.load(String(html)) 10 | injectStyles(dom) 11 | return new Buffer(dom.html()) 12 | 13 | function injectStyles(dom) { 14 | dom('link').each(function(idx, el) { 15 | el = dom(el) 16 | var href = el.attr('href') 17 | if (el.attr('rel') === 'stylesheet' && isLocal(href)) { 18 | var dir = path.dirname(href) 19 | var file = path.join(base, href) 20 | var style = fs.readFileSync(file) 21 | var inlined = inliner.css(style.toString(), { cssBasePath: dir }) 22 | var inlinedTag = "' 23 | el.replaceWith(inlinedTag) 24 | } 25 | }) 26 | } 27 | 28 | function isLocal(href) { 29 | return href && !url.parse(href).hostname; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "inline-styles", 3 | "version": "1.0.0", 4 | "description": "replaces link tags with inline style tags + inlines CSS url() calls with data URIs", 5 | "main": "index.js", 6 | "bin": { 7 | "inline-styles": "cli.js" 8 | }, 9 | "author": "max ogden", 10 | "license": "BSD", 11 | "dependencies": { 12 | "cheerio": "git://github.com/cheeriojs/cheerio", 13 | "imageinliner": "^0.2.2" 14 | }, 15 | "devDependencies": {}, 16 | "scripts": { 17 | "test": "echo \"Error: no test specified\" && exit 1" 18 | }, 19 | "repository": { 20 | "type": "git", 21 | "url": "https://github.com/maxogden/inline-styles.git" 22 | }, 23 | "bugs": { 24 | "url": "https://github.com/maxogden/inline-styles/issues" 25 | }, 26 | "homepage": "https://github.com/maxogden/inline-styles" 27 | } 28 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # inline-styles 2 | 3 | replaces link tags with inline style tags + inlines CSS url() calls with data URIs 4 | 5 | ``` 6 | npm install inline-styles -g 7 | inline-styles index.html > inlined.html 8 | ``` --------------------------------------------------------------------------------