From c3ffac34a11598ffa5587d1228d243043d89f4c3 Mon Sep 17 00:00:00 2001 From: Sinclair Chen Date: Mon, 3 Oct 2022 18:28:21 -0700 Subject: [PATCH] Remove tag parsing (#956) * Remove #tag parsing * Remove #tag linkifying * lint --- common/new-contract.ts | 14 +----- common/util/parse.ts | 29 ------------ functions/src/scripts/update-contract-tags.ts | 46 ------------------- .../contract/contract-description.tsx | 14 +----- web/components/linkify.tsx | 6 +-- 5 files changed, 6 insertions(+), 103 deletions(-) delete mode 100644 functions/src/scripts/update-contract-tags.ts diff --git a/common/new-contract.ts b/common/new-contract.ts index 431f435e..3580b164 100644 --- a/common/new-contract.ts +++ b/common/new-contract.ts @@ -12,7 +12,6 @@ import { visibility, } from './contract' import { User } from './user' -import { parseTags, richTextToString } from './util/parse' import { removeUndefinedProps } from './util/object' import { JSONContent } from '@tiptap/core' @@ -38,15 +37,6 @@ export function getNewContract( answers: string[], visibility: visibility ) { - const tags = parseTags( - [ - question, - richTextToString(description), - ...extraTags.map((tag) => `#${tag}`), - ].join(' ') - ) - const lowercaseTags = tags.map((tag) => tag.toLowerCase()) - const propsByOutcomeType = outcomeType === 'BINARY' ? getBinaryCpmmProps(initialProb, ante) // getBinaryDpmProps(initialProb, ante) @@ -70,8 +60,8 @@ export function getNewContract( question: question.trim(), description, - tags, - lowercaseTags, + tags: [], + lowercaseTags: [], visibility, isResolved: false, createdTime: Date.now(), diff --git a/common/util/parse.ts b/common/util/parse.ts index 0bb26454..0c50e07d 100644 --- a/common/util/parse.ts +++ b/common/util/parse.ts @@ -1,4 +1,3 @@ -import { MAX_TAG_LENGTH } from '../contract' import { generateText, JSONContent } from '@tiptap/core' // Tiptap starter extensions import { Blockquote } from '@tiptap/extension-blockquote' @@ -33,34 +32,6 @@ export function getUrl(text: string) { return results.length ? results[0].href : null } -export function parseTags(text: string) { - const regex = /(?:^|\s)(?:[#][a-z0-9_]+)/gi - const matches = (text.match(regex) || []).map((match) => - match.trim().substring(1).substring(0, MAX_TAG_LENGTH) - ) - const tagSet = new Set() - const uniqueTags: string[] = [] - // 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 taggedText = text - .split(/\s+/) - .map((tag) => (tag.startsWith('#') ? tag : `#${tag}`)) - .join(' ') - return parseTags(taggedText) -} - // TODO: fuzzy matching export const wordIn = (word: string, corpus: string) => corpus.toLocaleLowerCase().includes(word.toLocaleLowerCase()) diff --git a/functions/src/scripts/update-contract-tags.ts b/functions/src/scripts/update-contract-tags.ts deleted file mode 100644 index 37a2b60a..00000000 --- a/functions/src/scripts/update-contract-tags.ts +++ /dev/null @@ -1,46 +0,0 @@ -import * as admin from 'firebase-admin' -import { uniq } from 'lodash' - -import { initAdmin } from './script-init' -initAdmin() - -import { Contract } from '../../../common/contract' -import { parseTags } from '../../../common/util/parse' -import { getValues } from '../utils' - -async function updateContractTags() { - const firestore = admin.firestore() - console.log('Updating contracts tags') - - const contracts = await getValues(firestore.collection('contracts')) - - console.log('Loaded', contracts.length, 'contracts') - - for (const contract of contracts) { - const contractRef = firestore.doc(`contracts/${contract.id}`) - - const tags = uniq([ - ...parseTags(contract.question + contract.description), - ...(contract.tags ?? []), - ]) - const lowercaseTags = tags.map((tag) => tag.toLowerCase()) - - console.log( - 'Updating tags', - contract.slug, - 'from', - contract.tags, - 'to', - tags - ) - - await contractRef.update({ - tags, - lowercaseTags, - } as Partial) - } -} - -if (require.main === module) { - updateContractTags().then(() => process.exit()) -} diff --git a/web/components/contract/contract-description.tsx b/web/components/contract/contract-description.tsx index 53557305..259e39df 100644 --- a/web/components/contract/contract-description.tsx +++ b/web/components/contract/contract-description.tsx @@ -4,7 +4,7 @@ import { useState } from 'react' import Textarea from 'react-expanding-textarea' import { Contract, MAX_DESCRIPTION_LENGTH } from 'common/contract' -import { exhibitExts, parseTags } from 'common/util/parse' +import { exhibitExts } from 'common/util/parse' import { useAdmin } from 'web/hooks/use-admin' import { useUser } from 'web/hooks/use-user' import { updateContract } from 'web/lib/firebase/contracts' @@ -53,17 +53,7 @@ function RichEditContract(props: { contract: Contract; isAdmin?: boolean }) { async function saveDescription() { if (!editor) return - - const tags = parseTags( - `${editor.getText()} ${contract.tags.map((tag) => `#${tag}`).join(' ')}` - ) - const lowercaseTags = tags.map((tag) => tag.toLowerCase()) - - await updateContract(contract.id, { - description: editor.getJSON(), - tags, - lowercaseTags, - }) + await updateContract(contract.id, { description: editor.getJSON() }) } return editing ? ( diff --git a/web/components/linkify.tsx b/web/components/linkify.tsx index a24ab0b6..dffaa88e 100644 --- a/web/components/linkify.tsx +++ b/web/components/linkify.tsx @@ -2,8 +2,7 @@ import clsx from 'clsx' import { Fragment } from 'react' import { SiteLink } from './site-link' -// Return a JSX span, linkifying @username, #hashtags, and https://... -// TODO: Use a markdown parser instead of rolling our own here. +// Return a JSX span, linkifying @username, and https://... export function Linkify(props: { text: string className?: string @@ -16,7 +15,7 @@ export function Linkify(props: { // Find instances of @username, #hashtag, and https://... const regex = - /(?:^|\s)(?:[@#][a-z0-9_]+|https?:\/\/[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_|])/gi + /(?:^|\s)(?:@[a-z0-9_]+|https?:\/\/[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_|])/gi const matches = text.match(regex) || [] const links = matches.map((match) => { // Matches are in the form: " @username" or "https://example.com" @@ -26,7 +25,6 @@ export function Linkify(props: { const href = { '@': `/${tag}`, - '#': `/tag/${tag}`, }[symbol] ?? match.trim() return (