Skip to content

Commit af535dd

Browse files
committed
Auto-link URLs in plain text content
Use WordPress's make_clickable() function to automatically convert URLs in plain text content to clickable links. This only applies to plain text content, not HTML content which is preserved as-is. Fixes #303
1 parent 36b541c commit af535dd

2 files changed

Lines changed: 84 additions & 1 deletion

File tree

includes/rest/class-endpoint-controller.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,8 @@ protected function mp_to_wp( $mf2 ) {
831831
$args['post_content'] = $content['html'] ? $content['html'] :
832832
\htmlspecialchars( $content['value'] );
833833
} elseif ( $content ) {
834-
$args['post_content'] = \htmlspecialchars( $content );
834+
// Auto-link URLs in plain text content.
835+
$args['post_content'] = \make_clickable( \htmlspecialchars( $content ) );
835836
}
836837
}
837838

tests/test_endpoint.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,4 +815,86 @@ function test_create_publish_default_status() {
815815
$post = self::check_create( self::create_json_request( $input ) );
816816
$this->assertEquals( 'publish', $post->post_status );
817817
}
818+
819+
function test_create_plain_text_autolinks_urls() {
820+
$input = array(
821+
'type' => array( 'h-entry' ),
822+
'properties' => array(
823+
'content' => array( 'Check out https://example.com for more info' ),
824+
),
825+
);
826+
$post = self::check_create( self::create_json_request( $input ) );
827+
// URLs in plain text content should be auto-linked.
828+
$this->assertStringContainsString( '<a href="https://example.com"', $post->post_content );
829+
$this->assertStringContainsString( 'https://example.com</a>', $post->post_content );
830+
}
831+
832+
function test_create_plain_text_autolinks_multiple_urls() {
833+
$input = array(
834+
'type' => array( 'h-entry' ),
835+
'properties' => array(
836+
'content' => array( 'Visit https://example.com and http://test.org today' ),
837+
),
838+
);
839+
$post = self::check_create( self::create_json_request( $input ) );
840+
// Both URLs should be auto-linked.
841+
$this->assertStringContainsString( '<a href="https://example.com"', $post->post_content );
842+
$this->assertStringContainsString( '<a href="http://test.org"', $post->post_content );
843+
}
844+
845+
function test_create_html_content_not_autolinked() {
846+
$input = array(
847+
'type' => array( 'h-entry' ),
848+
'properties' => array(
849+
'content' => array(
850+
array( 'html' => '<p>Visit https://example.com today</p>' ),
851+
),
852+
),
853+
);
854+
$post = self::check_create( self::create_json_request( $input ) );
855+
// HTML content should not be auto-linked (preserved as-is).
856+
$this->assertEquals( '<p>Visit https://example.com today</p>', $post->post_content );
857+
}
858+
859+
function test_create_html_content_existing_link_preserved() {
860+
$input = array(
861+
'type' => array( 'h-entry' ),
862+
'properties' => array(
863+
'content' => array(
864+
array( 'html' => '<p>Visit <a href="https://example.com">my site</a> today</p>' ),
865+
),
866+
),
867+
);
868+
$post = self::check_create( self::create_json_request( $input ) );
869+
// Existing links in HTML content should be preserved.
870+
$this->assertEquals( '<p>Visit <a href="https://example.com">my site</a> today</p>', $post->post_content );
871+
}
872+
873+
function test_create_html_content_code_block_preserved() {
874+
$input = array(
875+
'type' => array( 'h-entry' ),
876+
'properties' => array(
877+
'content' => array(
878+
array( 'html' => '<p>Example: <code>https://example.com/api</code></p>' ),
879+
),
880+
),
881+
);
882+
$post = self::check_create( self::create_json_request( $input ) );
883+
// URLs in code blocks should not be auto-linked.
884+
$this->assertEquals( '<p>Example: <code>https://example.com/api</code></p>', $post->post_content );
885+
}
886+
887+
function test_create_html_content_pre_block_preserved() {
888+
$input = array(
889+
'type' => array( 'h-entry' ),
890+
'properties' => array(
891+
'content' => array(
892+
array( 'html' => '<pre>curl https://example.com/api</pre>' ),
893+
),
894+
),
895+
);
896+
$post = self::check_create( self::create_json_request( $input ) );
897+
// URLs in pre blocks should not be auto-linked.
898+
$this->assertEquals( '<pre>curl https://example.com/api</pre>', $post->post_content );
899+
}
818900
}

0 commit comments

Comments
 (0)