2022-03-09 17:08:57 +00:00
|
|
|
import { MAX_TAG_LENGTH } from '../contract'
|
|
|
|
|
2022-01-21 23:21:46 +00:00
|
|
|
export function parseTags(text: string) {
|
|
|
|
const regex = /(?:^|\s)(?:[#][a-z0-9_]+)/gi
|
|
|
|
const matches = (text.match(regex) || []).map((match) =>
|
2022-03-09 17:08:57 +00:00
|
|
|
match.trim().substring(1).substring(0, MAX_TAG_LENGTH)
|
2022-01-21 23:21:46 +00:00
|
|
|
)
|
2022-01-31 03:25:50 +00:00
|
|
|
const tagSet = new Set()
|
2022-01-21 23:21:46 +00:00
|
|
|
const uniqueTags: string[] = []
|
2022-01-31 03:25:50 +00:00
|
|
|
// Keep casing of last tag.
|
|
|
|
matches.reverse()
|
|
|
|
for (const tag of matches) {
|
|
|
|
const lowercase = tag.toLowerCase()
|
|
|
|
if (!tagSet.has(lowercase)) {
|
|
|
|
tagSet.add(lowercase)
|
|
|
|
uniqueTags.push(tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uniqueTags.reverse()
|
2022-01-21 23:21:46 +00:00
|
|
|
return uniqueTags
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseWordsAsTags(text: string) {
|
2022-01-31 03:25:50 +00:00
|
|
|
const taggedText = text
|
|
|
|
.split(/\s+/)
|
2022-02-01 17:02:30 +00:00
|
|
|
.map((tag) => (tag.startsWith('#') ? tag : `#${tag}`))
|
2022-01-31 03:25:50 +00:00
|
|
|
.join(' ')
|
|
|
|
return parseTags(taggedText)
|
2022-01-21 23:21:46 +00:00
|
|
|
}
|