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

feat: bring up config.updated_option #4278

Merged
merged 3 commits into from
Jun 22, 2020
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
6 changes: 4 additions & 2 deletions lib/hexo/default_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ 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,
updated_option: 'mtime',
// * mtime: file modification date (default)
// * date: use_date_for_updated
// * empty: no more update
// Pagination
per_page: 10,
pagination_dir: 'page',
Expand Down
1 change: 0 additions & 1 deletion lib/models/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ module.exports = ctx => {
},
updated: {
type: Moment,
default: moment,
language: ctx.config.languages,
timezone: ctx.config.timezone
},
Expand Down
1 change: 0 additions & 1 deletion lib/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ module.exports = ctx => {
},
updated: {
type: Moment,
default: moment,
language: ctx.config.languages,
timezone: ctx.config.timezone
},
Expand Down
6 changes: 4 additions & 2 deletions 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: timezoneCfg, use_date_for_updated } = config;
const { timezone: timezoneCfg, use_date_for_updated, updated_option } = config;

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

if (data.updated) {
if (timezoneCfg) data.updated = timezone(data.updated, timezoneCfg);
} else if (use_date_for_updated) {
} else if (use_date_for_updated || updated_option === 'date') {
data.updated = data.date;
} else if (updated_option === 'empty') {
delete data.updated;
} else {
data.updated = stats.mtime;
}
Expand Down
6 changes: 4 additions & 2 deletions lib/plugins/processor/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = ctx => {
const { path } = file.params;
const doc = Post.findOne({source: file.path});
const { config } = ctx;
const { timezone: timezoneCfg, use_date_for_updated } = config;
const { timezone: timezoneCfg, use_date_for_updated, updated_option } = config;
let categories, tags;

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

if (data.updated) {
if (timezoneCfg) data.updated = timezone(data.updated, timezoneCfg);
} else if (use_date_for_updated) {
} else if (use_date_for_updated || updated_option === 'date') {
data.updated = data.date;
} else if (updated_option === 'empty') {
delete data.updated;
} else {
data.updated = stats.mtime;
}
Expand Down
4 changes: 3 additions & 1 deletion test/scripts/helpers/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ describe('open_graph', () => {
meta({property: 'og:site_name', content: hexo.config.title}),
meta({property: 'og:locale', content: 'en_US'}),
meta({property: 'article:published_time', content: post.date.toISOString()}),
meta({property: 'article:modified_time', content: post.updated.toISOString()}),
// page.updated will no longer exist by default
// See https://github.com/hexojs/hexo/pull/4278
// meta({property: 'article:modified_time', content: post.updated.toISOString()}),
meta({property: 'article:author', content: hexo.config.author}),
meta({property: 'article:tag', content: 'optimize'}),
meta({property: 'article:tag', content: 'web'}),
Expand Down
2 changes: 1 addition & 1 deletion test/scripts/models/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ describe('Page', () => {

data.title.should.eql('');
data.date.valueOf().should.gte(now);
data.updated.valueOf().should.gte(now);
data.comments.should.be.true;
data.layout.should.eql('page');
data._content.should.eql('');
data.raw.should.eql('');
should.not.exist(data.updated);
should.not.exist(data.content);
should.not.exist(data.excerpt);
should.not.exist(data.more);
Expand Down
2 changes: 1 addition & 1 deletion test/scripts/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ describe('Post', () => {
}).then(data => {
data.title.should.eql('');
data.date.valueOf().should.gte(now);
data.updated.valueOf().should.gte(now);
data.comments.should.be.true;
data.layout.should.eql('post');
data._content.should.eql('');
data.link.should.eql('');
data.raw.should.eql('');
data.published.should.be.true;
should.not.exist(data.updated);
should.not.exist(data.content);
should.not.exist(data.excerpt);
should.not.exist(data.more);
Expand Down
112 changes: 110 additions & 2 deletions test/scripts/processors/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,22 +281,130 @@ describe('asset', () => {
]);
});

it('page - use the date for updated if use_date_for_updated is set', async () => {
it('page - use the date for updated if updated_option = date', async () => {
const body = [
'date: 2011-4-5 14:19:19',
'---'
].join('\n');

const file = newFile({
path: 'hello.njk',
type: 'create',
renderable: true
});

hexo.config.use_date_for_updated = true;
hexo.config.updated_option = 'date';

await writeFile(file.source, body);
await process(file);
const stats = await stat(file.source);
const page = Page.findOne({source: file.path});

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

await Promise.all([
page.remove(),
unlink(file.source)
]);
});

it('page - use the status if updated_option = mtime', async () => {
const file = newFile({
path: 'hello.njk',
type: 'create',
renderable: true
});

hexo.config.updated_option = 'mtime';

await writeFile(file.source, '');
await process(file);
const stats = await stat(file.source);
const page = Page.findOne({source: file.path});

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

await Promise.all([
page.remove(),
unlink(file.source)
]);
});

it('page - updated shouldn\'t exists if updated_option = empty', async () => {
const file = newFile({
path: 'hello.njk',
type: 'create',
renderable: true
});

hexo.config.updated_option = 'empty';

await writeFile(file.source, '');
await process(file);
const stats = await stat(file.source);
const page = Page.findOne({source: file.path});

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

await Promise.all([
page.remove(),
unlink(file.source)
]);
});

it('page - use_date_for_updated as fallback', async () => {
const body = [
'date: 2011-4-5 14:19:19',
'---'
].join('\n');

const file = newFile({
path: 'hello.njk',
type: 'create',
renderable: true
});

hexo.config.use_date_for_updated = true;

await writeFile(file.source, body);
await process(file);
const stats = await stat(file.source);
const page = Page.findOne({source: file.path});

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

await Promise.all([
page.remove(),
unlink(file.source)
]);
});

it('page - ignore updated_option when use_date_for_updated is set', async () => {
const body = [
'date: 2011-4-5 14:19:19',
'---'
].join('\n');

const file = newFile({
path: 'hello.njk',
type: 'create',
renderable: true
});

hexo.config.use_date_for_updated = true;
hexo.config.updated_option = 'mtime';

await writeFile(file.source, body);
await process(file);
const stats = await stat(file.source);
const page = Page.findOne({source: file.path});

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

await Promise.all([
page.remove(),
Expand Down
112 changes: 111 additions & 1 deletion test/scripts/processors/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,88 @@ describe('post', () => {
unlink(file.source);
});

it('post - use date when no updated and use_date_for_updated', async () => {
it('post - use the date for updated if updated_option = date', async () => {
const body = [
'date: 2011-4-5 14:19:19',
'title: "Hello world"',
'---'
].join('\n');

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

hexo.config.updated_option = 'date';

await writeFile(file.source, body);
const stats = await file.stat();
await process(file);
const post = Post.findOne({source: file.path});

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

post.remove();
unlink(file.source);
});

it('post - use the status of the source file if updated_option = mtime', async () => {
const body = [
'date: 2011-4-5 14:19:19',
'title: "Hello world"',
'---'
].join('\n');

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

hexo.config.updated_option = 'mtime';

await writeFile(file.source, body);
const stats = await file.stat();
await process(file);
const post = Post.findOne({source: file.path});

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

post.remove();
unlink(file.source);
});

it('post - updated shouldn\'t exists if updated_option = empty', async () => {
const body = [
'title: "Hello world"',
'---'
].join('\n');

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

hexo.config.updated_option = 'empty';

await writeFile(file.source, body);
await process(file);
const post = Post.findOne({source: file.path});

should.not.exist(post.updated);

post.remove();
unlink(file.source);
});

it('post - use use_date_for_updated as a fallback', async () => {
const body = [
'title: "Hello world"',
'---'
Expand All @@ -534,6 +615,35 @@ describe('post', () => {
unlink(file.source);
});

it('post - ignore updated_option when use_date_for_updated is set', async () => {
const body = [
'date: 2011-4-5 14:19:19',
'title: "Hello world"',
'---'
].join('\n');

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

hexo.config.use_date_for_updated = true;
hexo.config.updated_option = 'mtime';

await writeFile(file.source, body);
const stats = await file.stat();
await process(file);
const post = Post.findOne({source: file.path});

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

post.remove();
unlink(file.source);
});

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