Skip to content

Commit

Permalink
Replace the feed url with the post url
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk committed Jan 2, 2025
1 parent 1c33612 commit 2cf2b1e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 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, $item->get_permalink() ),
'content' => $this->convert_relative_urls_to_absolute_urls( $item->get_content(), $item->get_permalink() ),
)
);

Expand Down
11 changes: 7 additions & 4 deletions feed-parsers/class-feed-parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,22 @@ public function discover_available_feeds( $content, $url ) { // phpcs:ignore Gen
* Convert relative URLs to absolute ones in incoming content.
*
* @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, $permalink ) {
public function convert_relative_urls_to_absolute_urls( $html, $permalink ) {
if ( ! $html ) {
$html = '';
}

// Strip off the hash.
$permalink = strtok( $permalink, '#' );

// For now this only converts links and image srcs.
return preg_replace_callback(
'~(src|href)=(?:"([^"]+)|\'([^\']+))~i',
function ( $m ) use ( $url, $permalink ) {
function ( $m ) use ( $permalink ) {
if ( str_starts_with( $m[2], '#' ) ) {
// Don't update hash-only links.
return $m[0];
Expand All @@ -102,7 +104,8 @@ function ( $m ) use ( $url, $permalink ) {
return str_replace( $permalink, '', $m[0] );
}

return str_replace( $m[2], Mf2\resolveUrl( $url, $m[2] ), $m[0] );
// Convert relative URLs to absolute ones.
return str_replace( $m[2], Mf2\resolveUrl( $permalink, $m[2] ), $m[0] );
},
$html
);
Expand Down

0 comments on commit 2cf2b1e

Please sign in to comment.