@@ -10,32 +10,35 @@ import {
1010import type { Change } from "../../deps/socket.ts" ;
1111import type { HeadData } from "./pull.ts" ;
1212import { toTitleLc } from "../../title.ts" ;
13+ import { parseYoutube } from "../../parseYoutube.ts" ;
1314
1415export interface Init {
1516 userId : string ;
1617 head : HeadData ;
1718}
18- export const makeChanges = (
19+ export function * makeChanges (
1920 left : Pick < Line , "text" | "id" > [ ] ,
2021 right : string [ ] ,
2122 { userId, head } : Init ,
22- ) : Change [ ] = > {
23+ ) : Generator < Change , void , unknown > {
2324 // 改行文字が入るのを防ぐ
2425 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+ }
2730
2831 // titleの差分を入れる
2932 // 空ページの場合もタイトル変更commitを入れる
3033 if ( left [ 0 ] . text !== right_ [ 0 ] || ! head . persistent ) {
31- changes . push ( { title : right_ [ 0 ] } ) ;
34+ yield { title : right_ [ 0 ] } ;
3235 }
3336
3437 // descriptionsの差分を入れる
3538 const leftDescriptions = left . slice ( 1 , 6 ) . map ( ( line ) => line . text ) ;
3639 const rightDescriptions = right_ . slice ( 1 , 6 ) ;
3740 if ( leftDescriptions . join ( "" ) !== rightDescriptions . join ( "" ) ) {
38- changes . push ( { descriptions : rightDescriptions } ) ;
41+ yield { descriptions : rightDescriptions } ;
3942 }
4043
4144 // リンクと画像の差分を入れる
@@ -44,14 +47,12 @@ export const makeChanges = (
4447 head . links . length !== links . length ||
4548 ! head . links . every ( ( link ) => links . includes ( link ) )
4649 ) {
47- changes . push ( { links } ) ;
50+ yield { links } ;
4851 }
4952 if ( head . image !== image ) {
50- changes . push ( { image } ) ;
53+ yield { image } ;
5154 }
52-
53- return changes ;
54- } ;
55+ }
5556
5657/** テキストに含まれる全てのリンクと最初の画像を探す */
5758const findLinksAndImage = ( text : string ) : [ string [ ] , string | null ] => {
@@ -86,11 +87,20 @@ const findLinksAndImage = (text: string): [string[], string | null] => {
8687 links . push ( node . href ) ;
8788 return ;
8889 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+ }
94104 case "image" :
95105 case "strongImage" : {
96106 image ??= node . src . endsWith ( "/thumb/1000" )
0 commit comments