Skip to content

Commit

Permalink
Merge pull request #3235 from tomap/master
Browse files Browse the repository at this point in the history
feat: Use date instead of file mtime for updated date
  • Loading branch information
curbengh authored Sep 21, 2019
2 parents 7d4b6f2 + 677f5b8 commit 6f6d04e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/hexo/default_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ module.exports = {
// Date / Time format
date_format: 'YYYY-MM-DD',
time_format: 'HH:mm:ss',
// Use post date for updated date instead of file modification date when front-matter provides no updated date
use_date_for_updated: false,
// Pagination
per_page: 10,
pagination_dir: 'page',
Expand Down
4 changes: 3 additions & 1 deletion lib/plugins/processor/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = ctx => {
const { path } = file;
const doc = Page.findOne({source: path});
const { config } = ctx;
const { timezone } = config;
const { timezone, use_date_for_updated } = config;

if (file.type === 'skip' && doc) {
return;
Expand Down Expand Up @@ -48,6 +48,8 @@ module.exports = ctx => {

if (data.updated) {
if (timezone) data.updated = common.timezone(data.updated, timezone);
} else if (use_date_for_updated) {
data.updated = data.date;
} else {
data.updated = stats.mtime;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/plugins/processor/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = ctx => {
const { path } = file.params;
const doc = Post.findOne({source: file.path});
const { config } = ctx;
const { timezone } = config;
const { timezone, use_date_for_updated } = config;
let categories, tags;

if (file.type === 'skip' && doc) {
Expand Down Expand Up @@ -84,6 +84,8 @@ module.exports = ctx => {

if (data.updated) {
if (timezone) data.updated = common.timezone(data.updated, timezone);
} else if (use_date_for_updated) {
data.updated = data.date;
} else {
data.updated = stats.mtime;
}
Expand Down
29 changes: 28 additions & 1 deletion test/scripts/processors/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,33 @@ describe('asset', () => {
});
});

it('page - use the date for updated if use_date_for_updated is set', () => {
const file = newFile({
path: 'hello.swig',
type: 'create',
renderable: true
});

hexo.config.use_date_for_updated = true;

return fs.writeFile(file.source, '').then(() => Promise.all([
fs.stat(file.source),
process(file)
])).spread(stats => {
const page = Page.findOne({source: file.path});

page.date.toDate().should.eql(stats.ctime);
page.updated.toDate().should.eql(page.date.toDate());

return Promise.all([
page.remove(),
fs.unlink(file.source)
]);
}).finally(() => {
hexo.config.use_date_for_updated = undefined;
});
});

it('page - permalink', () => {
const body = [
'title: "Hello world"',
Expand Down Expand Up @@ -449,7 +476,7 @@ describe('asset', () => {
const page = Page.findOne({source: file.path});

page.date.toDate().should.eql(stats.ctime);
page.updated.toDate().should.eql(stats.mtime);
page.updated.toDate().should.eql(page.date.toDate());

return Promise.all([
page.remove(),
Expand Down
31 changes: 31 additions & 0 deletions test/scripts/processors/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,37 @@ describe('post', () => {
}).finally(() => fs.unlink(file.source));
});

it('post - use date when no updated and use_date_for_updated', () => {
const body = [
'title: "Hello world"',
'---'
].join('\n');

const file = newFile({
path: 'foo.html',
published: true,
type: 'create',
renderable: true
});

hexo.config.use_date_for_updated = true;

return fs.writeFile(file.source, body).then(() => Promise.all([
file.stat(),
process(file)
])).spread(stats => {
const post = Post.findOne({source: file.path});

post.date.toDate().setMilliseconds(0).should.eql(stats.birthtime.setMilliseconds(0));
post.updated.toDate().setMilliseconds(0).should.eql(stats.birthtime.setMilliseconds(0));

return post.remove();
}).finally(() => {
hexo.config.use_date_for_updated = undefined;
fs.unlink(file.source);
});
});

it('post - photo is an alias for photos', () => {
const body = [
'title: "Hello world"',
Expand Down

0 comments on commit 6f6d04e

Please sign in to comment.