Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: es6-fy #34

Merged
merged 1 commit into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global hexo */
'use strict';

var renderer = require('./lib/renderer');
const renderer = require('./lib/renderer');

hexo.extend.renderer.register('styl', 'css', renderer);
hexo.extend.renderer.register('stylus', 'css', renderer);
23 changes: 9 additions & 14 deletions lib/renderer.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
'use strict';

var stylus, nib;
const stylus = require('stylus');
const nib = require('nib');

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

var split = name.split('.');
var key = split.shift();
const split = name.split('.');
let key = split.shift();

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

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

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

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

Expand All @@ -28,14 +29,8 @@ function getProperty(obj, name) {
}

module.exports = function(data, options, callback) {
// Lazy require
if (!stylus) {
stylus = require('stylus');
nib = require('nib');
}

var config = this.config.stylus || {};
var self = this;
const config = this.config.stylus || {};
const self = this;

function defineConfig(style) {
style.define('hexo-config', function(data) {
Expand Down
34 changes: 17 additions & 17 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

var should = require('chai').should(); // eslint-disable-line
const should = require('chai').should(); // eslint-disable-line

describe('Stylus renderer', function() {
var ctx = {
describe('Stylus renderer', () => {
const ctx = {
config: {
stylus: {
compress: false
Expand All @@ -23,17 +23,17 @@ describe('Stylus renderer', function() {
}
};

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

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

ctx.config.stylus = null;
r({text: body}, {}, function(err, result) {
r({text: body}, {}, (err, result) => {
if (err) throw err;

ctx.config.stylus = config;
Expand All @@ -45,13 +45,13 @@ describe('Stylus renderer', function() {
});
});

it('default', function() {
var body = [
it('default', () => {
const body = [
'.foo',
' color: red'
].join('\n');

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

result.should.eql([
Expand All @@ -62,24 +62,24 @@ describe('Stylus renderer', function() {
});
});

it('compress', function() {
it('compress', () => {
ctx.config.stylus.compress = true;

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

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

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

it('hexo-config', function() {
var body = [
it('hexo-config', () => {
const body = [
// first depth and exist
'.foo',
' content: hexo-config("foo")',
Expand Down Expand Up @@ -114,7 +114,7 @@ describe('Stylus renderer', function() {
' content: i'
].join('\n');

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

result.should.eql([
Expand Down