Fix entities preceded by punctuation marks rendering weird

This commit is contained in:
Alessio 2024-05-03 16:14:58 -07:00
parent 47dbd4fe42
commit 1c874d8e0a
3 changed files with 32 additions and 2 deletions

View File

@ -161,8 +161,10 @@ func get_entities(text string) []Entity {
ret := []Entity{} ret := []Entity{}
start := 0 start := 0
for _, idxs := range regexp.MustCompile(`(\W|^)[@#]\w+`).FindAllStringIndex(text, -1) { for _, idxs := range regexp.MustCompile(`(\W|^)[@#]\w+`).FindAllStringIndex(text, -1) {
// Handle leading whitespace. Only match start-of-string or leading whitespace to avoid matching, e.g., emails // The character immediately preceding the entity must not be a word character (alphanumeric
if text[idxs[0]] == ' ' || text[idxs[0]] == '\n' { // or "_"). This is to avoid matching emails. Accordingly, if the first character in the
// match isn't a '@' or '#' (i.e., there's a preceding character), skip past it.
if text[idxs[0]] != '@' && text[idxs[0]] != '#' {
idxs[0] += 1 idxs[0] += 1
} }
if start != idxs[0] { if start != idxs[0] {

View File

@ -0,0 +1,23 @@
package webserver
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestEntitiesWithParentheses(t *testing.T) {
assert := assert.New(t)
entities := get_entities("Companies are looking for ways to reduce costs (@BowTiedBull has said this), through process automation.)")
assert.Len(entities, 3)
assert.Equal(entities[0].EntityType, ENTITY_TYPE_TEXT)
assert.Equal(entities[0].Contents, "Companies are looking for ways to reduce costs (")
assert.Equal(entities[1].EntityType, ENTITY_TYPE_MENTION)
assert.Equal(entities[1].Contents, "BowTiedBull")
assert.Equal(entities[2].EntityType, ENTITY_TYPE_TEXT)
assert.Equal(entities[2].Contents, " has said this), through process automation.)")
}

View File

@ -10,6 +10,11 @@
<a class="entity" href="/search/%23{{.Contents}}">#{{.Contents}}</a> <a class="entity" href="/search/%23{{.Contents}}">#{{.Contents}}</a>
{{else}} {{else}}
<!-- Just text --> <!-- Just text -->
<!-- TODO: Fix extra spaces being inserted between entities and text
- e.g., `(@asdf)` renders as `( @asdf )`
- https://css-tricks.com/fighting-the-space-between-inline-block-elements/
-->
{{.Contents}} {{.Contents}}
{{end}} {{end}}
{{end}} {{end}}