Fix missing commentId parsing for item mentions (#1219)

This commit is contained in:
ekzyis 2024-06-03 21:54:42 -05:00 committed by GitHub
parent 061d3f220d
commit d8fe698963
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 9 deletions

View File

@ -1,5 +1,5 @@
import { GraphQLError } from 'graphql'
import { ensureProtocol, removeTracking, stripTrailingSlash } from '@/lib/url'
import { ensureProtocol, parseInternalLinks, removeTracking, stripTrailingSlash } from '@/lib/url'
import serialize from './serial'
import { decodeCursor, LIMIT, nextCursorEncoded } from '@/lib/cursor'
import { getMetadata, metadataRuleSets } from 'page-metadata-parser'
@ -1185,7 +1185,7 @@ export default {
}
const namePattern = /\B@[\w_]+/gi
const refPattern = new RegExp(`(?:#|${process.env.NEXT_PUBLIC_URL}/items/)(?<id>\\d+)`, 'gi')
const refPattern = new RegExp(`(#\\d+|${process.env.NEXT_PUBLIC_URL}/items/\\d+.*)`, 'gi')
export const createMentions = async (item, models) => {
// if we miss a mention, in the rare circumstance there's some kind of
@ -1247,8 +1247,13 @@ const createItemMentions = async (item, models) => {
const refs = item.text.match(refPattern)?.map(m => {
if (m.startsWith('#')) return Number(m.slice(1))
// is not #<id> syntax but full URL
return Number(m.split('/').slice(-1)[0])
})
try {
const { itemId, commentId } = parseInternalLinks(m)
return Number(commentId || itemId)
} catch (err) {
return null
}
}).filter(r => !!r)
if (!refs || refs.length === 0) return
const referee = await models.item.findMany({

View File

@ -213,7 +213,7 @@ export default memo(function Text ({ rel, imgproxyUrls, children, tab, itemId, o
)
} else if (href.startsWith('/') || url?.origin === internalURL) {
try {
const linkText = parseInternalLinks(href)
const { linkText } = parseInternalLinks(href)
if (linkText) {
return (
<ItemPopover id={linkText.replace('#', '').split('/')[0]}>
@ -241,7 +241,7 @@ export default memo(function Text ({ rel, imgproxyUrls, children, tab, itemId, o
}
try {
const linkText = parseInternalLinks(href)
const { linkText } = parseInternalLinks(href)
if (linkText) {
return (
<ItemPopover id={linkText.replace('#', '').split('/')[0]}>

View File

@ -52,12 +52,14 @@ export function parseInternalLinks (href) {
// and not #2
// since commentId will be ignored anyway
const linkText = `#${itemId}/${itemPage}`
return linkText
return { itemId, linkText }
}
const commentId = searchParams.get('commentId')
const linkText = `#${commentId || itemId}`
return linkText
return { itemId, commentId, linkText }
}
return {}
}
export function parseEmbedUrl (href) {

View File

@ -24,7 +24,7 @@ describe('internal links', () => {
'parses %p as %p',
(href, expected) => {
process.env.NEXT_PUBLIC_URL = 'https://stacker.news'
const actual = parseInternalLinks(href)
const { linkText: actual } = parseInternalLinks(href)
expect(actual).toBe(expected)
}
)