feat: 编辑器支持引用站内帖子

Related #683
This commit is contained in:
WangHe
2025-08-22 19:27:33 +08:00
parent 86126699d3
commit 809a78fee3
2 changed files with 54 additions and 0 deletions

View File

@@ -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',

View File

@@ -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: `<div>${item.text}</div>`,
})) ?? []
)
} else {
return []
}
} catch {
return []
}
},
}
}