diff --git a/bots/converters/blog_converter.py b/bots/converters/blog_converter.py index 4c4cdd2..3e5efab 100644 --- a/bots/converters/blog_converter.py +++ b/bots/converters/blog_converter.py @@ -96,10 +96,16 @@ def build_full_html(article: dict, body_html: str, toc_html: str, json_ld = build_json_ld(article, post_url) disclaimer = article.get('disclaimer', '') parts = [json_ld] - # 목차: h2가 3개 이상인 긴 글에서만 표시 + # 목차: h2가 3개 이상이고 TOC에 실제 링크가 있을 때만 표시 h2_count = body_html.lower().count('= 3: - parts.append(f'
{toc_html}
') + toc_has_links = toc_html and '= 3 + if toc_has_links: + import re as _re + m = _re.match(r'(]*/>)\s*', body_html) + if m: + body_html = m.group(0) + f'
{toc_html}
\n' + body_html[m.end():] + else: + parts.append(f'
{toc_html}
') parts.append(body_html) if disclaimer: parts.append(f'

{disclaimer}

') diff --git a/bots/publisher_bot.py b/bots/publisher_bot.py index 6aefe3b..2a84f79 100644 --- a/bots/publisher_bot.py +++ b/bots/publisher_bot.py @@ -376,6 +376,16 @@ def fetch_featured_image(article: dict) -> str: return '' +def _insert_toc_after_image(body_html: str, toc_block: str) -> str: + """본문에 대표이미지가 있으면 이미지 뒤에, 없으면 맨 앞에 TOC 삽입""" + import re as _re + # 본문 시작이 ]*/>)\s*', body_html) + if m: + return m.group(0) + toc_block + body_html[m.end():] + return toc_block + body_html + + def build_full_html(article: dict, body_html: str, toc_html: str) -> str: """최종 HTML 조합: 대표이미지 + JSON-LD + 목차 + 본문 + 면책 문구""" json_ld = build_json_ld(article) @@ -399,10 +409,13 @@ def build_full_html(article: dict, body_html: str, toc_html: str) -> str: body_html = img_tag + '\n' + body_html html_parts.append(json_ld) - # 목차: h2가 3개 이상인 긴 글에서만 표시 + # 목차: h2가 3개 이상이고 TOC에 실제 링크가 있을 때만 표시 h2_count = body_html.lower().count('= 3: - html_parts.append(f'
{toc_html}
') + toc_has_links = toc_html and '
= 3 + if toc_has_links: + # 이미지 뒤, 본문 앞에 목차 삽입 + toc_block = f'
{toc_html}
\n' + body_html = _insert_toc_after_image(body_html, toc_block) html_parts.append(body_html) # 원문 출처 링크