3 Commits

  1. 13
      CHANGELOG.md
  2. 2
      assets/js/hooks.js
  3. 23
      lib/mirage/search.ex
  4. 2
      mix.exs

13
CHANGELOG.md

@ -5,6 +5,19 @@ See [Conventional Commits](Https://conventionalcommits.org) for commit guideline
<!-- changelog -->
## [v0.88.0](https://git.inhji.de/inhji/mirage/compare/v0.87.0...v0.88.0) (2021-03-24)
### Features:
* prevent submit for search form
### Bug Fixes:
* use regex to find and mark occurrences
## [v0.87.0](https://git.inhji.de/inhji/mirage/compare/v0.86.1...v0.87.0) (2021-03-23)

2
assets/js/hooks.js

@ -18,6 +18,8 @@ const SearchForm = {
if (queryInput) {
queryInput.focus()
queryInput.form.addEventListener("submit", e => e.preventDefault())
}
}
}

23
lib/mirage/search.ex

@ -20,13 +20,13 @@ defmodule Mirage.Search do
type CANNOT be an atom, otherwise it gets assigned wrongly!
"""
def search_notes(search_term) do
search_term = String.downcase(search_term)
downcased_term = String.downcase(search_term)
link_query =
from l in Mirage.Links.Link,
where: contains(l.title, ^search_term),
or_where: contains(l.content, ^search_term),
or_where: contains(l.url, ^search_term),
where: contains(l.title, ^downcased_term),
or_where: contains(l.content, ^downcased_term),
or_where: contains(l.url, ^downcased_term),
select: %{
type: "link",
id: l.id,
@ -37,8 +37,8 @@ defmodule Mirage.Search do
note_query =
from n in Mirage.Notes.Note,
where: contains(n.content, ^search_term),
or_where: contains(n.title, ^search_term),
where: contains(n.content, ^downcased_term),
or_where: contains(n.title, ^downcased_term),
select: %{
type: "note",
id: n.id,
@ -50,7 +50,7 @@ defmodule Mirage.Search do
note_query
|> Repo.all()
|> Enum.map(&find_occurrence(&1, search_term))
|> Enum.map(&find_occurrence(&1, downcased_term))
|> Enum.filter(&remove_false_positives/1)
|> Enum.map(&highlight_occurrence(&1, search_term))
end
@ -67,8 +67,13 @@ defmodule Mirage.Search do
result
else
{_index, _length, prop} = occurrence
fffound = Map.get(result, prop)
replaced = String.replace(fffound, search_term, "<mark>#{search_term}</mark>")
property_value = Map.get(result, prop)
replaced =
search_term
|> String.downcase()
|> Regex.compile!("i")
|> Regex.replace(property_value, "<mark>#{search_term}</mark>")
result
|> Map.put_new(:found_value, replaced)

2
mix.exs

@ -1,7 +1,7 @@
defmodule Mirage.MixProject do
use Mix.Project
@version "0.87.0"
@version "0.88.0"
def project do
[

Loading…
Cancel
Save