Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/Router/RewriteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ public function parse_markdown_url(\WP $wp): void {
return;
}

$path_without_md = $matches[1];

// Handle /index.md - could be front page or a page with slug "index"
if ($matches[1] === 'index') {
if ($path_without_md === 'index') {
// First try to resolve as a regular page with slug "index"
$clean_url = home_url('/index');
$post_id = url_to_postid($clean_url);
Expand All @@ -116,9 +118,19 @@ public function parse_markdown_url(\WP $wp): void {
}
}
} else {
// Strip .md to get the original URL path, let WordPress resolve it
$clean_url = home_url('/' . $matches[1]);
$post_id = url_to_postid($clean_url);
// Try to resolve the post by replacing .md with known permalink extensions,
// then fall back to no extension (original behavior).
$extensions_to_try = array_merge( UrlConverter::PERMALINK_EXTENSIONS, [ '' ] );
$post_id = 0;

foreach ($extensions_to_try as $ext) {
$clean_url = home_url('/' . $path_without_md . $ext);
$post_id = url_to_postid($clean_url);

if ($post_id) {
break;
}
}

if (!$post_id) {
return; // Let WordPress show its normal 404
Expand Down
20 changes: 19 additions & 1 deletion src/Router/UrlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@
*/
class UrlConverter {

/**
* File extensions used in permalink structures that should be replaced with .md.
*
* @var string[]
*/
public const PERMALINK_EXTENSIONS = [ '.html', '.htm', '.php', '.aspx', '.asp' ];

/**
* Regex pattern matching any of the supported permalink extensions.
*
* @var string
*/
private const EXTENSION_PATTERN = '/\.(html?|php|aspx?)$/i';

/**
* Convert a permalink to a markdown URL.
*
Expand All @@ -30,7 +44,11 @@ public static function convert_to_markdown_url( string $permalink ): string {
return home_url( '/index.md' );
}

// Otherwise, append .md to the permalink
// Replace file extension with .md, or append .md if no extension
if ( preg_match( self::EXTENSION_PATTERN, $normalized_permalink ) ) {
return preg_replace( self::EXTENSION_PATTERN, '.md', $normalized_permalink );
}

return $normalized_permalink . '.md';
}
}