File tree Expand file tree Collapse file tree 3 files changed +36
-2
lines changed Expand file tree Collapse file tree 3 files changed +36
-2
lines changed Original file line number Diff line number Diff 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 : { } ,
Original file line number Diff line number Diff 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 ) ) {
Original file line number Diff line number Diff 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"' ,
You can’t perform that action at this time.
0 commit comments