diff --git a/.gitignore b/.gitignore
index 34bdc0b..ab27d1e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,2 @@
-node_modules
-vendor
-mix-manifest.json
\ No newline at end of file
+/.idea
+/vendor
\ No newline at end of file
diff --git a/README.md b/README.md
index 70f34e3..79aa5e9 100644
--- a/README.md
+++ b/README.md
@@ -58,12 +58,27 @@ class MyReplicatorFieldTransformer
}
```
+### Preserving links targets
+
+When using the Content Renderer within a search transformer, it might be useful to be able to preserve link targets in the rendered output, to be able to find entries based on urls linked in the content. You can instruct the renderer to add link targets in parenthesis behind the link text:
+
+```php
+$renderer = (new Renderer())->$withLinkTargets();
+
+// read more about the author of this package
+// becomes: read more about the author (https://visuellverstehen.de) of this package
+```
+
+### Preserving HTML tags
+
If you want to keep the HTML tags and/or modify the content in your own way, you can instruct the renderer to keep them:
```php
$renderer = (new Renderer())->withHtmlTags();
```
+When you choose to preserve HTML tags, the `withLinkTargets` option (see above) will be ignored.
+
## More about us
- [www.visuellverstehen.de](https://visuellverstehen.de)
diff --git a/src/Renderer.php b/src/Renderer.php
index 52ac663..b9f7dc4 100644
--- a/src/Renderer.php
+++ b/src/Renderer.php
@@ -19,6 +19,7 @@ class Renderer
protected $fieldValue;
protected $viewPath;
protected $withHtmlTags = false;
+ protected $withLinkTargets = false;
public function render(): string
{
@@ -70,6 +71,13 @@ public function withHtmlTags(): self
return $this;
}
+
+ public function withLinkTargets(): self
+ {
+ $this->withLinkTargets = true;
+
+ return $this;
+ }
public function withoutHtmlTags(): self
{
@@ -77,6 +85,13 @@ public function withoutHtmlTags(): self
return $this;
}
+
+ public function withoutLinkTargets(): self
+ {
+ $this->withLinkTargets = false;
+
+ return $this;
+ }
protected function renderContent(): string
{
@@ -153,6 +168,12 @@ protected function sanitizeContent(string $content): string
$content = preg_replace('/\>[\s+]?\', '> <', $content);
if (! $this->withHtmlTags) {
+ // optionally extract link targets and add them in () behind the link name
+ if ($this->withLinkTargets) {
+ $content = preg_replace("/(.+)<\/a>/", '$2 ($1)', $content);
+ }
+
+ // add whitespace between strings within html tags
$content = preg_replace('/\>(\w+)\<\//', '/> $1 <', $content);
$content = strip_tags($content);
}