]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/core/renderer/linkifier.service.ts
Refactoring login component style
[github/Chocobozzz/PeerTube.git] / client / src / app / core / renderer / linkifier.service.ts
index d99591d6c630497cae8bf01d0979a6bfbfa36b1d..5c99722f6e4fbb786dfb543913dfba71f5c47905 100644 (file)
@@ -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
+  }
 }