Save tags unique and case-insensitive. Don't clear added tags on updating description.

This commit is contained in:
James Grugett 2022-01-30 21:25:50 -06:00
parent 997f13d986
commit 6427e1bd06
5 changed files with 24 additions and 20 deletions

View File

@ -19,7 +19,7 @@ export function getNewContract(
calcStartPool(initialProb, ante)
const tags = parseTags(
`${extraTags.map((tag) => `#${tag}`).join(' ')} ${question} ${description}`
`${question} ${description} ${extraTags.map((tag) => `#${tag}`).join(' ')}`
)
const lowercaseTags = tags.map((tag) => tag.toLowerCase())

View File

@ -3,19 +3,25 @@ export function parseTags(text: string) {
const matches = (text.match(regex) || []).map((match) =>
match.trim().substring(1)
)
const tagSet = new Set(matches)
const tagSet = new Set()
const uniqueTags: string[] = []
tagSet.forEach((tag) => uniqueTags.push(tag))
// 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()
return uniqueTags
}
export function parseWordsAsTags(text: string) {
const regex = /(?:^|\s)(?:#?[a-z0-9_]+)/gi
const matches = (text.match(regex) || [])
.map((match) => match.replace('#', '').trim())
.filter((tag) => tag)
const tagSet = new Set(matches)
const uniqueTags: string[] = []
tagSet.forEach((tag) => uniqueTags.push(tag))
return uniqueTags
const taggedText = text
.split(/\s+/)
.map((tag) => `#${tag}`)
.join(' ')
return parseTags(taggedText)
}

View File

@ -142,12 +142,9 @@ export function AbbrContractDetails(props: {
export function ContractDetails(props: { contract: Contract }) {
const { contract } = props
const { question, description, closeTime, creatorName, creatorUsername } =
contract
const { closeTime, creatorName, creatorUsername } = contract
const { truePool, createdDate, resolvedDate } = contractMetrics(contract)
const tags = parseTags(`${question} ${description}`).map((tag) => `#${tag}`)
return (
<Col className="text-sm text-gray-500 gap-2 sm:flex-row sm:flex-wrap">
<Row className="gap-2 flex-wrap">
@ -199,10 +196,10 @@ export function ContractDetails(props: { contract: Contract }) {
// String version of the above, to send to the OpenGraph image generator
export function contractTextDetails(contract: Contract) {
const { question, description, closeTime } = contract
const { closeTime, tags } = contract
const { truePool, createdDate, resolvedDate } = contractMetrics(contract)
const tags = parseTags(`${question} ${description}`).map((tag) => `#${tag}`)
const hashtags = tags.map((tag) => `#${tag}`)
return (
`${resolvedDate ? `${createdDate} - ${resolvedDate}` : createdDate}` +
@ -212,6 +209,6 @@ export function contractTextDetails(contract: Contract) {
).format('MMM D, h:mma')}`
: '') +
`${formatMoney(truePool)} pool` +
(tags.length > 0 ? `${tags.join(' ')}` : '')
(hashtags.length > 0 ? `${hashtags.join(' ')}` : '')
)
}

View File

@ -160,7 +160,9 @@ export function ContractDescription(props: {
setEditing(false)
const newDescription = `${contract.description}\n\n${description}`.trim()
const tags = parseTags(`${contract.tags.join(' ')} ${newDescription}`)
const tags = parseTags(
`${newDescription} ${contract.tags.map((tag) => `#${tag}`).join(' ')}`
)
const lowercaseTags = tags.map((tag) => tag.toLowerCase())
await updateContract(contract.id, {
description: newDescription,

View File

@ -11,7 +11,6 @@ import {
import { User } from '../lib/firebase/users'
import { Col } from './layout/col'
import { SiteLink } from './site-link'
import { parseTags } from '../../common/util/parse'
import { ContractCard } from './contract-card'
import { Sort, useQueryAndSortParams } from '../hooks/use-sort-and-query-params'