@@ -10,6 +10,7 @@ import type Hexo from '../../hexo';
1010import type { Stats } from 'fs' ;
1111import { PostAssetSchema , PostSchema } from '../../types' ;
1212import type Document from 'warehouse/dist/document' ;
13+ import SourceIdIndex from './source_id_index' ;
1314
1415const postDir = '_posts/' ;
1516const draftDir = '_drafts/' ;
@@ -26,6 +27,8 @@ const preservedKeys = {
2627} ;
2728
2829export = ( ctx : Hexo ) => {
30+ const postSourceIndex = new SourceIdIndex ( ctx . model ( 'Post' ) ) ;
31+
2932 return {
3033 pattern : new Pattern ( path => {
3134 if ( isTmpFile ( path ) ) return ;
@@ -59,18 +62,18 @@ export = (ctx: Hexo) => {
5962
6063 process : function postProcessor ( file : _File ) {
6164 if ( file . params . renderable ) {
62- return processPost ( ctx , file ) ;
65+ return processPost ( ctx , file , postSourceIndex ) ;
6366 } else if ( ctx . config . post_asset_folder ) {
6467 return processAsset ( ctx , file ) ;
6568 }
6669 }
6770 } ;
6871} ;
6972
70- function processPost ( ctx : Hexo , file : _File ) {
73+ function processPost ( ctx : Hexo , file : _File , sourceIndex : SourceIdIndex < PostSchema > ) {
7174 const Post = ctx . model ( 'Post' ) ;
7275 const { path } = file . params ;
73- const doc = Post . findOne ( { source : file . path } ) ;
76+ const doc = sourceIndex . find ( file . path ) ;
7477 const { config } = ctx ;
7578 const { timezone, updated_option, use_slug_as_post_title } = config ;
7679
@@ -82,7 +85,10 @@ function processPost(ctx: Hexo, file: _File) {
8285
8386 if ( file . type === 'delete' ) {
8487 if ( doc ) {
85- return doc . remove ( ) ;
88+ return doc . remove ( ) . then ( result => {
89+ sourceIndex . delete ( file . path ) ;
90+ return result ;
91+ } ) ;
8692 }
8793
8894 return ;
@@ -184,11 +190,15 @@ function processPost(ctx: Hexo, file: _File) {
184190 }
185191
186192 return Post . insert ( data ) ;
187- } ) . then ( ( doc : PostSchema ) => Promise . all ( [
188- doc . setCategories ( categories ) ,
189- doc . setTags ( tags ) ,
190- scanAssetDir ( ctx , doc )
191- ] ) . then ( ( ) => markFuturePostDirty ( ctx , file , doc ) ) ) ;
193+ } ) . then ( ( doc : PostSchema ) => {
194+ sourceIndex . set ( doc ) ;
195+
196+ return Promise . all ( [
197+ doc . setCategories ( categories ) ,
198+ doc . setTags ( tags ) ,
199+ scanAssetDir ( ctx , doc )
200+ ] ) . then ( ( ) => markFuturePostDirty ( ctx , file , doc ) ) ;
201+ } ) ;
192202}
193203
194204function parseFilename ( config : string , path : string ) {
0 commit comments