aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-02-15 11:12:23 +0100
committerChocobozzz <me@florianbigard.com>2023-02-15 11:12:23 +0100
commite81d45b4cb6462029661a525315529b02261722d (patch)
tree6959e27e9ed25ba6ef6b4c33258fa0f9bb04b9f3
parentc9c21fafdb481a4bbb661229f6dc3e2b673ab3c6 (diff)
downloadPeerTube-e81d45b4cb6462029661a525315529b02261722d.tar.gz
PeerTube-e81d45b4cb6462029661a525315529b02261722d.tar.zst
PeerTube-e81d45b4cb6462029661a525315529b02261722d.zip
Fix mention detection
-rw-r--r--client/package.json1
-rw-r--r--client/src/app/core/renderer/linkifier.service.ts53
-rw-r--r--client/yarn.lock5
3 files changed, 50 insertions, 9 deletions
diff --git a/client/package.json b/client/package.json
index 31d9b1e7c..5e4e4845e 100644
--- a/client/package.json
+++ b/client/package.json
@@ -105,7 +105,6 @@
105 "intl-messageformat": "^10.1.0", 105 "intl-messageformat": "^10.1.0",
106 "jschannel": "^1.0.2", 106 "jschannel": "^1.0.2",
107 "linkify-html": "^4.0.2", 107 "linkify-html": "^4.0.2",
108 "linkify-plugin-mention": "^4.0.2",
109 "linkifyjs": "^4.0.2", 108 "linkifyjs": "^4.0.2",
110 "lodash-es": "^4.17.4", 109 "lodash-es": "^4.17.4",
111 "markdown-it": "13.0.1", 110 "markdown-it": "13.0.1",
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 @@
1import { Injectable } from '@angular/core' 1import { Injectable } from '@angular/core'
2import { getAbsoluteAPIUrl } from '@app/helpers/utils' 2import { getAbsoluteAPIUrl } from '@app/helpers/utils'
3import * as LinkifyJS from 'linkifyjs'
3 4
4@Injectable() 5@Injectable()
5export class LinkifierService { 6export class LinkifierService {
6 static CLASSNAME = 'linkified' 7 static CLASSNAME = 'linkified'
7 8
8 private linkifyModule: any 9 private linkifyModule: typeof LinkifyJS
9 private linkifyHtmlModule: any 10 private linkifyHtmlModule: any
10 11
12 private mentionPluginInitialized = false
13
11 private linkifyOptions = { 14 private linkifyOptions = {
12 className: { 15 className: {
13 mention: LinkifierService.CLASSNAME + '-mention', 16 mention: LinkifierService.CLASSNAME + '-mention',
@@ -24,14 +27,58 @@ export class LinkifierService {
24 if (!this.linkifyModule) { 27 if (!this.linkifyModule) {
25 const result = await Promise.all([ 28 const result = await Promise.all([
26 import('linkifyjs'), 29 import('linkifyjs'),
27 import('linkify-plugin-mention'),
28 import('linkify-html').then(m => (m as any).default) 30 import('linkify-html').then(m => (m as any).default)
29 ]) 31 ])
30 32
31 this.linkifyModule = result[0] 33 this.linkifyModule = result[0]
32 this.linkifyHtmlModule = result[2] 34 this.linkifyHtmlModule = result[1]
35
36 this.buildMentionPlugin()
33 } 37 }
34 38
35 return this.linkifyHtmlModule(text, this.linkifyOptions) 39 return this.linkifyHtmlModule(text, this.linkifyOptions)
36 } 40 }
41
42 private buildMentionPlugin () {
43 if (this.mentionPluginInitialized) return
44
45 const MentionToken = this.linkifyModule.createTokenClass('mention', {
46 isLink: true,
47 toHref () {
48 return '/' + this.toString().slice(1)
49 }
50 })
51
52 this.linkifyModule.registerPlugin('mention', ({ scanner, parser }) => {
53 const { DOT, HYPHEN, UNDERSCORE, AT } = scanner.tokens
54 const { domain } = scanner.tokens.groups
55
56 // Start with @
57 const At = parser.start.tt(AT)
58
59 // Valid mention (not made up entirely of symbols)
60 const Mention = At.tt(UNDERSCORE, MentionToken as any)
61
62 At.ta(domain, Mention)
63 At.tt(UNDERSCORE, Mention)
64
65 // More valid mentions
66 Mention.ta(domain, Mention)
67 Mention.tt(HYPHEN, Mention)
68 Mention.tt(UNDERSCORE, Mention)
69
70 // ADDED: . transitions
71 const MentionDot = Mention.tt(DOT)
72 MentionDot.ta(domain, Mention)
73 MentionDot.tt(HYPHEN, Mention)
74 MentionDot.tt(UNDERSCORE, Mention)
75
76 const MentionAt = Mention.tt(AT)
77 MentionAt.ta(domain, Mention)
78 MentionAt.tt(HYPHEN, Mention)
79 MentionAt.tt(UNDERSCORE, Mention)
80 })
81
82 this.mentionPluginInitialized = true
83 }
37} 84}
diff --git a/client/yarn.lock b/client/yarn.lock
index 1799df7b1..62cf2819d 100644
--- a/client/yarn.lock
+++ b/client/yarn.lock
@@ -7393,11 +7393,6 @@ linkify-it@^4.0.1:
7393 dependencies: 7393 dependencies:
7394 uc.micro "^1.0.1" 7394 uc.micro "^1.0.1"
7395 7395
7396linkify-plugin-mention@^4.0.2:
7397 version "4.0.2"
7398 resolved "https://registry.yarnpkg.com/linkify-plugin-mention/-/linkify-plugin-mention-4.0.2.tgz#866e50d4047ea24fc647ebf706b15b0cd30afa63"
7399 integrity sha512-W30NL/uQxZahg6Ao/roZSe8ndXaQ+3UjOwUUtM+dVEOAUH5L6L02eLncH/lZLU3ZBj3VjwfZ+r98gw6Tp/8zEw==
7400
7401linkifyjs@^4.0.2: 7396linkifyjs@^4.0.2:
7402 version "4.0.2" 7397 version "4.0.2"
7403 resolved "https://registry.yarnpkg.com/linkifyjs/-/linkifyjs-4.0.2.tgz#5844ea70f4427004e50036d983b66da50fe13389" 7398 resolved "https://registry.yarnpkg.com/linkifyjs/-/linkifyjs-4.0.2.tgz#5844ea70f4427004e50036d983b66da50fe13389"