Skip to content

Commit

Permalink
Fix in-article links
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk committed Jan 1, 2025
1 parent 2336ac2 commit 1c33612
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion feed-parsers/class-feed-parser-simplepie.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public function process_items( $items, $url ) {
array(
'permalink' => $item->get_permalink(),
'title' => $title,
'content' => $this->convert_relative_urls_to_absolute_urls( $item->get_content(), $url ),
'content' => $this->convert_relative_urls_to_absolute_urls( $item->get_content(), $url, $item->get_permalink() ),
)
);

Expand Down
16 changes: 14 additions & 2 deletions feed-parsers/class-feed-parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,29 @@ public function discover_available_feeds( $content, $url ) { // phpcs:ignore Gen
*
* @param string $html The html.
* @param string $url The url of the feed.
* @param string $permalink The permalink of the feed.
*
* @return string The HTML with URLs replaced to their absolute represenation.
*/
public function convert_relative_urls_to_absolute_urls( $html, $url ) {
public function convert_relative_urls_to_absolute_urls( $html, $url, $permalink ) {
if ( ! $html ) {
$html = '';
}
$permalink = strtok( $permalink, '#' );
// For now this only converts links and image srcs.
return preg_replace_callback(
'~(src|href)=(?:"([^"]+)|\'([^\']+))~i',
function ( $m ) use ( $url ) {
function ( $m ) use ( $url, $permalink ) {
if ( str_starts_with( $m[2], '#' ) ) {
// Don't update hash-only links.
return $m[0];
}

if ( str_starts_with( $m[2], $permalink . '#' ) ) {
// Remove absolute URL from hashes.
return str_replace( $permalink, '', $m[0] );
}

return str_replace( $m[2], Mf2\resolveUrl( $url, $m[2] ), $m[0] );
},
$html
Expand Down

0 comments on commit 1c33612

Please sign in to comment.