From 809a78fee32f43ea5a24af6b8dd7313f4db063c4 Mon Sep 17 00:00:00 2001 From: WangHe <51102@163.com> Date: Fri, 22 Aug 2025 19:27:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E7=BC=96=E8=BE=91=E5=99=A8?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=BC=95=E7=94=A8=E7=AB=99=E5=86=85=E5=B8=96?= =?UTF-8?q?=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related #683 --- frontend_nuxt/utils/vditor.js | 2 + frontend_nuxt/utils/vditorPostCitation.js | 52 +++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 frontend_nuxt/utils/vditorPostCitation.js diff --git a/frontend_nuxt/utils/vditor.js b/frontend_nuxt/utils/vditor.js index c38aed7b1..d2211ff15 100644 --- a/frontend_nuxt/utils/vditor.js +++ b/frontend_nuxt/utils/vditor.js @@ -2,6 +2,7 @@ import Vditor from 'vditor' import { getToken, authState } from './auth' import { searchUsers, fetchFollowings, fetchAdmins } from './user' import { tiebaEmoji } from './tiebaEmoji' +import vditorPostCitation from './vditorPostCitation.js' export function getEditorTheme() { return document.documentElement.dataset.theme === 'dark' ? 'dark' : 'classic' @@ -79,6 +80,7 @@ export function createVditor(editorId, options = {}) { })) }, }, + vditorPostCitation(API_BASE_URL), ], }, cdn: 'https://openisle-1307107697.cos.ap-guangzhou.myqcloud.com/assert/vditor', diff --git a/frontend_nuxt/utils/vditorPostCitation.js b/frontend_nuxt/utils/vditorPostCitation.js new file mode 100644 index 000000000..37b80ecac --- /dev/null +++ b/frontend_nuxt/utils/vditorPostCitation.js @@ -0,0 +1,52 @@ +import { authState, getToken } from '~/utils/auth' + +async function getPost(apiBaseUrl, id) { + return await fetch(`${apiBaseUrl}/api/posts/${id}`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${getToken()}`, + }, + }) +} + +async function searchPost(apiBaseUrl, keyword) { + return await fetch(`${apiBaseUrl}/api/search/global?keyword=${keyword}`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${getToken()}`, + }, + }) +} + +export default (apiBaseUrl) => { + return { + key: '#', + hint: async (keyword) => { + if (!keyword.trim()) return [] + try { + const response = await searchPost(apiBaseUrl, keyword) + if (response.ok) { + const body = await response.json() + let value = '' + return ( + body + ?.filter((item) => item.type === 'comment' || item.type === 'post') + .map((item) => ({ + value: + item.type === 'comment' + ? `[${item.text}](posts/${item.postId}#comment-${item.id})` + : `[${item.text}](posts/${item.id})`, + html: `
${item.text}
`, + })) ?? [] + ) + } else { + return [] + } + } catch { + return [] + } + }, + } +} From 28b33d8c44f46ac8c0418e3f877a9db2ca564faa Mon Sep 17 00:00:00 2001 From: WangHe <51102@163.com> Date: Mon, 25 Aug 2025 11:38:18 +0800 Subject: [PATCH 2/2] =?UTF-8?q?opt:=20=E4=BC=98=E5=8C=96=E4=BB=85=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=96=87=E7=AB=A0=E6=A0=87=E9=A2=98=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend_nuxt/utils/vditorPostCitation.js | 25 +++++------------------ 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/frontend_nuxt/utils/vditorPostCitation.js b/frontend_nuxt/utils/vditorPostCitation.js index 37b80ecac..d3fbcad61 100644 --- a/frontend_nuxt/utils/vditorPostCitation.js +++ b/frontend_nuxt/utils/vditorPostCitation.js @@ -1,17 +1,7 @@ import { authState, getToken } from '~/utils/auth' -async function getPost(apiBaseUrl, id) { - return await fetch(`${apiBaseUrl}/api/posts/${id}`, { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${getToken()}`, - }, - }) -} - async function searchPost(apiBaseUrl, keyword) { - return await fetch(`${apiBaseUrl}/api/search/global?keyword=${keyword}`, { + return await fetch(`${apiBaseUrl}/api/search/posts/title?keyword=${keyword}`, { method: 'GET', headers: { 'Content-Type': 'application/json', @@ -31,15 +21,10 @@ export default (apiBaseUrl) => { const body = await response.json() let value = '' return ( - body - ?.filter((item) => item.type === 'comment' || item.type === 'post') - .map((item) => ({ - value: - item.type === 'comment' - ? `[${item.text}](posts/${item.postId}#comment-${item.id})` - : `[${item.text}](posts/${item.id})`, - html: `
${item.text}
`, - })) ?? [] + body.map((item) => ({ + value: `[${item.title}](/posts/${item.id})`, + html: `
${item.title}
`, + })) ?? [] ) } else { return []