diff --git a/common/new-contract.ts b/common/new-contract.ts index 9ee82c9f..99f27874 100644 --- a/common/new-contract.ts +++ b/common/new-contract.ts @@ -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()) diff --git a/common/util/parse.ts b/common/util/parse.ts index 99e5a82c..1499a5c2 100644 --- a/common/util/parse.ts +++ b/common/util/parse.ts @@ -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) } diff --git a/web/components/contract-card.tsx b/web/components/contract-card.tsx index e682c12a..77d32166 100644 --- a/web/components/contract-card.tsx +++ b/web/components/contract-card.tsx @@ -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 ( @@ -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(' ')}` : '') ) } diff --git a/web/components/contract-feed.tsx b/web/components/contract-feed.tsx index c5ae3ee5..fe1cbaaa 100644 --- a/web/components/contract-feed.tsx +++ b/web/components/contract-feed.tsx @@ -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, diff --git a/web/components/contracts-list.tsx b/web/components/contracts-list.tsx index c28f9251..ef76949c 100644 --- a/web/components/contracts-list.tsx +++ b/web/components/contracts-list.tsx @@ -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'