@@ -11,7 +11,7 @@ export async function processBacklinks(
1111 backlinkRepo : BacklinkRepo ,
1212 data : IPageBacklinkJob ,
1313) : Promise < void > {
14- const { pageId, mentions, workspaceId } = data ;
14+ const { pageId, mentions, workspaceId, internalLinkSlugIds = [ ] } = data ;
1515
1616 await executeTx ( db , async ( trx ) => {
1717 const existingBacklinks = await trx
@@ -20,24 +20,41 @@ export async function processBacklinks(
2020 . where ( 'sourcePageId' , '=' , pageId )
2121 . execute ( ) ;
2222
23- if ( existingBacklinks . length === 0 && mentions . length === 0 ) {
23+ const mentionTargetPageIds = mentions
24+ . filter ( ( mention ) => mention . entityId !== pageId )
25+ . map ( ( mention ) => mention . entityId ) ;
26+
27+ let resolvedLinkPageIds : string [ ] = [ ] ;
28+ if ( internalLinkSlugIds . length > 0 ) {
29+ const resolvedPages = await trx
30+ . selectFrom ( 'pages' )
31+ . select ( 'id' )
32+ . where ( 'slugId' , 'in' , internalLinkSlugIds )
33+ . where ( 'workspaceId' , '=' , workspaceId )
34+ . execute ( ) ;
35+ resolvedLinkPageIds = resolvedPages
36+ . map ( ( p ) => p . id )
37+ . filter ( ( id ) => id !== pageId ) ;
38+ }
39+
40+ const allTargetPageIds = [
41+ ...new Set ( [ ...mentionTargetPageIds , ...resolvedLinkPageIds ] ) ,
42+ ] ;
43+
44+ if ( existingBacklinks . length === 0 && allTargetPageIds . length === 0 ) {
2445 return ;
2546 }
2647
2748 const existingTargetPageIds = existingBacklinks . map (
2849 ( backlink ) => backlink . targetPageId ,
2950 ) ;
3051
31- const targetPageIds = mentions
32- . filter ( ( mention ) => mention . entityId !== pageId )
33- . map ( ( mention ) => mention . entityId ) ;
34-
3552 let validTargetPages = [ ] ;
36- if ( targetPageIds . length > 0 ) {
53+ if ( allTargetPageIds . length > 0 ) {
3754 validTargetPages = await trx
3855 . selectFrom ( 'pages' )
3956 . select ( 'id' )
40- . where ( 'id' , 'in' , targetPageIds )
57+ . where ( 'id' , 'in' , allTargetPageIds )
4158 . where ( 'workspaceId' , '=' , workspaceId )
4259 . execute ( ) ;
4360 }
0 commit comments