Skip to content

Commit

Permalink
Update DetectFacets.php (#6)
Browse files Browse the repository at this point in the history
* Update TextBuilderTest.php

* Update DetectFacets.php
  • Loading branch information
puklipo authored Nov 20, 2024
1 parent 182b9a7 commit 99e0859
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
20 changes: 10 additions & 10 deletions src/RichText/DetectFacets.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,19 @@ protected function link(): void
$start = data_get($match, 1);
$end = $start + strlen($uri);

if (! Str::startsWith($uri, 'http')) {
return;
if (! Str::startsWith($uri, 'http') && Str::isUrl('https://'.$uri, ['https'])) {
$uri = 'https://'.$uri;
}

$uri = Str::of($uri)->whenTest('/[.,;:!?]$/', function (Stringable $string) use (&$end) {
$end--;
return $string->rtrim('.,;:!?');
})->toString();
if (Str::of($uri)->test('/[.,;:!?]$/')) {
$uri = Str::rtrim($uri, '.,;:!?');
$end = $start + strlen($uri);
}

$uri = Str::of($uri)->whenTest('/[)]$/', function (Stringable $string) use (&$end) {
$end--;
return $string->rtrim(')');
})->toString();
if (Str::of($uri)->test('/[)]$/') && Str::doesntContain($uri, '(')) {
$uri = Str::rtrim($uri, ')');
$end = $start + strlen($uri);
}

$this->facets[] = [
'$type' => self::TYPE,
Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/RichText/TextBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public function test_detect_facets_mention()

public function test_detect_facets_link()
{
$builder = TextBuilder::make('https://localhost https://localhost? test test (https://localhost) example.com https://')->detectFacets();
$builder = TextBuilder::make('https://localhost https://localhost/?test=a test test (https://localhost) example.com https://localhost/.,;:!? https://localhost/#hash https://')->detectFacets();

$this->assertIsArray($builder->facets);
$this->assertSame(0, data_get($builder->facets, '0.index.byteStart'));
$this->assertSame(17, data_get($builder->facets, '0.index.byteEnd'));
$this->assertSame(48, data_get($builder->facets, '2.index.byteStart'));
$this->assertSame(65, data_get($builder->facets, '2.index.byteEnd'));
$this->assertSame(55, data_get($builder->facets, '2.index.byteStart'));
$this->assertSame(72, data_get($builder->facets, '2.index.byteEnd'));

$this->assertSame(['https://localhost', 'https://localhost', 'https://localhost'], collect($builder->facets)->pluck('features.0.uri')->toArray());
$this->assertSame(['https://localhost', 'https://localhost/?test=a', 'https://localhost','https://example.com', 'https://localhost/', 'https://localhost/#hash'], collect($builder->facets)->pluck('features.0.uri')->toArray());
}

public function test_detect_facets_tag()
Expand Down

0 comments on commit 99e0859

Please sign in to comment.