Skip to content

Commit

Permalink
Merge pull request #3 from steveparks/upgrade-to-v5
Browse files Browse the repository at this point in the history
Upgrade to for Statamic 5.x, and provide as an addon
  • Loading branch information
jackmcdade authored Jun 27, 2024
2 parents 1af6941 + ba2bea2 commit 0c3c14c
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 21 deletions.
7 changes: 4 additions & 3 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
## Installing

- Install by copying the files into `site/addons/Wikilinks`.
- That's it.
```
composer require statamic/wikilinks
```

## Usage

Wikilinks is a simple modifier that seeks out any content wrapped in [braces] and automatically links it to other entries or pages with the same title (or other field of your choosing).
Wikilinks provides a simple modifier that seeks out any content wrapped in [braces] and automatically links it to other entries or pages with the same title (or other field of your choosing).

### Example

Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Wikilinks ![Statamic v2](https://img.shields.io/badge/statamic-v2-blue.svg?style=flat-square)
# Wikilinks ![Statamic v5](https://img.shields.io/badge/statamic-v5-blue.svg?style=flat-square)

## Modifier

Wikilinks is a simple modifier that seeks out any content wrapped in [braces] and automatically links it to other entries or pages with the same title (or other field of your choosing).
Wikilinks is an add-on for Statamic 5.x that enables Wiki style [links] in the content of your site.

It provides a simple modifier that seeks out any content wrapped in [braces] and automatically links it to other entries or pages with the same title (or other field of your choosing).

```
{{ content | wikilinks }}
```

See the Documentation for more details of usage.
37 changes: 37 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "statamic/wikilinks",
"autoload": {
"psr-4": {
"Statamic\\Wikilinks\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Statamic\\Wikilinks\\Tests\\": "tests"
}
},
"require": {
"statamic/cms": "^5.0"
},
"require-dev": {
"orchestra/testbench": "^9.0"
},
"config": {
"allow-plugins": {
"pixelfear/composer-dist-plugin": true
}
},
"extra": {
"statamic": {
"name": "Wikilinks",
"description": "Wikilinks addon"
},
"laravel": {
"providers": [
"Statamic\\Wikilinks\\ServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
6 changes: 0 additions & 6 deletions meta.yaml

This file was deleted.

24 changes: 15 additions & 9 deletions WikilinksModifier.php → src/Modifiers/WikilinksModifier.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php

namespace Statamic\Addons\Wikilinks;
use Statamic\API\Search;
use Statamic\API\Content;
use Statamic\Extend\Modifier;
namespace Statamic\Wikilinks\Modifiers;

use Statamic\Facades\Entry;
use Illuminate\Support\Arr;
use Statamic\Modifiers\Modifier;

class WikilinksModifier extends Modifier
{
protected static $handle = 'wikilinks';

/**
* Maps to {{ var | wikilinks }}
*
Expand All @@ -23,15 +26,18 @@ public function index($value, $params, $context)

$replacements = [];

$field = array_get($params, 0, 'title');
$field = data_get($params, 0, 'title');

foreach($matches[1] as $index => $query) {
if ($result = Search::get($query, [$field])) {
$result = Entry::query()
->where($field, $query)
->first();

if ($result) {

$id = array_get($result->first(), 'id');
$url = Content::find($id)->url();
$uri = $result->uri;

$replacements[] = ($url) ? "<a href='{$url}'>{$query}</a>" : $query;
$replacements[] = ($uri) ? "<a href='{$uri}' title='{$query}'>{$query}</a>" : $query;
}
}
return str_replace($matches[0], $replacements, $value);
Expand Down
18 changes: 18 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Statamic\Wikilinks;

use Statamic\Wikilinks\Modifiers\WikilinksModifier;
use Statamic\Providers\AddonServiceProvider;

class ServiceProvider extends AddonServiceProvider
{
public function bootAddon()
{
//
}

protected $modifiers = [
WikilinksModifier::class,
];
}

0 comments on commit 0c3c14c

Please sign in to comment.