Fix missing commentId parsing for item mentions (#1219)
This commit is contained in:
parent
061d3f220d
commit
d8fe698963
|
@ -1,5 +1,5 @@
|
||||||
import { GraphQLError } from 'graphql'
|
import { GraphQLError } from 'graphql'
|
||||||
import { ensureProtocol, removeTracking, stripTrailingSlash } from '@/lib/url'
|
import { ensureProtocol, parseInternalLinks, removeTracking, stripTrailingSlash } from '@/lib/url'
|
||||||
import serialize from './serial'
|
import serialize from './serial'
|
||||||
import { decodeCursor, LIMIT, nextCursorEncoded } from '@/lib/cursor'
|
import { decodeCursor, LIMIT, nextCursorEncoded } from '@/lib/cursor'
|
||||||
import { getMetadata, metadataRuleSets } from 'page-metadata-parser'
|
import { getMetadata, metadataRuleSets } from 'page-metadata-parser'
|
||||||
|
@ -1185,7 +1185,7 @@ export default {
|
||||||
}
|
}
|
||||||
|
|
||||||
const namePattern = /\B@[\w_]+/gi
|
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) => {
|
export const createMentions = async (item, models) => {
|
||||||
// if we miss a mention, in the rare circumstance there's some kind of
|
// 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 => {
|
const refs = item.text.match(refPattern)?.map(m => {
|
||||||
if (m.startsWith('#')) return Number(m.slice(1))
|
if (m.startsWith('#')) return Number(m.slice(1))
|
||||||
// is not #<id> syntax but full URL
|
// 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
|
if (!refs || refs.length === 0) return
|
||||||
|
|
||||||
const referee = await models.item.findMany({
|
const referee = await models.item.findMany({
|
||||||
|
|
|
@ -213,7 +213,7 @@ export default memo(function Text ({ rel, imgproxyUrls, children, tab, itemId, o
|
||||||
)
|
)
|
||||||
} else if (href.startsWith('/') || url?.origin === internalURL) {
|
} else if (href.startsWith('/') || url?.origin === internalURL) {
|
||||||
try {
|
try {
|
||||||
const linkText = parseInternalLinks(href)
|
const { linkText } = parseInternalLinks(href)
|
||||||
if (linkText) {
|
if (linkText) {
|
||||||
return (
|
return (
|
||||||
<ItemPopover id={linkText.replace('#', '').split('/')[0]}>
|
<ItemPopover id={linkText.replace('#', '').split('/')[0]}>
|
||||||
|
@ -241,7 +241,7 @@ export default memo(function Text ({ rel, imgproxyUrls, children, tab, itemId, o
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const linkText = parseInternalLinks(href)
|
const { linkText } = parseInternalLinks(href)
|
||||||
if (linkText) {
|
if (linkText) {
|
||||||
return (
|
return (
|
||||||
<ItemPopover id={linkText.replace('#', '').split('/')[0]}>
|
<ItemPopover id={linkText.replace('#', '').split('/')[0]}>
|
||||||
|
|
|
@ -52,12 +52,14 @@ export function parseInternalLinks (href) {
|
||||||
// and not #2
|
// and not #2
|
||||||
// since commentId will be ignored anyway
|
// since commentId will be ignored anyway
|
||||||
const linkText = `#${itemId}/${itemPage}`
|
const linkText = `#${itemId}/${itemPage}`
|
||||||
return linkText
|
return { itemId, linkText }
|
||||||
}
|
}
|
||||||
const commentId = searchParams.get('commentId')
|
const commentId = searchParams.get('commentId')
|
||||||
const linkText = `#${commentId || itemId}`
|
const linkText = `#${commentId || itemId}`
|
||||||
return linkText
|
return { itemId, commentId, linkText }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseEmbedUrl (href) {
|
export function parseEmbedUrl (href) {
|
||||||
|
|
|
@ -24,7 +24,7 @@ describe('internal links', () => {
|
||||||
'parses %p as %p',
|
'parses %p as %p',
|
||||||
(href, expected) => {
|
(href, expected) => {
|
||||||
process.env.NEXT_PUBLIC_URL = 'https://stacker.news'
|
process.env.NEXT_PUBLIC_URL = 'https://stacker.news'
|
||||||
const actual = parseInternalLinks(href)
|
const { linkText: actual } = parseInternalLinks(href)
|
||||||
expect(actual).toBe(expected)
|
expect(actual).toBe(expected)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue