Skip to content

Commit

Permalink
Improve "hexo-config" method
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Mar 11, 2015
1 parent be966b2 commit 1e9bf4c
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 28 deletions.
22 changes: 17 additions & 5 deletions lib/renderer.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
var stylus = require('stylus');
var nib = require('nib');

function getProperty(obj, key){
key = key.replace(/\[(\w+)\]/g, '.$1').replace(/^\./, '');
function getProperty(obj, name){
name = name.replace(/\[(\w+)\]/g, '.$1').replace(/^\./, '');

var split = key.split('.');
var result = obj[split[0]];
var split = name.split('.');
var key = split.shift();

if (!obj.hasOwnProperty(key)) return '';

var result = obj[key];
var len = split.length;

if (!len) return result || '';
if (typeof result !== 'object') return '';

for (var i = 0; i < len; i++){
key = split[i];
if (!result.hasOwnProperty(key)) return '';

for (var i = 1, len = split.length; i < len; i++){
result = result[split[i]];
if (typeof result !== 'object') return result;
}

return result;
Expand Down
92 changes: 69 additions & 23 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,18 @@ describe('Stylus renderer', function(){

var r = require('../lib/renderer').bind(ctx);

var body = [
'.foo',
' color: red',
' content: hexo-config("foo")',
'',
'.bar',
' content: hexo-config("bar.baz")',
'',
'.baz',
' content: hexo-config("bar[baz]")'
].join('\n');

it('default', function(){
ctx.config.stylus.compress = false;
var body = [
'.foo',
' color: red'
].join('\n');

r({text: body}, {}, function(err, result){
should.not.exist(err);
if (err) throw err;

result.should.eql([
'.foo {',
' color: #f00;',
' content: 1;',
'}',
'.bar {',
' content: 2;',
'}',
'.baz {',
' content: 2;',
'}'
].join('\n') + '\n');
});
Expand All @@ -55,9 +39,71 @@ describe('Stylus renderer', function(){
it('compress', function(){
ctx.config.stylus.compress = true;

var body = [
'.foo',
' color: red'
].join('\n');

r({text: body}, {}, function(err, result){
if (err) throw err;

ctx.config.stylus.compress = false;
result.should.eql('.foo{color:#f00}');
});
});

it('hexo-config', function(){
var body = [
// first depth and exist
'.foo',
' content: hexo-config("foo")',
'',
// second depth and exist
'.bar',
' content: hexo-config("bar.baz")',
'',
// another style for nested attribute
'.baz',
' content: hexo-config("bar[baz]")',
'',
// nested attribute does not exist
'.boo',
' content: hexo-config("bar.boo")',
'',
// config does not exist
'.test',
' content: hexo-config("boo")',
'',
// try to get nested attribute in non-object
'.app',
' content: hexo-config("foo.test")'
].join('\n');

r({text: body}, {}, function(err, result){
should.not.exist(err);
result.should.eql('.foo{color:#f00;content:1}.bar{content:2}.baz{content:2}');
if (err) throw err;

console.log(result);

result.should.eql([
'.foo {',
' content: 1;',
'}',
'.bar {',
' content: 2;',
'}',
'.baz {',
' content: 2;',
'}',
'.boo {',
' content: \'\';',
'}',
'.test {',
' content: \'\';',
'}',
'.app {',
' content: \'\';',
'}'
].join('\n') + '\n');
});
});
});

0 comments on commit 1e9bf4c

Please sign in to comment.