Compare commits

..

No commits in common. "79ed07ae741ae0dcaa309cd85930a4626a513ab2" and "1dcb6461c77bc0e5a9f680f78d48850dcaf41afc" have entirely different histories.

4 changed files with 39 additions and 19 deletions

View File

@ -1185,7 +1185,7 @@ export default {
}
const namePattern = /\B@[\w_]+/gi
const refPattern = new RegExp(`${process.env.NEXT_PUBLIC_URL}/items/\\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
@ -1245,6 +1245,8 @@ const createUserMentions = async (item, models) => {
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
try {
const { itemId, commentId } = parseInternalLinks(m)
return Number(commentId || itemId)
@ -1259,6 +1261,7 @@ const createItemMentions = async (item, models) => {
id: { in: refs },
// Don't create mentions for your own items
userId: { not: item.userId }
}
})

View File

@ -23,6 +23,7 @@ import { UNKNOWN_LINK_REL } from '@/lib/constants'
import isEqual from 'lodash/isEqual'
import UserPopover from './user-popover'
import ItemPopover from './item-popover'
import ref from '@/lib/remark-ref2link'
export function SearchText ({ text }) {
return (
@ -294,7 +295,7 @@ export default memo(function Text ({ rel, imgproxyUrls, children, tab, itemId, o
},
img: Img
}}
remarkPlugins={[gfm, mention, sub]}
remarkPlugins={[gfm, mention, sub, ref]}
rehypePlugins={[rehypeInlineCodeProperty]}
>
{children}

26
lib/remark-ref2link.js Normal file
View File

@ -0,0 +1,26 @@
import { findAndReplace } from 'mdast-util-find-and-replace'
const refRegex = /#(\d+(\/(edit|related|ots))?)/gi
export default function ref (options) {
return function transformer (tree) {
findAndReplace(
tree,
[
[refRegex, replaceRef]
],
{ ignore: ['link', 'linkReference'] }
)
}
function replaceRef (value, itemId, match) {
const node = { type: 'text', value }
return {
type: 'link',
title: null,
url: `/items/${itemId}`,
children: [node]
}
}
}

View File

@ -79,23 +79,13 @@ export function parseEmbedUrl (href) {
try {
const { hostname, pathname, searchParams } = new URL(href)
if (hostname.endsWith('youtube.com')) {
if (pathname.includes('/watch')) {
return {
provider: 'youtube',
id: searchParams.get('v'),
meta: {
href,
start: searchParams.get('t')
}
}
}
if (pathname.includes('/shorts')) {
const id = pathname.split('/').slice(-1).join()
return {
provider: 'youtube',
id
if (hostname.endsWith('youtube.com') && pathname.includes('/watch')) {
return {
provider: 'youtube',
id: searchParams.get('v'),
meta: {
href,
start: searchParams.get('t')
}
}
}