Skip to content

Commit

Permalink
support hashtag
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn committed Feb 29, 2024
1 parent dc19b91 commit 693ed59
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
28 changes: 28 additions & 0 deletions extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
const (
urlPattern = `https?://[-A-Za-z0-9+&@#\/%?=~_|!:,.;\(\)]+`
mentionPattern = `@[a-zA-Z0-9.]+`
tagPattern = `\B#\S+`
)

var (
urlRe = regexp.MustCompile(urlPattern)
mentionRe = regexp.MustCompile(mentionPattern)
tagRe = regexp.MustCompile(tagPattern)
)

type entry struct {
Expand Down Expand Up @@ -72,3 +74,29 @@ func extractMentionsBytes(text string) []entry {
}
return result
}

func extractTags(text string) []entry {
var result []entry
matches := tagRe.FindAllStringSubmatchIndex(text, -1)
for _, m := range matches {
result = append(result, entry{
text: strings.TrimPrefix(text[m[0]:m[1]], "#"),
start: int64(len([]rune(text[0:m[0]]))),
end: int64(len([]rune(text[0:m[1]])))},
)
}
return result
}

func extractTagsBytes(text string) []entry {
var result []entry
matches := tagRe.FindAllStringSubmatchIndex(text, -1)
for _, m := range matches {
result = append(result, entry{
text: strings.TrimPrefix(text[m[0]:m[1]], "#"),
start: int64(len(text[0:m[0]])),
end: int64(len(text[0:m[1]]))},
)
}
return result
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ toolchain go1.21.0

require (
github.com/PuerkitoBio/goquery v1.8.1
github.com/bluesky-social/indigo v0.0.0-20240208004502-c72ce2340db3
github.com/bluesky-social/indigo v0.0.0-20240228193119-0f7714848b28
github.com/fatih/color v1.16.0
github.com/gorilla/websocket v1.5.1
github.com/ipfs/go-cid v0.4.1
github.com/urfave/cli/v2 v2.26.0
)

require (
github.com/RussellLuo/slidingwindow v0.0.0-20200528002341-535bb99d338b // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/carlmjohnson/versioninfo v0.22.5 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
github.com/RussellLuo/slidingwindow v0.0.0-20200528002341-535bb99d338b h1:5/++qT1/z812ZqBvqQt6ToRswSuPZ/B33m6xVHRzADU=
github.com/RussellLuo/slidingwindow v0.0.0-20200528002341-535bb99d338b/go.mod h1:4+EPqMRApwwE/6yo6CxiHoSnBzjRr3jsqer7frxP8y4=
github.com/alexbrainman/goissue34681 v0.0.0-20191006012335-3fc7a47baff5 h1:iW0a5ljuFxkLGPNem5Ui+KBjFJzKg4Fv2fnxe4dvzpM=
github.com/alexbrainman/goissue34681 v0.0.0-20191006012335-3fc7a47baff5/go.mod h1:Y2QMoi1vgtOIfc+6DhrMOGkLoGzqSV2rKp4Sm+opsyA=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
Expand All @@ -11,8 +13,8 @@ github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bluesky-social/indigo v0.0.0-20240208004502-c72ce2340db3 h1:jy20dRWSP6woXLf1jn3JhOmou422fM1GK8mjwYJAXIE=
github.com/bluesky-social/indigo v0.0.0-20240208004502-c72ce2340db3/go.mod h1:N3Fv7QoBtarvhoHtGLP2U+my6ZbAetbPMwAFTtRAUrI=
github.com/bluesky-social/indigo v0.0.0-20240228193119-0f7714848b28 h1:e8l9/8aWmk5zYsj663H9SsKcQWLvWSwmnDOAdHiQdjI=
github.com/bluesky-social/indigo v0.0.0-20240228193119-0f7714848b28/go.mod h1:zheM9Nt+x0CPWv7cQ/16tOFuxDRxr0fDjLTXRNfpKgQ=
github.com/carlmjohnson/versioninfo v0.22.5 h1:O00sjOLUAFxYQjlN/bzYTuZiS0y6fWDQjMRvwtKgwwc=
github.com/carlmjohnson/versioninfo v0.22.5/go.mod h1:QT9mph3wcVfISUKd0i9sZfVrPviHuSF+cUtLjm2WSf8=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
Expand All @@ -39,6 +41,8 @@ github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg=
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
Expand Down
16 changes: 16 additions & 0 deletions timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,22 @@ func doPost(cCtx *cli.Context) error {
})
}

for _, entry := range extractTagsBytes(text) {
post.Facets = append(post.Facets, &bsky.RichtextFacet{
Features: []*bsky.RichtextFacet_Features_Elem{
{
RichtextFacet_Tag: &bsky.RichtextFacet_Tag{
Tag: entry.text,
},
},
},
Index: &bsky.RichtextFacet_ByteSlice{
ByteStart: entry.start,
ByteEnd: entry.end,
},
})
}

// embeded images
imageFn := cCtx.StringSlice("image")
imageAltFn := cCtx.StringSlice("image-alt")
Expand Down

0 comments on commit 693ed59

Please sign in to comment.