---
title: "Fixing the search"
slug: fixing-the-search
section: tech
date: 2015-05-19T16:05:00.000Z
canonical: https://roland.leth.ro/blog/tech/fixing-the-search
---

First of all, I changed the `<search-mark>` tag to `<span class='search-mark'>`. Secondly, I'm properly adding the `search-mark` to all occurrences of the searched term, instead of only the first (silly me). Lastly, I search the content again to remove the `search-mark` from inside `href`s:

```ruby
start_index = content.downcase.index(w)
end_index = start_index + w.length
original_occurrence = content[start_index..end_index - 1]
content.gsub!(/#{original_occurrence}/i, '<span class=\'search-mark\'>\0</span>')
content.scan(/href=".*?"/).each do |s|
  edited_link = s.gsub('<span class=\'search-mark\'>', '')
  edited_link.gsub!('</span>', '')
  content.gsub!(s, edited_link)
end
```