z3by / vuepress-plugin-flexsearch

Next-Generation full text search library for Vuepress
MIT License
41 stars 16 forks source link

overlapping search tokens produce broken HTML output #8

Closed karottenreibe closed 4 years ago

karottenreibe commented 4 years ago

E.g:

image

The problem is that the search and replace in https://github.com/z3by/vuepress-plugin-flexsearch/blob/b24f602e1b1fcd59b3f78fed0ca1e577830aa90b/SearchBox.vue#L208 does overlapping replacements, i.e. will also replace parts of the inserted html tags.

Code snippet to demonstrate this:

queryWords = ["install", "text"]
querySnippet = "install"
for (const word of queryWords) {
        querySnippet = querySnippet.replace(new RegExp(word, 'gi'), `<strong class="text--primary">${word}</strong>`)
}

Running this results in

"<strong class=\"<strong class=\"text--primary\">text</strong>--primary\">install</strong>"

(i.e. the "text" of "text--primary" also gets replaced)

karottenreibe commented 4 years ago

I took the liberty to create a pull request to fix this :-)

z3by commented 4 years ago

Thank you for contributing to this package! @karottenreibe 😄