renderer.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict';
  2. var stylus, nib;
  3. function getProperty(obj, name) {
  4. name = name.replace(/\[(\w+)\]/g, '.$1').replace(/^\./, '');
  5. var split = name.split('.');
  6. var key = split.shift();
  7. if (!obj.hasOwnProperty(key)) return '';
  8. var result = obj[key];
  9. var len = split.length;
  10. if (!len) return result || '';
  11. if (typeof result !== 'object') return '';
  12. for (var i = 0; i < len; i++) {
  13. key = split[i];
  14. if (!result.hasOwnProperty(key)) return '';
  15. result = result[split[i]];
  16. if (typeof result !== 'object') return result;
  17. }
  18. return result;
  19. }
  20. module.exports = function(data, options, callback) {
  21. // Lazy require
  22. if (!stylus) {
  23. stylus = require('stylus');
  24. nib = require('nib');
  25. }
  26. var config = this.config.stylus || {};
  27. var self = this;
  28. function defineConfig(style) {
  29. style.define('hexo-config', function(data) {
  30. return getProperty(self.theme.config, data.val);
  31. });
  32. }
  33. stylus(data.text)
  34. .use(nib())
  35. .use(defineConfig)
  36. .set('filename', data.path)
  37. .set('sourcemap', config.sourcemaps)
  38. .set('compress', config.compress)
  39. .set('include css', true)
  40. .render(callback);
  41. };