diff --git a/app/Http/Controllers/NotesController.php b/app/Http/Controllers/NotesController.php index ef215f58..a501be81 100644 --- a/app/Http/Controllers/NotesController.php +++ b/app/Http/Controllers/NotesController.php @@ -155,17 +155,9 @@ class NotesController extends Controller */ public function taggedNotes($tag) { - $tag = mb_strtolower( - preg_replace( - '/&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml|caron);/i', - '$1', - htmlentities($tag) - ), - 'UTF-8' - ); - - $tagId = Tag::where('tag', $tag)->pluck('id'); - $notes = Tag::find($tagId)->notes()->orderBy('updated_at', 'desc')->get(); + $notes = Note::whereHas('tags', function ($query) use ($tag) { + $query->where('tag', $tag); + })->get(); foreach ($notes as $note) { $note->iso8601_time = $note->updated_at->toISO8601String(); $note->human_time = $note->updated_at->diffForHumans(); diff --git a/app/Note.php b/app/Note.php index 29213e14..2e8c53c7 100644 --- a/app/Note.php +++ b/app/Note.php @@ -2,6 +2,7 @@ namespace App; +use App\Tag; use Normalizer; use Jonnybarnes\IndieWeb\Numbers; use Illuminate\Database\Eloquent\Model; @@ -216,7 +217,7 @@ class Note extends Model implements HasMedia foreach ($matches[0] as $name) { $name = str_replace('#', '', $name); $replacements[$name] = - ''; + ''; } // Replace #tags with valid microformat-enabled link diff --git a/app/Tag.php b/app/Tag.php index fb55663a..e6c6bcac 100644 --- a/app/Tag.php +++ b/app/Tag.php @@ -36,4 +36,32 @@ class Tag extends Model * @var array */ protected $guarded = ['id']; + + /** + * Normalize tags so they’re lowercase and fancy diatrics are removed. + * + * @param string + */ + public function setTagAttribute($value) + { + $this->attributes['tag'] = $this->normalizeTag($value); + } + + /** + * This method actually normalizes a tag. That means lowercase-ing and + * removing fancy diatric characters. + * + * @param string + */ + public static function normalizeTag($tag) + { + return mb_strtolower( + preg_replace( + '/&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml|caron);/i', + '$1', + htmlentities($tag) + ), + 'UTF-8' + ); + } } diff --git a/changelog.md b/changelog.md index affb1d3b..e123718a 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ # Changelog +## Version {next} + - Better tag normalization code organisation + ## Version 0.0.2 (2016-05-25) - Fix issue#1: tagged notes page needs the tag from the URL normalizing.