From e81d45b4cb6462029661a525315529b02261722d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 15 Feb 2023 11:12:23 +0100 Subject: Fix mention detection --- client/src/app/core/renderer/linkifier.service.ts | 53 +++++++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) (limited to 'client/src') diff --git a/client/src/app/core/renderer/linkifier.service.ts b/client/src/app/core/renderer/linkifier.service.ts index d99591d6c..5c99722f6 100644 --- a/client/src/app/core/renderer/linkifier.service.ts +++ b/client/src/app/core/renderer/linkifier.service.ts @@ -1,13 +1,16 @@ import { Injectable } from '@angular/core' import { getAbsoluteAPIUrl } from '@app/helpers/utils' +import * as LinkifyJS from 'linkifyjs' @Injectable() export class LinkifierService { static CLASSNAME = 'linkified' - private linkifyModule: any + private linkifyModule: typeof LinkifyJS private linkifyHtmlModule: any + private mentionPluginInitialized = false + private linkifyOptions = { className: { mention: LinkifierService.CLASSNAME + '-mention', @@ -24,14 +27,58 @@ export class LinkifierService { if (!this.linkifyModule) { const result = await Promise.all([ import('linkifyjs'), - import('linkify-plugin-mention'), import('linkify-html').then(m => (m as any).default) ]) this.linkifyModule = result[0] - this.linkifyHtmlModule = result[2] + this.linkifyHtmlModule = result[1] + + this.buildMentionPlugin() } return this.linkifyHtmlModule(text, this.linkifyOptions) } + + private buildMentionPlugin () { + if (this.mentionPluginInitialized) return + + const MentionToken = this.linkifyModule.createTokenClass('mention', { + isLink: true, + toHref () { + return '/' + this.toString().slice(1) + } + }) + + this.linkifyModule.registerPlugin('mention', ({ scanner, parser }) => { + const { DOT, HYPHEN, UNDERSCORE, AT } = scanner.tokens + const { domain } = scanner.tokens.groups + + // Start with @ + const At = parser.start.tt(AT) + + // Valid mention (not made up entirely of symbols) + const Mention = At.tt(UNDERSCORE, MentionToken as any) + + At.ta(domain, Mention) + At.tt(UNDERSCORE, Mention) + + // More valid mentions + Mention.ta(domain, Mention) + Mention.tt(HYPHEN, Mention) + Mention.tt(UNDERSCORE, Mention) + + // ADDED: . transitions + const MentionDot = Mention.tt(DOT) + MentionDot.ta(domain, Mention) + MentionDot.tt(HYPHEN, Mention) + MentionDot.tt(UNDERSCORE, Mention) + + const MentionAt = Mention.tt(AT) + MentionAt.ta(domain, Mention) + MentionAt.tt(HYPHEN, Mention) + MentionAt.tt(UNDERSCORE, Mention) + }) + + this.mentionPluginInitialized = true + } } -- cgit v1.2.3