mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-08-12 22:31:01 +00:00
Add mention link rendering in Markdown
This commit is contained in:
@@ -3,6 +3,30 @@ import hljs from 'highlight.js'
|
|||||||
import 'highlight.js/styles/github.css'
|
import 'highlight.js/styles/github.css'
|
||||||
import { toast } from '../main'
|
import { toast } from '../main'
|
||||||
|
|
||||||
|
function mentionPlugin(md) {
|
||||||
|
const mentionReg = /^@\[([^\]]+)\]/
|
||||||
|
function mention(state, silent) {
|
||||||
|
const pos = state.pos
|
||||||
|
if (state.src.charCodeAt(pos) !== 0x40) return false
|
||||||
|
const match = mentionReg.exec(state.src.slice(pos))
|
||||||
|
if (!match) return false
|
||||||
|
if (!silent) {
|
||||||
|
const tokenOpen = state.push('link_open', 'a', 1)
|
||||||
|
tokenOpen.attrs = [
|
||||||
|
['href', `/users/${match[1]}`],
|
||||||
|
['target', '_blank'],
|
||||||
|
['class', 'mention-link']
|
||||||
|
]
|
||||||
|
const text = state.push('text', '', 0)
|
||||||
|
text.content = `@${match[1]}`
|
||||||
|
state.push('link_close', 'a', -1)
|
||||||
|
}
|
||||||
|
state.pos += match[0].length
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
md.inline.ruler.before('emphasis', 'mention', mention)
|
||||||
|
}
|
||||||
|
|
||||||
const md = new MarkdownIt({
|
const md = new MarkdownIt({
|
||||||
html: false,
|
html: false,
|
||||||
linkify: true,
|
linkify: true,
|
||||||
@@ -18,6 +42,8 @@ const md = new MarkdownIt({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
md.use(mentionPlugin)
|
||||||
|
|
||||||
export function renderMarkdown(text) {
|
export function renderMarkdown(text) {
|
||||||
return md.render(text || '')
|
return md.render(text || '')
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user