Remove tag parsing (#956)

* Remove #tag parsing

* Remove #tag linkifying

* lint
This commit is contained in:
Sinclair Chen 2022-10-03 18:28:21 -07:00 committed by GitHub
parent 375a4e089f
commit c3ffac34a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 6 additions and 103 deletions

View File

@ -12,7 +12,6 @@ import {
visibility, visibility,
} from './contract' } from './contract'
import { User } from './user' import { User } from './user'
import { parseTags, richTextToString } from './util/parse'
import { removeUndefinedProps } from './util/object' import { removeUndefinedProps } from './util/object'
import { JSONContent } from '@tiptap/core' import { JSONContent } from '@tiptap/core'
@ -38,15 +37,6 @@ export function getNewContract(
answers: string[], answers: string[],
visibility: visibility visibility: visibility
) { ) {
const tags = parseTags(
[
question,
richTextToString(description),
...extraTags.map((tag) => `#${tag}`),
].join(' ')
)
const lowercaseTags = tags.map((tag) => tag.toLowerCase())
const propsByOutcomeType = const propsByOutcomeType =
outcomeType === 'BINARY' outcomeType === 'BINARY'
? getBinaryCpmmProps(initialProb, ante) // getBinaryDpmProps(initialProb, ante) ? getBinaryCpmmProps(initialProb, ante) // getBinaryDpmProps(initialProb, ante)
@ -70,8 +60,8 @@ export function getNewContract(
question: question.trim(), question: question.trim(),
description, description,
tags, tags: [],
lowercaseTags, lowercaseTags: [],
visibility, visibility,
isResolved: false, isResolved: false,
createdTime: Date.now(), createdTime: Date.now(),

View File

@ -1,4 +1,3 @@
import { MAX_TAG_LENGTH } from '../contract'
import { generateText, JSONContent } from '@tiptap/core' import { generateText, JSONContent } from '@tiptap/core'
// Tiptap starter extensions // Tiptap starter extensions
import { Blockquote } from '@tiptap/extension-blockquote' import { Blockquote } from '@tiptap/extension-blockquote'
@ -33,34 +32,6 @@ export function getUrl(text: string) {
return results.length ? results[0].href : null 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 // TODO: fuzzy matching
export const wordIn = (word: string, corpus: string) => export const wordIn = (word: string, corpus: string) =>
corpus.toLocaleLowerCase().includes(word.toLocaleLowerCase()) corpus.toLocaleLowerCase().includes(word.toLocaleLowerCase())

View File

@ -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<Contract>(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<Contract>)
}
}
if (require.main === module) {
updateContractTags().then(() => process.exit())
}

View File

@ -4,7 +4,7 @@ import { useState } from 'react'
import Textarea from 'react-expanding-textarea' import Textarea from 'react-expanding-textarea'
import { Contract, MAX_DESCRIPTION_LENGTH } from 'common/contract' 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 { useAdmin } from 'web/hooks/use-admin'
import { useUser } from 'web/hooks/use-user' import { useUser } from 'web/hooks/use-user'
import { updateContract } from 'web/lib/firebase/contracts' import { updateContract } from 'web/lib/firebase/contracts'
@ -53,17 +53,7 @@ function RichEditContract(props: { contract: Contract; isAdmin?: boolean }) {
async function saveDescription() { async function saveDescription() {
if (!editor) return if (!editor) return
await updateContract(contract.id, { description: editor.getJSON() })
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,
})
} }
return editing ? ( return editing ? (

View File

@ -2,8 +2,7 @@ import clsx from 'clsx'
import { Fragment } from 'react' import { Fragment } from 'react'
import { SiteLink } from './site-link' import { SiteLink } from './site-link'
// Return a JSX span, linkifying @username, #hashtags, and https://... // Return a JSX span, linkifying @username, and https://...
// TODO: Use a markdown parser instead of rolling our own here.
export function Linkify(props: { export function Linkify(props: {
text: string text: string
className?: string className?: string
@ -16,7 +15,7 @@ export function Linkify(props: {
// Find instances of @username, #hashtag, and https://... // Find instances of @username, #hashtag, and https://...
const regex = 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 matches = text.match(regex) || []
const links = matches.map((match) => { const links = matches.map((match) => {
// Matches are in the form: " @username" or "https://example.com" // Matches are in the form: " @username" or "https://example.com"
@ -26,7 +25,6 @@ export function Linkify(props: {
const href = const href =
{ {
'@': `/${tag}`, '@': `/${tag}`,
'#': `/tag/${tag}`,
}[symbol] ?? match.trim() }[symbol] ?? match.trim()
return ( return (