mirror of
https://github.com/elk-zone/elk.git
synced 2024-11-21 17:05:22 +03:00
fix(editor): mention handling on post editing (#1223)
This commit is contained in:
parent
7389226c38
commit
b049b40298
4 changed files with 20 additions and 1 deletions
|
@ -141,6 +141,12 @@ export function treeToText(input: Node): string {
|
|||
if (['p', 'pre'].includes(input.name))
|
||||
pre = '\n'
|
||||
|
||||
if (input.attributes?.['data-type'] === 'mention') {
|
||||
const acct = input.attributes['data-id']
|
||||
if (acct)
|
||||
return acct.startsWith('@') ? acct : `@${acct}`
|
||||
}
|
||||
|
||||
if (input.name === 'code') {
|
||||
if (input.parent?.name === 'pre') {
|
||||
const lang = input.attributes.class?.replace('language-', '')
|
||||
|
|
|
@ -73,7 +73,7 @@ function handleMention(el: Node) {
|
|||
const matchUser = href.match(UserLinkRE)
|
||||
if (matchUser) {
|
||||
const [, server, username] = matchUser
|
||||
const handle = `@${username}@${server.replace(/(.+\.)(.+\..+)/, '$2')}`
|
||||
const handle = `${username}@${server.replace(/(.+\.)(.+\..+)/, '$2')}`
|
||||
el.attributes.href = `/${server}/@${username}`
|
||||
return h(AccountHoverWrapper, { handle, class: 'inline-block' }, () => nodeToVNode(el))
|
||||
}
|
||||
|
|
|
@ -152,3 +152,8 @@ exports[`content-rich > link + mention 1`] = `
|
|||
</p>
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`editor > transform mentions 1`] = `
|
||||
"
|
||||
@elk Hello"
|
||||
`;
|
||||
|
|
|
@ -168,6 +168,14 @@ describe('content-rich', () => {
|
|||
})
|
||||
})
|
||||
|
||||
describe('editor', () => {
|
||||
it('transform mentions', () => {
|
||||
const ast = parseMastodonHTML('<p><span class="h-card"><a href="https://m.webtoo.ls/@elk" class="u-url mention">@<span>elk</span></a></span> Hello</p>')
|
||||
const transformed = treeToText(ast)
|
||||
expect(transformed).toMatchSnapshot()
|
||||
})
|
||||
})
|
||||
|
||||
async function render(content: string, options?: ContentParseOptions) {
|
||||
const vnode = contentToVNode(content, options)
|
||||
const html = (await renderToString(vnode))
|
||||
|
|
Loading…
Reference in a new issue