Handle HTML in account descriptions
This commit is contained in:
parent
d8232bac6a
commit
7e049ea467
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
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 }
|
||||
}
|
||||
|
||||
const html = this.textMarkdownIt.render(markdown)
|
||||
return this.avoidTruncatedTags(html)
|
||||
constructor (private htmlRenderer: HtmlRendererService) {}
|
||||
|
||||
textMarkdownToHTML (markdown: string) {
|
||||
return this.render('textMarkdownIt', markdown)
|
||||
}
|
||||
|
||||
async enhancedMarkdownToHTML (markdown: string) {
|
||||
if (!markdown) return ''
|
||||
async enhancedMarkdownToHTML (markdown: string, withHtml = false) {
|
||||
if (withHtml) return this.render('enhancedMarkdownWithHTMLIt', markdown)
|
||||
|
||||
if (!this.enhancedMarkdownIt) {
|
||||
this.enhancedMarkdownIt = await this.createMarkdownIt(MarkdownService.ENHANCED_RULES)
|
||||
}
|
||||
|
||||
const html = this.enhancedMarkdownIt.render(markdown)
|
||||
return this.avoidTruncatedTags(html)
|
||||
return this.render('enhancedMarkdownIt', markdown)
|
||||
}
|
||||
|
||||
async completeMarkdownToHTML (markdown: string) {
|
||||
return this.render('completeMarkdownIt', markdown)
|
||||
}
|
||||
|
||||
private async render (name: keyof MarkdownParsers, markdown: string) {
|
||||
if (!markdown) return ''
|
||||
|
||||
if (!this.completeMarkdownIt) {
|
||||
this.completeMarkdownIt = await this.createMarkdownIt(MarkdownService.COMPLETE_RULES, true)
|
||||
const config = this.parsersConfig[ name ]
|
||||
if (!this.markdownParsers[ name ]) {
|
||||
this.markdownParsers[ name ] = await this.createMarkdownIt(config)
|
||||
}
|
||||
|
||||
const html = this.completeMarkdownIt.render(markdown)
|
||||
return this.avoidTruncatedTags(html)
|
||||
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 (rules: string[], html = false) {
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue