Lazy load linkifier

This commit is contained in:
Chocobozzz 2020-11-19 16:23:19 +01:00
parent c4f7fe09cd
commit b355b39408
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 32 additions and 19 deletions

View File

@ -168,7 +168,11 @@
"@babel/runtime/helpers/possibleConstructorReturn", "@babel/runtime/helpers/possibleConstructorReturn",
"@babel/runtime/helpers/inherits", "@babel/runtime/helpers/inherits",
"@babel/runtime/helpers/construct", "@babel/runtime/helpers/construct",
"@videojs/xhr" "@videojs/xhr",
"htmlparser2",
"url",
"parse-srcset",
"postcss"
], ],
"scripts": [] "scripts": []
}, },

View File

@ -21,10 +21,12 @@ export class HtmlRendererService {
} }
async toSafeHtml (text: string) { async toSafeHtml (text: string) {
await this.loadSanitizeHtml() const [ html ] = await Promise.all([
// Convert possible markdown to html // Convert possible markdown to html
const html = this.linkifier.linkify(text) this.linkifier.linkify(text),
this.loadSanitizeHtml()
])
return this.sanitizeHtml(html, SANITIZE_OPTIONS) return this.sanitizeHtml(html, SANITIZE_OPTIONS)
} }

View File

@ -1,13 +1,13 @@
import { Injectable } from '@angular/core' import { Injectable } from '@angular/core'
import { getAbsoluteAPIUrl } from '@app/helpers/utils' import { getAbsoluteAPIUrl } from '@app/helpers/utils'
import * as linkify from 'linkifyjs'
import linkifyHtml from 'linkifyjs/html'
@Injectable() @Injectable()
export class LinkifierService { export class LinkifierService {
static CLASSNAME = 'linkified' static CLASSNAME = 'linkified'
private linkifyModule: any
private linkifyHtmlModule: any
private linkifyOptions = { private linkifyOptions = {
className: { className: {
mention: LinkifierService.CLASSNAME + '-mention', mention: LinkifierService.CLASSNAME + '-mention',
@ -15,20 +15,27 @@ export class LinkifierService {
} }
} }
constructor () { async linkify (text: string) {
// Apply plugin if (!this.linkifyModule) {
this.mentionWithDomainPlugin(linkify) const result = await Promise.all([
import('linkifyjs'), // ES module
import('linkifyjs/html').then(m => m.default)
])
this.linkifyModule = result[0]
this.linkifyHtmlModule = result[1]
this.mentionWithDomainPlugin()
} }
linkify (text: string) { return this.linkifyHtmlModule(text, this.linkifyOptions)
return linkifyHtml(text, this.linkifyOptions)
} }
private mentionWithDomainPlugin (linkify: any) { private mentionWithDomainPlugin () {
const TT = linkify.scanner.TOKENS // Text tokens const TT = this.linkifyModule.scanner.TOKENS // Text tokens
const { TOKENS: MT, State } = linkify.parser // Multi tokens, state const { TOKENS: MT, State } = this.linkifyModule.parser // Multi tokens, state
const MultiToken = MT.Base const MultiToken = MT.Base
const S_START = linkify.parser.start const S_START = this.linkifyModule.parser.start
const TT_AT = TT.AT const TT_AT = TT.AT
const TT_DOMAIN = TT.DOMAIN const TT_DOMAIN = TT.DOMAIN
@ -44,7 +51,7 @@ export class LinkifierService {
this.v = value this.v = value
} }
linkify.inherits(MultiToken, MENTION, { this.linkifyModule.inherits(MultiToken, MENTION, {
type: 'mentionWithDomain', type: 'mentionWithDomain',
isLink: true, isLink: true,
toHref () { toHref () {