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
.subscribe(async 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 { 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()
export class MarkdownService {
@ -13,52 +31,63 @@ export class MarkdownService {
'list'
]
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 enhancedMarkdownIt: MarkdownIt
private completeMarkdownIt: MarkdownIt
async textMarkdownToHTML (markdown: string) {
if (!markdown) return ''
if (!this.textMarkdownIt) {
this.textMarkdownIt = await this.createMarkdownIt(MarkdownService.TEXT_RULES)
}
const html = this.textMarkdownIt.render(markdown)
return this.avoidTruncatedTags(html)
private markdownParsers: MarkdownParsers = {
textMarkdownIt: null,
enhancedMarkdownIt: null,
enhancedMarkdownWithHTMLIt: null,
completeMarkdownIt: null
}
private parsersConfig: MarkdownParserConfigs = {
textMarkdownIt: { rules: MarkdownService.TEXT_RULES, html: false },
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 }
}
async enhancedMarkdownToHTML (markdown: string) {
if (!markdown) return ''
constructor (private htmlRenderer: HtmlRendererService) {}
if (!this.enhancedMarkdownIt) {
this.enhancedMarkdownIt = await this.createMarkdownIt(MarkdownService.ENHANCED_RULES)
}
textMarkdownToHTML (markdown: string) {
return this.render('textMarkdownIt', markdown)
}
const html = this.enhancedMarkdownIt.render(markdown)
return this.avoidTruncatedTags(html)
async enhancedMarkdownToHTML (markdown: string, withHtml = false) {
if (withHtml) return this.render('enhancedMarkdownWithHTMLIt', markdown)
return this.render('enhancedMarkdownIt', markdown)
}
async completeMarkdownToHTML (markdown: string) {
if (!markdown) return ''
if (!this.completeMarkdownIt) {
this.completeMarkdownIt = await this.createMarkdownIt(MarkdownService.COMPLETE_RULES, true)
}
const html = this.completeMarkdownIt.render(markdown)
return this.avoidTruncatedTags(html)
return this.render('completeMarkdownIt', markdown)
}
private async createMarkdownIt (rules: string[], html = false) {
private async render (name: keyof MarkdownParsers, markdown: string) {
if (!markdown) return ''
const config = this.parsersConfig[ name ]
if (!this.markdownParsers[ name ]) {
this.markdownParsers[ name ] = await this.createMarkdownIt(config)
}
let html = this.markdownParsers[ name ].render(markdown)
html = this.avoidTruncatedTags(html)
console.log(html)
if (config.escape) return this.htmlRenderer.toSafeHtml(html)
return html
}
private async createMarkdownIt (config: MarkdownConfig) {
// 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 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)
}