fix: resolve static front page .md URL generation (#5)#8
Conversation
Fixes invalid markdown URL generation when a static page is configured as the front page (Settings → Reading). Previously, get_permalink() would return the site root (e.g., https://example.com/), and appending .md resulted in https://example.com.md, which appears to be an invalid TLD rather than a markdown file. Changes: - Added convert_to_markdown_url() method to both AlternateLinkHandler and RewriteHandler to detect front page permalinks and convert them to /index.md instead of .md - Updated parse_markdown_url() to handle /index.md requests and resolve them to the page_on_front option - Added dedicated rewrite rule for index.md to ensure proper routing Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where setting a static page as the WordPress front page resulted in an invalid markdown URL (e.g., https://example.com.md instead of https://example.com/index.md). The fix introduces special handling for the front page by detecting when a permalink matches the site root and converting it to /index.md.
Changes:
- Added
convert_to_markdown_url()methods in bothAlternateLinkHandlerandRewriteHandlerto handle front page URL generation - Added special routing logic in
parse_markdown_url()to resolve/index.mdrequests to the configured front page - Added a dedicated rewrite rule for
index.mdpattern
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/Router/RewriteHandler.php | Added rewrite rule for index.md, special handling in parse_markdown_url() for front page resolution, new convert_to_markdown_url() method, and updated Accept header negotiation to use the new method |
| src/Discovery/AlternateLinkHandler.php | Added convert_to_markdown_url() method for generating proper markdown URLs in alternate link tags |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/Router/RewriteHandler.php
Outdated
| private function convert_to_markdown_url(string $permalink): string { | ||
| // Normalize both URLs by removing trailing slashes for comparison | ||
| $normalized_permalink = rtrim($permalink, '/'); | ||
| $normalized_home = rtrim(home_url('/'), '/'); | ||
|
|
||
| // If this is the front page, use /index.md | ||
| if ($normalized_permalink === $normalized_home) { | ||
| return home_url('/index.md'); | ||
| } | ||
|
|
||
| // Otherwise, append .md to the permalink | ||
| return $normalized_permalink . '.md'; | ||
| } |
There was a problem hiding this comment.
The convert_to_markdown_url() method is duplicated between RewriteHandler and AlternateLinkHandler classes. This is a maintainability concern - any future changes to the URL conversion logic would need to be made in two places, increasing the risk of inconsistencies.
Consider extracting this method to a shared utility class or trait that both classes can use. For example, create a MarkdownUrlConverter utility class or a trait that both handlers can use.
src/Router/RewriteHandler.php
Outdated
| // Handle /index.md as the front page | ||
| if ($matches[1] === 'index') { | ||
| $page_on_front = get_option('page_on_front'); | ||
| if ($page_on_front) { | ||
| $post_id = (int) $page_on_front; | ||
| } else { | ||
| // No static front page set - let WordPress show its normal behavior | ||
| return; |
There was a problem hiding this comment.
There's a potential collision if a user creates an actual page with the slug "index". When visiting /index.md, the current logic will always treat it as the front page (lines 103-110) rather than checking if there's an actual page with slug "index".
The logic should first attempt to resolve "index" as a regular page slug, and only fall back to treating it as the front page if no such page exists. Suggested fix:
// Handle /index.md - could be front page or a page with slug "index"
if ($matches[1] === 'index') {
// First try to resolve as a regular page
$clean_url = home_url('/index');
$post_id = url_to_postid($clean_url);
// If no page with slug "index" exists, treat as front page
if (!$post_id) {
$page_on_front = get_option('page_on_front');
if ($page_on_front) {
$post_id = (int) $page_on_front;
} else {
return;
}
}
}| // Handle /index.md as the front page | |
| if ($matches[1] === 'index') { | |
| $page_on_front = get_option('page_on_front'); | |
| if ($page_on_front) { | |
| $post_id = (int) $page_on_front; | |
| } else { | |
| // No static front page set - let WordPress show its normal behavior | |
| return; | |
| // Handle /index.md - could be front page or a page with slug "index" | |
| if ($matches[1] === 'index') { | |
| // First try to resolve as a regular page with slug "index" | |
| $clean_url = home_url('/index'); | |
| $post_id = url_to_postid($clean_url); | |
| // If no page with slug "index" exists, treat as front page | |
| if (!$post_id) { | |
| $page_on_front = get_option('page_on_front'); | |
| if ($page_on_front) { | |
| $post_id = (int) $page_on_front; | |
| } else { | |
| // No static front page set - let WordPress show its normal behavior | |
| return; | |
| } |
- Extract convert_to_markdown_url() to shared UrlConverter utility class to eliminate code duplication between RewriteHandler and AlternateLinkHandler - Fix edge case where a page with slug "index" would be inaccessible at /index.md - now checks for actual "index" page before falling back to front page logic Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
Consolidation note: treating this as the primary PR for static front-page markdown URL behavior (supersedes #6). Recommended to merge early before rewrite refactors. |
Closes #5
Summary
Fixes invalid markdown URL generation when a static page is configured as the front page.
Problem
When a static page is set as the front page (Settings → Reading),
get_permalink()returns the site root (e.g.,https://example.com/). The plugin was appending.mddirectly, resulting inhttps://example.com.md, which looks like an invalid TLD.Solution
/index.mdinstead of.md/index.mdrequestsindex.mdChanges
convert_to_markdown_url()method to generate proper markdown URLsconvert_to_markdown_url()method for Accept header negotiationparse_markdown_url()for/index.md^index\.md$Test Plan
<link rel="alternate">in HTML showshttps://example.com/index.mdAccept: text/markdownon front page redirects to/index.md/index.mdserves front page content as markdown/.mdreturns 404 (does not resolve to front page).mdURLs🤖 Generated with Claude Code