Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions lib/plugins/filter/post_permalink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function postPermalinkFilter(this: Hexo, data: PostSchema): string {
second: date.format('ss'),
i_month: date.format('M'),
i_day: date.format('D'),
timestamp: date.format('X'),
hash,
category: config.default_category
Comment on lines 35 to 40
Copy link
Member

@SukkaW SukkaW Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought, we could use an approach like the following to only calculate the permalink value when needed, and cache it afterward:

function defineLazyProperty(object, propertyName, valueGetter) {
  const define = value => Object.defineProperty(object, propertyName, {value, enumerable: true, writable: true});
  Object.defineProperty(object, propertyName, {
    configurable: true, enumerable: true,
    get() {
      const result = valueGetter();
      define(result);
      return result;
    },
    set: define
  });
  return object;
}

const permalink = {};

defineLazyProperty(permalink, 'second', () => date.format('ss'));
// ...
defineLazyProperty(permalink, 'timestamp', () => date.format('X'));

Or we can use Map instead of object here.

We can do this in another PR.

};
Expand Down
23 changes: 23 additions & 0 deletions test/scripts/filters/post_permalink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,29 @@ describe('post_permalink', () => {
Post.removeById(post._id);
});

it('timestamp', async () => {
hexo.config.permalink = ':timestamp/:slug';
const timestamp = '1736401514';
const dates = [
moment('2025-01-09 05:45:14Z'),
moment('2025-01-08 22:45:14-07')
];
const posts = await Post.insert(
dates.map((date, idx) => {
return { source: `test${idx}.md`, slug: `test${idx}`, date: date };
})
);

postPermalink(posts[0]).should.eql(`${timestamp}/test0`);
postPermalink(posts[1]).should.eql(`${timestamp}/test1`);

await Promise.all(
posts.map(post => {
return Post.removeById(post._id);
})
);
});

it('time is omitted in front-matter', async () => {
hexo.config.permalink = ':year/:month/:day/:hour/:minute/:second/:post_title/';

Expand Down
Loading