Handle HTML in account descriptions

pull/2302/head
Chocobozzz 2019-12-06 13:57:57 +01:00
parent d8232bac6a
commit 7e049ea467
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 63 additions and 34 deletions

View File

@ -27,7 +27,7 @@ export class AccountAboutComponent implements OnInit, OnDestroy {
this.accountSub = this.accountService.accountLoaded this.accountSub = this.accountService.accountLoaded
.subscribe(async account => { .subscribe(async account => {
this.account = account this.account = account
this.descriptionHTML = await this.markdownService.textMarkdownToHTML(this.account.description) this.descriptionHTML = await this.markdownService.enhancedMarkdownToHTML(this.account.description, true)
}) })
} }

View File

@ -1,6 +1,24 @@
import { Injectable } from '@angular/core' import { Injectable } from '@angular/core'
import { MarkdownIt } from 'markdown-it' import { MarkdownIt } from 'markdown-it'
import { HtmlRendererService } from '@app/shared/renderer/html-renderer.service'
import { mark } from '@angular/compiler-cli/src/ngtsc/perf/src/clock'
type MarkdownParsers = {
textMarkdownIt: MarkdownIt
enhancedMarkdownIt: MarkdownIt
enhancedMarkdownWithHTMLIt: MarkdownIt
completeMarkdownIt: MarkdownIt
}
type MarkdownConfig = {
rules: string[]
html: boolean
escape?: boolean
}
type MarkdownParserConfigs = {
[id in keyof MarkdownParsers]: MarkdownConfig
}
@Injectable() @Injectable()
export class MarkdownService { export class MarkdownService {
@ -13,52 +31,63 @@ export class MarkdownService {
'list' 'list'
] ]
static ENHANCED_RULES = MarkdownService.TEXT_RULES.concat([ 'image' ]) static ENHANCED_RULES = MarkdownService.TEXT_RULES.concat([ 'image' ])
static COMPLETE_RULES = MarkdownService.ENHANCED_RULES.concat([ 'block', 'inline', 'heading', 'html_inline', 'html_block', 'paragraph' ]) static ENHANCED_WITH_HTML_RULES = MarkdownService.ENHANCED_RULES.concat([ 'html_inline', 'html_block' ])
static COMPLETE_RULES = MarkdownService.ENHANCED_WITH_HTML_RULES.concat([ 'block', 'inline', 'heading', 'paragraph' ])
private textMarkdownIt: MarkdownIt private markdownParsers: MarkdownParsers = {
private enhancedMarkdownIt: MarkdownIt textMarkdownIt: null,
private completeMarkdownIt: MarkdownIt enhancedMarkdownIt: null,
enhancedMarkdownWithHTMLIt: null,
async textMarkdownToHTML (markdown: string) { completeMarkdownIt: null
if (!markdown) return '' }
private parsersConfig: MarkdownParserConfigs = {
if (!this.textMarkdownIt) { textMarkdownIt: { rules: MarkdownService.TEXT_RULES, html: false },
this.textMarkdownIt = await this.createMarkdownIt(MarkdownService.TEXT_RULES) enhancedMarkdownIt: { rules: MarkdownService.ENHANCED_RULES, html: false },
enhancedMarkdownWithHTMLIt: { rules: MarkdownService.ENHANCED_WITH_HTML_RULES, html: true, escape: true },
completeMarkdownIt: { rules: MarkdownService.COMPLETE_RULES, html: true }
} }
const html = this.textMarkdownIt.render(markdown) constructor (private htmlRenderer: HtmlRendererService) {}
return this.avoidTruncatedTags(html)
textMarkdownToHTML (markdown: string) {
return this.render('textMarkdownIt', markdown)
} }
async enhancedMarkdownToHTML (markdown: string) { async enhancedMarkdownToHTML (markdown: string, withHtml = false) {
if (!markdown) return '' if (withHtml) return this.render('enhancedMarkdownWithHTMLIt', markdown)
if (!this.enhancedMarkdownIt) { return this.render('enhancedMarkdownIt', markdown)
this.enhancedMarkdownIt = await this.createMarkdownIt(MarkdownService.ENHANCED_RULES)
}
const html = this.enhancedMarkdownIt.render(markdown)
return this.avoidTruncatedTags(html)
} }
async completeMarkdownToHTML (markdown: string) { async completeMarkdownToHTML (markdown: string) {
return this.render('completeMarkdownIt', markdown)
}
private async render (name: keyof MarkdownParsers, markdown: string) {
if (!markdown) return '' if (!markdown) return ''
if (!this.completeMarkdownIt) { const config = this.parsersConfig[ name ]
this.completeMarkdownIt = await this.createMarkdownIt(MarkdownService.COMPLETE_RULES, true) if (!this.markdownParsers[ name ]) {
this.markdownParsers[ name ] = await this.createMarkdownIt(config)
} }
const html = this.completeMarkdownIt.render(markdown) let html = this.markdownParsers[ name ].render(markdown)
return this.avoidTruncatedTags(html) html = this.avoidTruncatedTags(html)
console.log(html)
if (config.escape) return this.htmlRenderer.toSafeHtml(html)
return html
} }
private async createMarkdownIt (rules: string[], html = false) { private async createMarkdownIt (config: MarkdownConfig) {
// FIXME: import('...') returns a struct module, containing a "default" field corresponding to our sanitizeHtml function // FIXME: import('...') returns a struct module, containing a "default" field corresponding to our sanitizeHtml function
const MarkdownItClass: typeof import ('markdown-it') = (await import('markdown-it') as any).default const MarkdownItClass: typeof import ('markdown-it') = (await import('markdown-it') as any).default
const markdownIt = new MarkdownItClass('zero', { linkify: true, breaks: true, html }) const markdownIt = new MarkdownItClass('zero', { linkify: true, breaks: true, html: config.html })
for (const rule of rules) { for (const rule of config.rules) {
markdownIt.enable(rule) markdownIt.enable(rule)
} }