Skip to content

Commit

Permalink
Lazy load linkifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobozzz committed Nov 19, 2020
1 parent c4f7fe0 commit b355b39
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
6 changes: 5 additions & 1 deletion client/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@
"@babel/runtime/helpers/possibleConstructorReturn",
"@babel/runtime/helpers/inherits",
"@babel/runtime/helpers/construct",
"@videojs/xhr"
"@videojs/xhr",
"htmlparser2",
"url",
"parse-srcset",
"postcss"
],
"scripts": []
},
Expand Down
8 changes: 5 additions & 3 deletions client/src/app/core/renderer/html-renderer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ export class HtmlRendererService {
}

async toSafeHtml (text: string) {
await this.loadSanitizeHtml()
const [ html ] = await Promise.all([
// Convert possible markdown to html
this.linkifier.linkify(text),

// Convert possible markdown to html
const html = this.linkifier.linkify(text)
this.loadSanitizeHtml()
])

return this.sanitizeHtml(html, SANITIZE_OPTIONS)
}
Expand Down
35 changes: 21 additions & 14 deletions client/src/app/core/renderer/linkifier.service.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
import { Injectable } from '@angular/core'
import { getAbsoluteAPIUrl } from '@app/helpers/utils'
import * as linkify from 'linkifyjs'
import linkifyHtml from 'linkifyjs/html'

@Injectable()
export class LinkifierService {

static CLASSNAME = 'linkified'

private linkifyModule: any
private linkifyHtmlModule: any

private linkifyOptions = {
className: {
mention: LinkifierService.CLASSNAME + '-mention',
url: LinkifierService.CLASSNAME + '-url'
}
}

constructor () {
// Apply plugin
this.mentionWithDomainPlugin(linkify)
}
async linkify (text: string) {
if (!this.linkifyModule) {
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 linkifyHtml(text, this.linkifyOptions)
return this.linkifyHtmlModule(text, this.linkifyOptions)
}

private mentionWithDomainPlugin (linkify: any) {
const TT = linkify.scanner.TOKENS // Text tokens
const { TOKENS: MT, State } = linkify.parser // Multi tokens, state
private mentionWithDomainPlugin () {
const TT = this.linkifyModule.scanner.TOKENS // Text tokens
const { TOKENS: MT, State } = this.linkifyModule.parser // Multi tokens, state
const MultiToken = MT.Base
const S_START = linkify.parser.start
const S_START = this.linkifyModule.parser.start

const TT_AT = TT.AT
const TT_DOMAIN = TT.DOMAIN
Expand All @@ -44,7 +51,7 @@ export class LinkifierService {
this.v = value
}

linkify.inherits(MultiToken, MENTION, {
this.linkifyModule.inherits(MultiToken, MENTION, {
type: 'mentionWithDomain',
isLink: true,
toHref () {
Expand Down

0 comments on commit b355b39

Please sign in to comment.