@@ -10,32 +10,35 @@ import {
10
10
import type { Change } from "../../deps/socket.ts" ;
11
11
import type { HeadData } from "./pull.ts" ;
12
12
import { toTitleLc } from "../../title.ts" ;
13
+ import { parseYoutube } from "../../parseYoutube.ts" ;
13
14
14
15
export interface Init {
15
16
userId : string ;
16
17
head : HeadData ;
17
18
}
18
- export const makeChanges = (
19
+ export function * makeChanges (
19
20
left : Pick < Line , "text" | "id" > [ ] ,
20
21
right : string [ ] ,
21
22
{ userId, head } : Init ,
22
- ) : Change [ ] = > {
23
+ ) : Generator < Change , void , unknown > {
23
24
// 改行文字が入るのを防ぐ
24
25
const right_ = right . flatMap ( ( text ) => text . split ( "\n" ) ) ;
25
- // 本文の差分
26
- const changes : Change [ ] = [ ...diffToChanges ( left , right_ , { userId } ) ] ;
26
+ // 本文の差分を先に返す
27
+ for ( const change of diffToChanges ( left , right_ , { userId } ) ) {
28
+ yield change ;
29
+ }
27
30
28
31
// titleの差分を入れる
29
32
// 空ページの場合もタイトル変更commitを入れる
30
33
if ( left [ 0 ] . text !== right_ [ 0 ] || ! head . persistent ) {
31
- changes . push ( { title : right_ [ 0 ] } ) ;
34
+ yield { title : right_ [ 0 ] } ;
32
35
}
33
36
34
37
// descriptionsの差分を入れる
35
38
const leftDescriptions = left . slice ( 1 , 6 ) . map ( ( line ) => line . text ) ;
36
39
const rightDescriptions = right_ . slice ( 1 , 6 ) ;
37
40
if ( leftDescriptions . join ( "" ) !== rightDescriptions . join ( "" ) ) {
38
- changes . push ( { descriptions : rightDescriptions } ) ;
41
+ yield { descriptions : rightDescriptions } ;
39
42
}
40
43
41
44
// リンクと画像の差分を入れる
@@ -44,14 +47,12 @@ export const makeChanges = (
44
47
head . links . length !== links . length ||
45
48
! head . links . every ( ( link ) => links . includes ( link ) )
46
49
) {
47
- changes . push ( { links } ) ;
50
+ yield { links } ;
48
51
}
49
52
if ( head . image !== image ) {
50
- changes . push ( { image } ) ;
53
+ yield { image } ;
51
54
}
52
-
53
- return changes ;
54
- } ;
55
+ }
55
56
56
57
/** テキストに含まれる全てのリンクと最初の画像を探す */
57
58
const findLinksAndImage = ( text : string ) : [ string [ ] , string | null ] => {
@@ -86,11 +87,20 @@ const findLinksAndImage = (text: string): [string[], string | null] => {
86
87
links . push ( node . href ) ;
87
88
return ;
88
89
case "link" :
89
- if ( node . pathType !== "relative" ) return ;
90
- if ( linksLc . get ( toTitleLc ( node . href ) ) ) return ;
91
- linksLc . set ( toTitleLc ( node . href ) , true ) ;
92
- links . push ( node . href ) ;
93
- return ;
90
+ switch ( node . pathType ) {
91
+ case "relative" :
92
+ if ( linksLc . get ( toTitleLc ( node . href ) ) ) return ;
93
+ linksLc . set ( toTitleLc ( node . href ) , true ) ;
94
+ links . push ( node . href ) ;
95
+ return ;
96
+ case "absolute" : {
97
+ const props = parseYoutube ( node . href ) ;
98
+ if ( ! props ) return ;
99
+ return `https://i.ytimg.com/vi/${ props . videoId } /mqdefault.jpg` ;
100
+ }
101
+ default :
102
+ return ;
103
+ }
94
104
case "image" :
95
105
case "strongImage" : {
96
106
image ??= node . src . endsWith ( "/thumb/1000" )
0 commit comments