aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/core/renderer/linkifier.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/core/renderer/linkifier.service.ts')
-rw-r--r--client/src/app/core/renderer/linkifier.service.ts53
1 files changed, 50 insertions, 3 deletions
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}