Skip to content

Commit

Permalink
Supports localhost #6
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Jan 27, 2025
1 parent 43845f0 commit 20d5858
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Autolink.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,18 @@ public function __construct(array $options = [], array $schemes = [])
public function convert(string $text, array $attribs = []): string
{
$linkNoScheme = $this->getLinkNoScheme();
$staticDomains = '|localhost';

if ($linkNoScheme) {
$schemeRegex = "[(%s)\:\/\/@]*";
$staticDomains = '';
} else {
$schemeRegex = "(%s)\:\/\/";
}

$schemeRegex = sprintf($schemeRegex, $this->getSchemes(true));

$regex = '/(([a-zA-Z]*=")*' . $schemeRegex . "[\-\p{L}\p{N}\p{M}]+\.[\p{L}\p{M}]{2,}([\/\p{L}\p{N}\p{M}\-._~:?#\[\]@!$&'()*+,;=%\">]*)?)/u";
$regex = '/(([a-zA-Z]*=")*' . $schemeRegex . "([\-\p{L}\p{N}\p{M}]+\.[\p{L}\p{M}]{2,}$staticDomains)([\/\p{L}\p{N}\p{M}\-._~:?#\[\]@!$&'()*+,;=%\">]*)?)/u";

return preg_replace_callback(
$regex,
Expand Down
32 changes: 32 additions & 0 deletions test/AutolinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,38 @@ public function testIgnoreTrailingDot(): void
);
}

public function testLocalhost(): void
{
$txt = 'Link to http://localhost with some text.';

$html = $this->instance->convert($txt);

assertEquals(
'Link to <a href="http://localhost">http://localhost</a> with some text.',
$html,
);

$txt = 'Link to http://localhost.';

$html = $this->instance->convert($txt);

assertEquals(
'Link to <a href="http://localhost">http://localhost</a>.',
$html,
);

// Localhost without scheme should be ignored.
$txt = 'Link to localhost.';

$this->instance->linkNoScheme(true);
$html = $this->instance->convert($txt);

assertEquals(
'Link to localhost.',
$html,
);
}

/**
* urlProvider
*
Expand Down

0 comments on commit 20d5858

Please sign in to comment.