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

Use date instead of file mtime for updated date #3235

Merged
merged 8 commits into from Sep 21, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/hexo/default_config.js
Expand Up @@ -45,6 +45,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
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
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
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
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