Skip to content

Commit 93d11b1

Browse files
committed
fix: change position
1 parent eee9658 commit 93d11b1

File tree

1 file changed

+57
-43
lines changed

1 file changed

+57
-43
lines changed

src/components/common/MarkdownRender.tsx

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ const MarkdownRender: React.FC<MarkdownRenderProps> = ({
234234
const [element, setElement] = useState<RenderedElement>(null);
235235
const [hasTagError, setHasTagError] = useState(false);
236236
const [delay, setDelay] = useState(25);
237-
const [hasEnoughBlock, setHasEnoughBlock] = useState(false);
238237
const [htmlWithAds, setHtmlWithAds] = useState('');
239238

240239
const throttledUpdate = React.useMemo(() => {
@@ -300,63 +299,78 @@ const MarkdownRender: React.FC<MarkdownRenderProps> = ({
300299

301300
const parser = new DOMParser();
302301
const doc = parser.parseFromString(html, 'text/html');
303-
const blockElements = doc.querySelectorAll('p, h1, h2, h3, h4, h5, h6, blockquote, pre, ul, ol, hr, table');
302+
const blockElements = doc.querySelectorAll(
303+
'p, h1, h2, h3, h4, h5, h6, blockquote, pre, ul, ol, hr, table',
304+
);
304305

305306
console.log('Total blocks:', blockElements.length);
306307

307-
const hasEnough = blockElements.length >= 20;
308-
setHasEnoughBlock(hasEnough);
309-
console.log('hasEnoughBlock:', hasEnough);
308+
const blockCount = blockElements.length;
310309

311-
if (hasEnough) {
312-
// Find the position to insert ad
313-
let targetBlock = null;
314-
let insertPosition = 19; // 20th block (0-based index)
310+
// Find the position to insert ad
311+
let insertPosition = -1;
312+
313+
if (blockCount >= 20) {
314+
// 블록이 20개 이상이면 20번째 블록부터 헤딩 찾기
315+
insertPosition = 19; // 20th block (0-based index)
315316

316317
// Look for first h1, h2, h3 after 20th block
317318
for (let i = 20; i < blockElements.length && i < 50; i++) {
318319
const block = blockElements[i];
319-
if (block.tagName === 'H1' || block.tagName === 'H2' || block.tagName === 'H3') {
320-
targetBlock = block;
320+
if (
321+
block.tagName === 'H1' ||
322+
block.tagName === 'H2' ||
323+
block.tagName === 'H3'
324+
) {
321325
insertPosition = i;
322326
break;
323327
}
324328
}
329+
} else {
330+
// 블록이 20개 미만이면 블록수 / 2부터 헤딩 찾기
331+
const startPosition = Math.floor(blockCount / 2);
332+
insertPosition = startPosition;
325333

326-
// If heading found and before index 50, insert before it
327-
// Otherwise insert after 20th block
328-
const blockToInsertAfter = targetBlock
329-
? blockElements[insertPosition - 1]
330-
: blockElements[19];
331-
332-
if (blockToInsertAfter) {
333-
const adDiv = doc.createElement('ins');
334-
adDiv.className = 'adsbygoogle';
335-
adDiv.style.display = 'block';
336-
adDiv.style.textAlign = 'center';
337-
adDiv.setAttribute('data-ad-layout', 'in-article');
338-
adDiv.setAttribute('data-ad-format', 'fluid');
339-
adDiv.setAttribute('data-ad-client', 'ca-pub-5574866530496701');
340-
adDiv.setAttribute('data-ad-slot', '9632367492');
341-
342-
// Insert after the target block (or before heading if found)
343-
if (targetBlock) {
344-
targetBlock.parentNode?.insertBefore(adDiv, targetBlock);
345-
} else {
346-
blockToInsertAfter.parentNode?.insertBefore(adDiv, blockToInsertAfter.nextSibling);
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;
347344
}
348-
349-
// Set the modified HTML
350-
const updatedHtml = doc.body.innerHTML;
351-
setHtmlWithAds(updatedHtml);
352-
353-
// Push ad after 1 second
354-
setTimeout(() => {
355-
(window.adsbygoogle = window.adsbygoogle || []).push({});
356-
}, 1000);
357-
} else {
358-
setHtmlWithAds('');
359345
}
346+
}
347+
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);
365+
366+
// Set the modified HTML
367+
const updatedHtml = doc.body.innerHTML;
368+
setHtmlWithAds(updatedHtml);
369+
370+
// Push ad after 1 second
371+
setTimeout(() => {
372+
(window.adsbygoogle = window.adsbygoogle || []).push({});
373+
}, 1000);
360374
} else {
361375
setHtmlWithAds('');
362376
}

0 commit comments

Comments
 (0)