Skip to content

Commit

Permalink
Update DetectFacets.php (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
puklipo authored Nov 20, 2024
1 parent 80ca527 commit 182b9a7
Showing 1 changed file with 91 additions and 109 deletions.
200 changes: 91 additions & 109 deletions src/RichText/DetectFacets.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,125 +45,107 @@ public function detect(string $text): static

protected function mention(): void
{
$start = 0;

while (preg_match(self::MENTION_REGEX, $this->text, $matches, offset: $start) !== 0) {
$handle = data_get($matches, 3);

if (empty($handle)) {
break;
}

$start = strpos($this->text, $handle, offset: $start + 1) - 1;

if (! Identity::isHandle($handle)) {
$start++;
continue;
}

$did = Bluesky::resolveHandle($handle)->json('did');

if (! Identity::isDID($did)) {
$start++;
continue;
}

$this->facets[] = [
'$type' => self::TYPE,
'index' => [
'byteStart' => $start,
'byteEnd' => $start + strlen($handle) + 1,
],
'features' => [
[
'$type' => Facet::Mention->value,
'did' => $did,
],
],
];
preg_match_all(self::MENTION_REGEX, $this->text, $matches, flags: PREG_OFFSET_CAPTURE);

collect(data_get($matches, 3))
->each(function ($match) {
$handle = data_get($match, 0);
$start = data_get($match, 1);

if (empty($handle) || ! Identity::isHandle($handle)) {
return;
}

$did = Bluesky::resolveHandle($handle)->json('did');

$start++;
}
if (! Identity::isDID($did)) {
return;
}

$this->facets[] = [
'$type' => self::TYPE,
'index' => [
'byteStart' => $start - 1,
'byteEnd' => $start + strlen($handle),
],
'features' => [
[
'$type' => Facet::Mention->value,
'did' => $did,
],
],
];
});
}

protected function link(): void
{
$start = 0;

while (preg_match(self::URL_REGEX, $this->text, $matches, offset: $start) !== 0) {
$uri = data_get($matches, 2);

if (empty($uri)) {
break;
}

$start = strpos($this->text, $uri, offset: $start);
$end = $start + strlen($uri);

if (! Str::startsWith($uri, 'http')) {
$start++;
continue;
}

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

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

$this->facets[] = [
'$type' => self::TYPE,
'index' => [
'byteStart' => $start,
'byteEnd' => $end,
],
'features' => [
[
'$type' => Facet::Link->value,
'uri' => $uri,
preg_match_all(self::URL_REGEX, $this->text, $matches, flags: PREG_OFFSET_CAPTURE);

collect(data_get($matches, 2))
->each(function ($match) {
$uri = data_get($match, 0);
$start = data_get($match, 1);
$end = $start + strlen($uri);

if (! Str::startsWith($uri, 'http')) {
return;
}

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

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

$this->facets[] = [
'$type' => self::TYPE,
'index' => [
'byteStart' => $start,
'byteEnd' => $end,
],
],
];

$start++;
}
'features' => [
[
'$type' => Facet::Link->value,
'uri' => $uri,
],
],
];
});
}

protected function tag(): void
{
$start = 0;

while (preg_match(self::TAG_REGEX, $this->text, $matches, offset: $start) !== 0) {
$tag = data_get($matches, 2);

$tag = Str::of($tag)->replaceMatches(self::TRAILING_PUNCTUATION_REGEX, '')->toString();

if (empty($tag) || strlen($tag) > 64) {
$start++;
continue;
}

$start = strpos($this->text, '#'.$tag, offset: $start);

$this->facets[] = [
'$type' => self::TYPE,
'index' => [
'byteStart' => $start,
'byteEnd' => $start + 1 + strlen($tag),
],
'features' => [
[
'$type' => Facet::Tag->value,
'tag' => $tag,
],
],
];
preg_match_all(self::TAG_REGEX, $this->text, $matches, flags: PREG_OFFSET_CAPTURE);

collect(data_get($matches, 2))
->each(function ($match) {
$tag = data_get($match, 0);
$start = data_get($match, 1) - 1;

$tag = Str::of($tag)->replaceMatches(self::TRAILING_PUNCTUATION_REGEX, '')->toString();

$start++;
}
if (empty($tag) || strlen($tag) > 64) {
return;
}

$this->facets[] = [
'$type' => self::TYPE,
'index' => [
'byteStart' => $start,
'byteEnd' => $start + 1 + strlen($tag),
],
'features' => [
[
'$type' => Facet::Tag->value,
'tag' => $tag,
],
],
];
});
}
}

0 comments on commit 182b9a7

Please sign in to comment.