diff options
author | Chocobozzz <me@florianbigard.com> | 2019-02-15 15:52:18 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-02-15 15:52:18 +0100 |
commit | 41d713446c2152d47943ddb0c841a9e36ca5a9db (patch) | |
tree | 7b22f6f7ea5652107ef503470d2455c4bb087799 /client/src/app/shared/renderer | |
parent | 17036be5bc2f14dc4e66053087e39887599df4de (diff) | |
download | PeerTube-41d713446c2152d47943ddb0c841a9e36ca5a9db.tar.gz PeerTube-41d713446c2152d47943ddb0c841a9e36ca5a9db.tar.zst PeerTube-41d713446c2152d47943ddb0c841a9e36ca5a9db.zip |
Lazy import some modules
Diffstat (limited to 'client/src/app/shared/renderer')
-rw-r--r-- | client/src/app/shared/renderer/html-renderer.service.ts | 6 | ||||
-rw-r--r-- | client/src/app/shared/renderer/linkifier.service.ts | 5 | ||||
-rw-r--r-- | client/src/app/shared/renderer/markdown.service.ts | 32 |
3 files changed, 25 insertions, 18 deletions
diff --git a/client/src/app/shared/renderer/html-renderer.service.ts b/client/src/app/shared/renderer/html-renderer.service.ts index d49df9b6d..28ef51e72 100644 --- a/client/src/app/shared/renderer/html-renderer.service.ts +++ b/client/src/app/shared/renderer/html-renderer.service.ts | |||
@@ -1,6 +1,5 @@ | |||
1 | import { Injectable } from '@angular/core' | 1 | import { Injectable } from '@angular/core' |
2 | import { LinkifierService } from '@app/shared/renderer/linkifier.service' | 2 | import { LinkifierService } from '@app/shared/renderer/linkifier.service' |
3 | import * as sanitizeHtml from 'sanitize-html' | ||
4 | 3 | ||
5 | @Injectable() | 4 | @Injectable() |
6 | export class HtmlRendererService { | 5 | export class HtmlRendererService { |
@@ -9,7 +8,10 @@ export class HtmlRendererService { | |||
9 | 8 | ||
10 | } | 9 | } |
11 | 10 | ||
12 | toSafeHtml (text: string) { | 11 | async toSafeHtml (text: string) { |
12 | // FIXME: import('..') returns a struct module, containing a "default" field corresponding to our sanitizeHtml function | ||
13 | const sanitizeHtml: typeof import ('sanitize-html') = (await import('sanitize-html') as any).default | ||
14 | |||
13 | // Convert possible markdown to html | 15 | // Convert possible markdown to html |
14 | const html = this.linkifier.linkify(text) | 16 | const html = this.linkifier.linkify(text) |
15 | 17 | ||
diff --git a/client/src/app/shared/renderer/linkifier.service.ts b/client/src/app/shared/renderer/linkifier.service.ts index 2529c9eaf..95d5f17cc 100644 --- a/client/src/app/shared/renderer/linkifier.service.ts +++ b/client/src/app/shared/renderer/linkifier.service.ts | |||
@@ -1,8 +1,7 @@ | |||
1 | import { Injectable } from '@angular/core' | 1 | import { Injectable } from '@angular/core' |
2 | import { getAbsoluteAPIUrl } from '@app/shared/misc/utils' | 2 | import { getAbsoluteAPIUrl } from '@app/shared/misc/utils' |
3 | // FIXME: use @types/linkify when https://github.com/DefinitelyTyped/DefinitelyTyped/pull/29682/files is merged? | 3 | import * as linkify from 'linkifyjs' |
4 | const linkify = require('linkifyjs') | 4 | import linkifyHtml from 'linkifyjs/html' |
5 | const linkifyHtml = require('linkifyjs/html') | ||
6 | 5 | ||
7 | @Injectable() | 6 | @Injectable() |
8 | export class LinkifierService { | 7 | export class LinkifierService { |
diff --git a/client/src/app/shared/renderer/markdown.service.ts b/client/src/app/shared/renderer/markdown.service.ts index 07017eca5..69dc60aaf 100644 --- a/client/src/app/shared/renderer/markdown.service.ts +++ b/client/src/app/shared/renderer/markdown.service.ts | |||
@@ -1,6 +1,6 @@ | |||
1 | import { Injectable } from '@angular/core' | 1 | import { Injectable } from '@angular/core' |
2 | 2 | ||
3 | import * as MarkdownIt from 'markdown-it' | 3 | import { MarkdownIt } from 'markdown-it' |
4 | 4 | ||
5 | @Injectable() | 5 | @Injectable() |
6 | export class MarkdownService { | 6 | export class MarkdownService { |
@@ -14,30 +14,36 @@ export class MarkdownService { | |||
14 | ] | 14 | ] |
15 | static ENHANCED_RULES = MarkdownService.TEXT_RULES.concat([ 'image' ]) | 15 | static ENHANCED_RULES = MarkdownService.TEXT_RULES.concat([ 'image' ]) |
16 | 16 | ||
17 | private textMarkdownIt: MarkdownIt.MarkdownIt | 17 | private textMarkdownIt: MarkdownIt |
18 | private enhancedMarkdownIt: MarkdownIt.MarkdownIt | 18 | private enhancedMarkdownIt: MarkdownIt |
19 | 19 | ||
20 | constructor () { | 20 | async textMarkdownToHTML (markdown: string) { |
21 | this.textMarkdownIt = this.createMarkdownIt(MarkdownService.TEXT_RULES) | ||
22 | this.enhancedMarkdownIt = this.createMarkdownIt(MarkdownService.ENHANCED_RULES) | ||
23 | } | ||
24 | |||
25 | textMarkdownToHTML (markdown: string) { | ||
26 | if (!markdown) return '' | 21 | if (!markdown) return '' |
27 | 22 | ||
23 | if (!this.textMarkdownIt) { | ||
24 | this.textMarkdownIt = await this.createMarkdownIt(MarkdownService.TEXT_RULES) | ||
25 | } | ||
26 | |||
28 | const html = this.textMarkdownIt.render(markdown) | 27 | const html = this.textMarkdownIt.render(markdown) |
29 | return this.avoidTruncatedTags(html) | 28 | return this.avoidTruncatedTags(html) |
30 | } | 29 | } |
31 | 30 | ||
32 | enhancedMarkdownToHTML (markdown: string) { | 31 | async enhancedMarkdownToHTML (markdown: string) { |
33 | if (!markdown) return '' | 32 | if (!markdown) return '' |
34 | 33 | ||
34 | if (!this.enhancedMarkdownIt) { | ||
35 | this.enhancedMarkdownIt = await this.createMarkdownIt(MarkdownService.ENHANCED_RULES) | ||
36 | } | ||
37 | |||
35 | const html = this.enhancedMarkdownIt.render(markdown) | 38 | const html = this.enhancedMarkdownIt.render(markdown) |
36 | return this.avoidTruncatedTags(html) | 39 | return this.avoidTruncatedTags(html) |
37 | } | 40 | } |
38 | 41 | ||
39 | private createMarkdownIt (rules: string[]) { | 42 | private async createMarkdownIt (rules: string[]) { |
40 | const markdownIt = new MarkdownIt('zero', { linkify: true, breaks: true }) | 43 | // FIXME: import('..') returns a struct module, containing a "default" field corresponding to our sanitizeHtml function |
44 | const MarkdownItClass: typeof import ('markdown-it') = (await import('markdown-it') as any).default | ||
45 | |||
46 | const markdownIt = new MarkdownItClass('zero', { linkify: true, breaks: true }) | ||
41 | 47 | ||
42 | for (let rule of rules) { | 48 | for (let rule of rules) { |
43 | markdownIt.enable(rule) | 49 | markdownIt.enable(rule) |
@@ -48,7 +54,7 @@ export class MarkdownService { | |||
48 | return markdownIt | 54 | return markdownIt |
49 | } | 55 | } |
50 | 56 | ||
51 | private setTargetToLinks (markdownIt: MarkdownIt.MarkdownIt) { | 57 | private setTargetToLinks (markdownIt: MarkdownIt) { |
52 | // Snippet from markdown-it documentation: https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer | 58 | // Snippet from markdown-it documentation: https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer |
53 | const defaultRender = markdownIt.renderer.rules.link_open || function (tokens, idx, options, env, self) { | 59 | const defaultRender = markdownIt.renderer.rules.link_open || function (tokens, idx, options, env, self) { |
54 | return self.renderToken(tokens, idx, options) | 60 | return self.renderToken(tokens, idx, options) |