Skip to content

Commit 3d34e1c

Browse files
committed
fix: change condition
1 parent b86e85c commit 3d34e1c

File tree

2 files changed

+64
-56
lines changed

2 files changed

+64
-56
lines changed

src/components/common/MarkdownRender.tsx

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -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('');

src/components/common/NarrowAd.tsx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { useEffect, useState } from 'react';
22
import styled from 'styled-components';
33
import media from '../../lib/styles/media';
4-
import { isTablet } from '../../lib/deviceDetection';
54

65
const Wrapper = styled.div`
76
display: flex;
@@ -46,28 +45,25 @@ function AdInsComponent({
4645
}
4746

4847
export default function NarrowAd() {
49-
const [isMobileOrTablet, setIsMobileOrTablet] = useState(false);
48+
const [isWindowNarrow, setIsWindowNarrow] = useState(false);
5049
const [mode, setMode] = useState<'wide' | 'narrow'>('wide');
5150

5251
useEffect(() => {
53-
const userAgent = navigator.userAgent.toLowerCase();
54-
const checkIsMobile =
55-
/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(
56-
userAgent,
57-
);
58-
const checkIsTablet = isTablet();
52+
const windowWidth = window.innerWidth;
5953

60-
setIsMobileOrTablet(checkIsMobile || checkIsTablet);
54+
setIsWindowNarrow(windowWidth <= 1200);
6155

6256
// Set mode based on window width
63-
const windowWidth = window.innerWidth;
6457
setMode(windowWidth < 768 ? 'narrow' : 'wide');
6558
}, []);
6659

6760
useEffect(() => {
6861
const handleResize = () => {
6962
const windowWidth = window.innerWidth;
7063

64+
// Update isWindowNarrow based on width
65+
setIsWindowNarrow(windowWidth <= 1200);
66+
7167
// Update mode based on width
7268
setMode(windowWidth < 768 ? 'narrow' : 'wide');
7369
};
@@ -84,7 +80,7 @@ export default function NarrowAd() {
8480

8581
return (
8682
<Wrapper>
87-
{isMobileOrTablet && mode === 'narrow' && (
83+
{isWindowNarrow && mode === 'narrow' && (
8884
<AdBlock width={size.width} height={size.height}>
8985
<AdInsComponent
9086
width={size.width}
@@ -93,7 +89,7 @@ export default function NarrowAd() {
9389
/>
9490
</AdBlock>
9591
)}
96-
{isMobileOrTablet && mode === 'wide' && (
92+
{isWindowNarrow && mode === 'wide' && (
9793
<AdBlock width={size.width} height={size.height}>
9894
<AdInsComponent
9995
width={size.width}

0 commit comments

Comments
 (0)