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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ the first 100 posts are listed, followed by a link to the HTML archive for the r
The `?format=markdown` fallback, the Accept redirect, and the alternate link work
on these archives as they do on posts.

Author and date archives remain HTML, including when a client sends
`Accept: text/markdown`; they have no Markdown renderer.

Frontmatter links to a term point to its `.md` URL only when its taxonomy is
served; other terms link to their HTML archive. Serve another taxonomy with
`markdown_alternate_supported_taxonomies`:
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Yes, as an opt-in. With `add_filter( 'markdown_alternate_skip_noindexed', '__ret
== Changelog ==

= Unreleased =
* Kept author and date archives in HTML instead of redirecting Markdown requests to missing pages
* Fixed cover block images running into the first inner heading
* Omitted aria-hidden and data-URI images from Markdown
* Preserved iframe embeds as links instead of dropping their content
Expand Down
33 changes: 7 additions & 26 deletions src/Router/RewriteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,14 +612,13 @@ public function handle_accept_negotiation(): void {
}


/**
* Get canonical URL for current content.
*
* Supports singular posts/pages, category/tag archives, author archives,
* and date archives.
*
* @return string|null The canonical URL or null if not determinable.
*/
/**
* Get the canonical URL for content with a supported Markdown route.
*
* Author and date archives have no Markdown renderer, so they retain HTML.
*
* @return string|null The canonical URL, or null for unsupported content.
*/
private function get_current_canonical_url(): ?string {
// Singular posts and pages
if (is_singular()) {
Expand All @@ -637,24 +636,6 @@ private function get_current_canonical_url(): ?string {
return is_wp_error($link) ? null : $link;
}

// Author archives
if (is_author()) {
return get_author_posts_url(get_queried_object_id());
}

// Date archives
if (is_date()) {
if (is_year()) {
return get_year_link(get_query_var('year'));
}
if (is_month()) {
return get_month_link(get_query_var('year'), get_query_var('monthnum'));
}
if (is_day()) {
return get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day'));
}
}

return null;
}

Expand Down
47 changes: 42 additions & 5 deletions tests/router-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,37 @@ function get_queried_object() {
return $GLOBALS['md_alt_router_post'];
}
function is_singular() {
return true;
return ! in_array( $GLOBALS['md_alt_mode'], array( 'author', 'year', 'month', 'day' ), true );
}
function is_author() {
return 'author' === $GLOBALS['md_alt_mode'];
}
function is_date() {
return in_array( $GLOBALS['md_alt_mode'], array( 'year', 'month', 'day' ), true );
}
function is_year() {
return 'year' === $GLOBALS['md_alt_mode'];
}
function is_month() {
return 'month' === $GLOBALS['md_alt_mode'];
}
function is_day() {
return 'day' === $GLOBALS['md_alt_mode'];
}
function get_queried_object_id() {
return 5;
}
function get_author_posts_url( $author_id ) {
return 'https://example.com/author/test/';
}
function get_year_link( $year ) {
return 'https://example.com/' . $year . '/';
}
function get_month_link( $year, $month ) {
return sprintf( 'https://example.com/%04d/%02d/', $year, $month );
}
function get_day_link( $year, $month, $day ) {
return sprintf( 'https://example.com/%04d/%02d/%02d/', $year, $month, $day );
}
function is_category() {
return false;
Expand Down Expand Up @@ -83,10 +113,17 @@ function wp_safe_redirect( $url, $status = 302 ) {
$GLOBALS['md_alt_response_headers']['Location'] = $url;
}

$md_alt_router_post = md_alt_test_fixture( 'ordinary' );
$md_alt_route = $argv[1];
$md_alt_mode = $argv[2];
$md_alt_query_vars = array();
$md_alt_router_post = md_alt_test_fixture( 'ordinary' );
$md_alt_route = $argv[1];
$md_alt_mode = $argv[2];
$md_alt_query_vars = array();
if ( is_date() ) {
$md_alt_query_vars = array(
'year' => 2001,
'monthnum' => 2,
'day' => 3,
);
}
$md_alt_response_headers = array();
$md_alt_response_status = 200;
$_SERVER['REQUEST_URI'] = 'md' === $md_alt_route ? '/post.md' : '/post/';
Expand Down
10 changes: 10 additions & 0 deletions tests/security.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,13 @@ function md_alt_router_request( $route, $mode ) {
md_alt_assert_same( 'HTML template', $response['body'] );
md_alt_assert_same( 0, $response['renders'] );
};

$md_alt_tests['Author and date archives retain HTML when Markdown is requested'] = static function () {
foreach ( array( 'author', 'year', 'month', 'day' ) as $archive ) {
$response = md_alt_router_request( 'accept', $archive );
md_alt_assert_same( 200, $response['status'], $archive );
md_alt_assert_same( 'HTML template', $response['body'], $archive );
md_alt_assert_same( false, isset( $response['headers']['Location'] ), $archive );
md_alt_assert_same( 0, $response['renders'], $archive );
}
};
23 changes: 23 additions & 0 deletions tests/term-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,29 @@ function md_alt_term_assert( $condition, $message ) {
)
);
md_alt_term_assert( ! empty( $md_alt_admin_ids ), 'HTTP cache tests have an administrator fixture' );
wp_update_post(
array(
'ID' => $md_alt_term_post_id,
'post_author' => (int) $md_alt_admin_ids[0],
'post_date' => '2001-02-03 12:00:00',
'post_date_gmt' => '2001-02-03 12:00:00',
)
);
$md_alt_accept_args = array(
'headers' => array( 'Accept' => 'text/markdown' ),
'redirection' => 0,
);
foreach ( array( get_author_posts_url( (int) $md_alt_admin_ids[0] ), get_year_link( 2001 ), get_month_link( 2001, 2 ), get_day_link( 2001, 2, 3 ) ) as $md_alt_archive_url ) {
$md_alt_response = wp_remote_get( $md_alt_archive_url, $md_alt_accept_args );
md_alt_term_assert( 200 === wp_remote_retrieve_response_code( $md_alt_response ), 'Unsupported archive stays on its HTML URL: ' . $md_alt_archive_url );
md_alt_term_assert( '' === wp_remote_retrieve_header( $md_alt_response, 'location' ), 'Unsupported archive does not redirect to missing Markdown' );
md_alt_term_assert( 0 === strpos( wp_remote_retrieve_header( $md_alt_response, 'content-type' ), 'text/html' ), 'Unsupported archive response remains HTML' );
}
$md_alt_response = wp_remote_get( get_term_link( $md_alt_term_child ), $md_alt_accept_args );
$md_alt_term_url = UrlConverter::convert_to_markdown_url( get_term_link( $md_alt_term_child ) );
md_alt_term_assert( 303 === wp_remote_retrieve_response_code( $md_alt_response ) && wp_remote_retrieve_header( $md_alt_response, 'location' ) === $md_alt_term_url, 'Supported category still redirects to Markdown' );
$md_alt_response = wp_remote_get( $md_alt_term_url );
md_alt_term_assert( 200 === wp_remote_retrieve_response_code( $md_alt_response ) && 0 === strpos( wp_remote_retrieve_header( $md_alt_response, 'content-type' ), 'text/markdown' ), 'Advertised category Markdown URL resolves successfully' );
$md_alt_auth_cookie = wp_generate_auth_cookie( (int) $md_alt_admin_ids[0], time() + 600, 'logged_in' );
foreach ( array( array( LOGGED_IN_COOKIE => $md_alt_auth_cookie ), array( 'wp_woocommerce_session_fixture' => 'test-session' ) ) as $md_alt_cookies ) {
$md_alt_response = wp_remote_get(
Expand Down
Loading