nothingislost / obsidian-dynamic-highlights

An experimental Obsidian plugin that highlights all occurrences of the word under the cursor
MIT License
134 stars 7 forks source link

Regexp doesn't match if special chars at beginning or end of word #3

Closed kometenstaub closed 2 years ago

kometenstaub commented 2 years ago

I've noticed that it doesn't highlight the word if there is a special character (é, à, ü) at the beginning or end of a word.

highlight-current-word

I looked at the code and it does get the word correctly, but it doesn't apply the span with the CSS class to it.

const view = app.workspace.activeLeaf.view
const word = view.editor.cm.state.wordAt(view.editor.cm.state.selection.main.from)
let wordText = word && view.editor.cm.state.doc.sliceString(word.from, word.to)

image

The issue seems to be the \b in the regular expression:

image image image image

chrisgrieser commented 2 years ago

Not sure how to implement that correctly, but maybe the regexes used by the better word count plugin helps with this issue, since it considers non-ascii characters correctly for the word count? 🤔

https://github.com/lukeleppan/better-word-count/blob/master/src/data/stats.ts

nothingislost commented 2 years ago

Addressed in 0.0.5

kometenstaub commented 2 years ago

It works well now, thank you so much! :heart: