Skip to content

Spread blog ad units across top, mid-article, and bottom - #113

Merged
ralyodio merged 1 commit into
masterfrom
fix/blog-ad-placement
Jul 24, 2026
Merged

ralyodio merged 1 commit into
masterfrom
fix/blog-ad-placement

Conversation

@ralyodio

Copy link
Copy Markdown
Contributor

Follow-up to #112, which stacked both ad units at the bottom of the page — the lowest-value placement.

Post page (/blog/[slug]): one unit above the body, one mid-article, one after the article.
Index (/blog): one above the list, one after the 3rd card, one below the list. The mid-list unit only appears when there are 6+ posts, otherwise it's just the bottom unit again.

Mid-article placement means cutting the rendered post HTML in half. A naive cut at the halfway character routinely lands inside a list, blockquote or code block and leaves unbalanced tags in both halves (the bodies are injected with dangerouslySetInnerHTML), so lib/blog/split-html.ts only cuts after a top-level block element closes, tracking container depth as it scans.

One case the tests turned up: a single big block (long list, 200-line code sample) can straddle the midpoint and leave no legal boundary inside the preferred 25–75% window. Rather than silently drop the mid unit on exactly those long posts, it retries in a wider window before giving up. Short posts and plain-text bodies get no mid unit at all.

8 unit tests in tests/split-html.test.ts cover the balanced-tag guarantees (list / blockquote / code block), the midpoint bounds, and the null cases. Full suite: 434 passing. /blog route caching is unchanged.

Both units were stacked at the bottom of the page, which is the
lowest-value placement and looks like a footer dump. Spread them out:

- Post page: one above the body, one mid-article, one after the article.
- Index: one above the list, one after the 3rd card (only when the list
  has 6+ posts, else it's just the bottom unit again), one below.

Mid-article means cutting rendered post HTML in half, and a naive cut at
the halfway character lands inside a list, blockquote or code block and
leaves unbalanced tags in both halves. splitHtmlForMidAd only cuts after
a top-level block closes, tracking container depth as it scans.

A single big block can straddle the midpoint and leave no boundary in
the preferred 25-75% window; rather than drop the unit on exactly those
long posts, it falls back to a wider window before giving up. Short
posts and plain-text bodies get no mid unit at all.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

vu1nz Security Review

0 finding(s) in PR #?

No security issues found.

@ralyodio
ralyodio merged commit cae1eb1 into master Jul 24, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant