Fix entities preceded by punctuation marks rendering weird
This commit is contained in:
parent
47dbd4fe42
commit
1c874d8e0a
@ -161,8 +161,10 @@ func get_entities(text string) []Entity {
|
||||
ret := []Entity{}
|
||||
start := 0
|
||||
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
|
||||
if text[idxs[0]] == ' ' || text[idxs[0]] == '\n' {
|
||||
// The character immediately preceding the entity must not be a word character (alphanumeric
|
||||
// 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
|
||||
}
|
||||
if start != idxs[0] {
|
||||
|
23
internal/webserver/renderer_helpers_test.go
Normal file
23
internal/webserver/renderer_helpers_test.go
Normal 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.)")
|
||||
}
|
||||
|
||||
|
||||
|
@ -10,6 +10,11 @@
|
||||
<a class="entity" href="/search/%23{{.Contents}}">#{{.Contents}}</a>
|
||||
{{else}}
|
||||
<!-- 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}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
Loading…
x
Reference in New Issue
Block a user