Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/hexojs/hexo into fix-0x
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Feb 9, 2020
2 parents c680016 + b385a94 commit 987be53
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 257 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ Also, we welcome PR or issue to [official-plugins](https://github.com/hexojs).

<a href="https://www.jetbrains.com/"><img src="/.github/jetbrains-variant-4.svg" alt="JetBrains" width="200"/></a>

<a href="https://www.cmscritic.com/"><img src="/.github/CMS-Critic_logo-3.png" alt="CMS Critic" width="200"/></a>

<a href="https://getform.io/"><img src="/.github/getform-logo.svg" alt="Getform" width="200"/></a>

## License

[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fhexojs%2Fhexo.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fhexojs%2Fhexo?ref=badge_large)
10 changes: 5 additions & 5 deletions lib/box/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const { readFile, readFileSync, stat, statSync } = require('hexo-fs');

function File(data) {
this.source = data.source;
this.path = data.path;
this.params = data.params;
this.type = data.type;
function File({ source, path, params, type }) {
this.source = source;
this.path = path;
this.params = params;
this.type = type;
}

File.prototype.read = function(options, callback) {
Expand Down
40 changes: 21 additions & 19 deletions lib/extend/migrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,31 @@

const Promise = require('bluebird');

function Migrator() {
this.store = {};
}
class Migrator {
constructor() {
this.store = {};
}

Migrator.prototype.list = function() {
return this.store;
};
list() {
return this.store;
}

Migrator.prototype.get = function(name) {
return this.store[name];
};
get(name) {
return this.store[name];
}

Migrator.prototype.register = function(name, fn) {
if (!name) throw new TypeError('name is required');
if (typeof fn !== 'function') throw new TypeError('fn must be a function');
register(name, fn) {
if (!name) throw new TypeError('name is required');
if (typeof fn !== 'function') throw new TypeError('fn must be a function');

if (fn.length > 1) {
fn = Promise.promisify(fn);
} else {
fn = Promise.method(fn);
}
if (fn.length > 1) {
fn = Promise.promisify(fn);
} else {
fn = Promise.method(fn);
}

this.store[name] = fn;
};
this.store[name] = fn;
}
}

module.exports = Migrator;
48 changes: 25 additions & 23 deletions lib/extend/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,36 @@
const Promise = require('bluebird');
const { Pattern } = require('hexo-util');

function Processor() {
this.store = [];
}
class Processor {
constructor() {
this.store = [];
}

list() {
return this.store;
}

Processor.prototype.list = function() {
return this.store;
};
register(pattern, fn) {
if (!fn) {
if (typeof pattern === 'function') {
fn = pattern;
pattern = /(.*)/;
} else {
throw new TypeError('fn must be a function');
}
}

Processor.prototype.register = function(pattern, fn) {
if (!fn) {
if (typeof pattern === 'function') {
fn = pattern;
pattern = /(.*)/;
if (fn.length > 1) {
fn = Promise.promisify(fn);
} else {
throw new TypeError('fn must be a function');
fn = Promise.method(fn);
}
}

if (fn.length > 1) {
fn = Promise.promisify(fn);
} else {
fn = Promise.method(fn);
this.store.push({
pattern: new Pattern(pattern),
process: fn
});
}

this.store.push({
pattern: new Pattern(pattern),
process: fn
});
};
}

module.exports = Processor;
10 changes: 5 additions & 5 deletions test/scripts/console/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('publish', () => {
const now = Date.now();
let clock;

before(async() => {
before(async () => {
clock = useFakeTimers(now);

await mkdirs(hexo.base_dir);
Expand Down Expand Up @@ -44,7 +44,7 @@ describe('publish', () => {
layout: 'draft'
}));

it('slug', async() => {
it('slug', async () => {
const draftPath = join(hexo.source_dir, '_drafts', 'Hello-World.md');
const path = join(hexo.source_dir, '_posts', 'Hello-World.md');
const date = moment(now);
Expand All @@ -70,7 +70,7 @@ describe('publish', () => {
await unlink(path);
});

it('layout', async() => {
it('layout', async () => {
const path = join(hexo.source_dir, '_posts', 'Hello-World.md');
const date = moment(now);

Expand All @@ -92,7 +92,7 @@ describe('publish', () => {
await unlink(path);
});

it('rename if target existed', async() => {
it('rename if target existed', async () => {
const path = join(hexo.source_dir, '_posts', 'Hello-World-1.md');

await post.create({
Expand All @@ -111,7 +111,7 @@ describe('publish', () => {
]);
});

it('replace existing target', async() => {
it('replace existing target', async () => {
const path = join(hexo.source_dir, '_posts', 'Hello-World.md');

await post.create({
Expand Down
138 changes: 69 additions & 69 deletions test/scripts/models/post_asset.js
Original file line number Diff line number Diff line change
@@ -1,110 +1,110 @@
'use strict';

const sinon = require('sinon');
const pathFn = require('path');
const { join } = require('path');

describe('PostAsset', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo();
const PostAsset = hexo.model('PostAsset');
const Post = hexo.model('Post');
let post;
const defaults = require('../../../lib/hexo/default_config');

before(() => hexo.init().then(() => Post.insert({
source: 'foo.md',
slug: 'bar'
})).then(post_ => {
post = post_;
}));

it('default values', () => PostAsset.insert({
_id: 'foo',
slug: 'foo',
post: post._id
}).then(data => {
data.modified.should.be.true;
return PostAsset.removeById(data._id);
}));

it('_id - required', () => {
const errorCallback = sinon.spy(err => {
err.should.have.property('message', 'ID is not defined');
before(async () => {
await hexo.init();
post = await Post.insert({
source: 'foo.md',
slug: 'bar'
});
});

return PostAsset.insert({}).catch(errorCallback).finally(() => {
errorCallback.calledOnce.should.be.true;
});
beforeEach(() => {
hexo.config = Object.assign({}, defaults);
});

it('slug - required', () => {
const errorCallback = sinon.spy(err => {
err.should.have.property('message', '`slug` is required!');
it('default values', async () => {
const data = await PostAsset.insert({
_id: 'foo',
slug: 'foo',
post: post._id
});
data.modified.should.eql(true);
PostAsset.removeById(data._id);
});

return PostAsset.insert({
_id: 'foo'
}).catch(errorCallback).finally(() => {
errorCallback.calledOnce.should.be.true;
});
it('_id - required', async () => {
try {
await PostAsset.insert({});
} catch (err) {
err.message.should.eql('ID is not defined');
}
});

it('path - virtual', () => PostAsset.insert({
_id: 'source/_posts/test/foo.jpg',
slug: 'foo.jpg',
post: post._id
}).then(data => {
data.path.should.eql(pathFn.join(post.path, data.slug));
return PostAsset.removeById(data._id);
}));
it('slug - required', async () => {
try {
await PostAsset.insert({
_id: 'foo'
});
} catch (err) {
err.message.should.eql('`slug` is required!');
}
});

it('path - virtual', async () => {
const data = await PostAsset.insert({
_id: 'source/_posts/test/foo.jpg',
slug: 'foo.jpg',
post: post._id
});
data.path.should.eql(join(post.path, data.slug));

PostAsset.removeById(data._id);
});

it('path - virtual - when permalink is .html', () => {
it('path - virtual - when permalink is .html', async () => {
hexo.config.permalink = ':year/:month/:day/:title.html';
return PostAsset.insert({
const data = await PostAsset.insert({
_id: 'source/_posts/test/foo.html',
slug: 'foo.htm',
post: post._id
}).then(data => {
data.path.should.eql(pathFn.join(post.path, data.slug));
return PostAsset.removeById(data._id);
}).finally(() => {
hexo.config.permalink = ':year/:month/:day/:title';
});
data.path.should.eql(join(post.path, data.slug));

PostAsset.removeById(data._id);
});

it('path - virtual - when permalink is .htm', () => {
it('path - virtual - when permalink is .htm', async () => {
hexo.config.permalink = ':year/:month/:day/:title.htm';
return PostAsset.insert({
const data = await PostAsset.insert({
_id: 'source/_posts/test/foo.htm',
slug: 'foo.htm',
post: post._id
}).then(data => {
data.path.should.eql(pathFn.join(post.path, data.slug));
return PostAsset.removeById(data._id);
}).finally(() => {
hexo.config.permalink = ':year/:month/:day/:title';
});
data.path.should.eql(join(post.path, data.slug));

PostAsset.removeById(data._id);
});

it('path - virtual - when permalink contains .htm not in the end', () => {
it('path - virtual - when permalink contains .htm not in the end', async () => {
hexo.config.permalink = ':year/:month/:day/:title/.htm-foo/';
return PostAsset.insert({
const data = await PostAsset.insert({
_id: 'source/_posts/test/foo.html',
slug: 'foo.html',
post: post._id
}).then(data => {
data.path.should.eql(pathFn.join(post.path + '.htm-foo/', data.slug));
return PostAsset.removeById(data._id);
}).finally(() => {
hexo.config.permalink = ':year/:month/:day/:title';
});
data.path.should.eql(join(post.path + '.htm-foo/', data.slug));

PostAsset.removeById(data._id);
});

it('source - virtual', () => PostAsset.insert({
_id: 'source/_posts/test/foo.jpg',
slug: 'foo.jpg',
post: post._id
}).then(data => {
data.source.should.eql(pathFn.join(hexo.base_dir, data._id));
return PostAsset.removeById(data._id);
}));
it('source - virtual', async () => {
const data = await PostAsset.insert({
_id: 'source/_posts/test/foo.jpg',
slug: 'foo.jpg',
post: post._id
});
data.source.should.eql(join(hexo.base_dir, data._id));

PostAsset.removeById(data._id);
});
});
Loading

0 comments on commit 987be53

Please sign in to comment.