notes: replace hashtags and mentions

This commit is contained in:
HookedBehemoth 2022-06-29 01:11:07 +02:00
parent 7127054a12
commit f87c91780c
2 changed files with 12 additions and 3 deletions

View File

@ -27,6 +27,8 @@ let
userPicRegex = re"_(normal|bigger|mini|200x200|400x400)(\.[A-z]+)$"
extRegex = re"(\.[A-z]+)$"
illegalXmlRegex = re"(*UTF8)[^\x09\x0A\x0D\x20-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]"
hashtagRegex = re"#(\w+)"
mentionRegex = re"@(\w+)"
proc getUrlPrefix*(cfg: Config): string =
if cfg.useHttps: https & cfg.hostname
@ -78,6 +80,13 @@ proc replaceUrls*(body: string; prefs: Prefs; absolute=""): string =
if absolute.len > 0 and "href" in result:
result = result.replace("href=\"/", &"href=\"{absolute}/")
proc replaceHashtagsAndMentions*(body: string): string =
result = body
result = result.replacef(hashtagRegex, a(
"#$1", href = "/search?q=%23$1"))
result = result.replacef(mentionRegex, a(
"@$1", href = "/$1"))
proc getM3u8Url*(content: string): string =
var matches: array[1, string]
if re.find(content, m3u8Regex, matches) != -1:

View File

@ -51,13 +51,13 @@ proc renderNoteParagraph(articleParagraph: ArticleParagraph; article: Article):
for er in articleParagraph.entityRanges:
# flush remaining text
if er.offset > last:
result.add text text.runeSubStr(last, er.offset - last)
result.add verbatim text.runeSubStr(last, er.offset - last).replaceHashtagsAndMentions
let entity = article.entities[er.key]
case entity.entityType
of ArticleEntityType.link:
let link = buildHtml(a(href=entity.url)):
text text.runeSubStr(er.offset, er.length)
verbatim text.runeSubStr(er.offset, er.length).replaceHashtagsAndMentions
result.add link
of ArticleEntityType.media:
for id in entity.mediaIds:
@ -79,7 +79,7 @@ proc renderNoteParagraph(articleParagraph: ArticleParagraph; article: Article):
# flush remaining text
if last < text.len:
result.add text text.runeSubStr(last)
result.add verbatim text.runeSubStr(last).replaceHashtagsAndMentions
proc renderNote*(article: Article; prefs: Prefs): VNode =
let cover = getSmallPic(article.coverImage)