@@ -303,73 +303,85 @@ const MarkdownRender: React.FC<MarkdownRenderProps> = ({
303303 'p, h1, h2, h3, h4, h5, h6, blockquote, pre, ul, ol, hr, table' ,
304304 ) ;
305305
306- console . log ( 'Total blocks:' , blockElements . length ) ;
307-
308306 const blockCount = blockElements . length ;
309307
310- // Find the position to insert ad
311- let insertPosition = - 1 ;
308+ // Find the position to insert first ad (10~25 범위)
309+ let firstAdPosition = 9 ; // 10th block (0-based index)
310+
311+ // Look for first h1, h2, h3 from 10th to 25th block
312+ for ( let i = 10 ; i <= 25 && i < blockElements . length ; i ++ ) {
313+ const block = blockElements [ i ] ;
314+ if (
315+ block . tagName === 'H1' ||
316+ block . tagName === 'H2' ||
317+ block . tagName === 'H3'
318+ ) {
319+ firstAdPosition = i ;
320+ break ;
321+ }
322+ }
312323
313- if ( blockCount >= 20 ) {
314- // 블록이 20개 이상이면 20번째 블록부터 헤딩 찾기
315- insertPosition = 19 ; // 20th block (0-based index)
324+ // Find the position to insert second ad (only if blockCount >= 35)
325+ let secondAdPosition = - 1 ;
326+ if ( blockCount >= 35 ) {
327+ // 블록이 35 이상인 경우: 25번째부터 40번째까지 h1, h2, h3 찾기
328+ secondAdPosition = 24 ; // 25th block (0-based index)
316329
317- // Look for first h1, h2, h3 after 20th block
318- for ( let i = 20 ; i < blockElements . length && i < 50 ; i ++ ) {
330+ // Look for first h1, h2, h3 from 25th to 40th block
331+ for ( let i = 25 ; i <= 40 && i < blockElements . length ; i ++ ) {
319332 const block = blockElements [ i ] ;
320333 if (
321334 block . tagName === 'H1' ||
322335 block . tagName === 'H2' ||
323336 block . tagName === 'H3'
324337 ) {
325- insertPosition = i ;
338+ secondAdPosition = i ;
326339 break ;
327340 }
328341 }
329342 } else {
330- // 블록이 20개 미만이면 블록수 / 2부터 헤딩 찾기
331- const startPosition = Math . floor ( blockCount / 2 ) ;
332- insertPosition = startPosition ;
333-
334- // Look for first h1, h2, h3 from the middle
335- for ( let i = startPosition ; i < blockElements . length ; i ++ ) {
336- const block = blockElements [ i ] ;
337- if (
338- block . tagName === 'H1' ||
339- block . tagName === 'H2' ||
340- block . tagName === 'H3'
341- ) {
342- insertPosition = i ;
343- break ;
344- }
345- }
346343 }
347344
348- // Determine where to insert the ad
349- const insertBlock = blockElements [ insertPosition ] ;
350-
351- if ( insertBlock ) {
352- const adDiv = doc . createElement ( 'ins' ) ;
353- adDiv . className = 'adsbygoogle' ;
354- adDiv . style . display = 'block' ;
355- adDiv . style . textAlign = 'center' ;
356- adDiv . setAttribute ( 'data-ad-layout' , 'in-article' ) ;
357- adDiv . setAttribute ( 'data-ad-format' , 'fluid' ) ;
358- adDiv . setAttribute ( 'data-ad-client' , 'ca-pub-5574866530496701' ) ;
359- adDiv . setAttribute ( 'data-ad-slot' , '9632367492' ) ;
360-
361- // Insert before the target block
362- // If heading found, insert before heading
363- // Otherwise, insert at the calculated position
364- insertBlock . parentNode ?. insertBefore ( adDiv , insertBlock ) ;
345+ // Insert ads
346+ const firstAdBlock = blockElements [ firstAdPosition ] ;
347+ const secondAdBlock =
348+ secondAdPosition >= 0 ? blockElements [ secondAdPosition ] : null ;
349+
350+ if ( firstAdBlock ) {
351+ // Insert first ad
352+ const adDiv1 = doc . createElement ( 'ins' ) ;
353+ adDiv1 . className = 'adsbygoogle' ;
354+ adDiv1 . style . display = 'block' ;
355+ adDiv1 . style . textAlign = 'center' ;
356+ adDiv1 . setAttribute ( 'data-ad-layout' , 'in-article' ) ;
357+ adDiv1 . setAttribute ( 'data-ad-format' , 'fluid' ) ;
358+ adDiv1 . setAttribute ( 'data-ad-client' , 'ca-pub-5574866530496701' ) ;
359+ adDiv1 . setAttribute ( 'data-ad-slot' , '9632367492' ) ;
360+ firstAdBlock . parentNode ?. insertBefore ( adDiv1 , firstAdBlock ) ;
361+
362+ // Insert second ad if applicable
363+ if ( secondAdBlock ) {
364+ const adDiv2 = doc . createElement ( 'ins' ) ;
365+ adDiv2 . className = 'adsbygoogle' ;
366+ adDiv2 . style . display = 'block' ;
367+ adDiv2 . style . textAlign = 'center' ;
368+ adDiv2 . setAttribute ( 'data-ad-layout' , 'in-article' ) ;
369+ adDiv2 . setAttribute ( 'data-ad-format' , 'fluid' ) ;
370+ adDiv2 . setAttribute ( 'data-ad-client' , 'ca-pub-5574866530496701' ) ;
371+ adDiv2 . setAttribute ( 'data-ad-slot' , '9632367492' ) ;
372+ secondAdBlock . parentNode ?. insertBefore ( adDiv2 , secondAdBlock ) ;
373+ }
365374
366375 // Set the modified HTML
367376 const updatedHtml = doc . body . innerHTML ;
368377 setHtmlWithAds ( updatedHtml ) ;
369378
370- // Push ad after 1 second
379+ // Push ads after 1 second
371380 setTimeout ( ( ) => {
372381 ( window . adsbygoogle = window . adsbygoogle || [ ] ) . push ( { } ) ;
382+ if ( secondAdBlock ) {
383+ ( window . adsbygoogle = window . adsbygoogle || [ ] ) . push ( { } ) ;
384+ }
373385 } , 1000 ) ;
374386 } else {
375387 setHtmlWithAds ( '' ) ;
0 commit comments