Skip to content

Commit 069ac38

Browse files
uioleeSukkaW
andauthored
feat: add option to use slug as title of post (#5470)
Signed-off-by: Uiolee <22849383+uiolee@users.noreply.github.com> Co-authored-by: Sukka <isukkaw@gmail.com>
1 parent 6d880a2 commit 069ac38

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

lib/hexo/default_config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ export = {
5858
exclude_languages: [],
5959
strip_indent: true
6060
},
61+
use_filename_as_post_title: false,
62+
6163
// Category & Tag
6264
default_category: 'uncategorized',
6365
category_map: {},

lib/plugins/processor/post.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ function processPost(ctx: Hexo, file: _File) {
7171
const { path } = file.params;
7272
const doc = Post.findOne({source: file.path});
7373
const { config } = ctx;
74-
const { timezone: timezoneCfg } = config;
75-
const updated_option = config.updated_option;
74+
const { timezone: timezoneCfg, updated_option, use_slug_as_post_title } = config;
75+
7676
let categories, tags;
7777

7878
if (file.type === 'skip' && doc) {
@@ -110,6 +110,12 @@ function processPost(ctx: Hexo, file: _File) {
110110
if (!preservedKeys[key]) data[key] = info[key];
111111
}
112112

113+
// use `slug` as `title` of post when `title` is not specified.
114+
// https://github.com/hexojs/hexo/issues/5372
115+
if (use_slug_as_post_title && !('title' in data)) {
116+
data.title = info.title;
117+
}
118+
113119
if (data.date) {
114120
data.date = toDate(data.date);
115121
} else if (info && info.year && (info.month || info.i_month) && (info.day || info.i_day)) {

test/scripts/processors/post.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,32 @@ describe('post', () => {
805805
]);
806806
});
807807

808+
// use `slug` as `title` of post when `title` is not specified.
809+
// https://github.com/hexojs/hexo/issues/5372
810+
it('post - without title - use filename', async () => {
811+
hexo.config.use_slug_as_post_title = true;
812+
813+
const body = '';
814+
815+
const file = newFile({
816+
path: 'bar.md',
817+
published: true,
818+
type: 'create',
819+
renderable: true
820+
});
821+
822+
await writeFile(file.source, body);
823+
await process(file);
824+
const post = Post.findOne({ source: file.path });
825+
826+
post.title.should.eql('bar');
827+
828+
return Promise.all([
829+
post.remove(),
830+
unlink(file.source)
831+
]);
832+
});
833+
808834
it('post - category is an alias for categories', async () => {
809835
const body = [
810836
'title: "Hello world"',

0 commit comments

Comments
 (0)