From 7e049ea4673f05cbb90b37199bb93713cc0d5611 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 6 Dec 2019 13:57:57 +0100 Subject: Handle HTML in account descriptions --- client/src/app/shared/renderer/markdown.service.ts | 85 +++++++++++++++------- 1 file changed, 57 insertions(+), 28 deletions(-) (limited to 'client/src/app/shared/renderer') diff --git a/client/src/app/shared/renderer/markdown.service.ts b/client/src/app/shared/renderer/markdown.service.ts index 9aec3b4bc..09bbbfb7e 100644 --- a/client/src/app/shared/renderer/markdown.service.ts +++ b/client/src/app/shared/renderer/markdown.service.ts @@ -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 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 } + } - private textMarkdownIt: MarkdownIt - private enhancedMarkdownIt: MarkdownIt - private completeMarkdownIt: MarkdownIt + constructor (private htmlRenderer: HtmlRendererService) {} - async textMarkdownToHTML (markdown: string) { - if (!markdown) return '' + textMarkdownToHTML (markdown: string) { + return this.render('textMarkdownIt', markdown) + } - if (!this.textMarkdownIt) { - this.textMarkdownIt = await this.createMarkdownIt(MarkdownService.TEXT_RULES) - } + async enhancedMarkdownToHTML (markdown: string, withHtml = false) { + if (withHtml) return this.render('enhancedMarkdownWithHTMLIt', markdown) - const html = this.textMarkdownIt.render(markdown) - return this.avoidTruncatedTags(html) + return this.render('enhancedMarkdownIt', markdown) + } + + async completeMarkdownToHTML (markdown: string) { + return this.render('completeMarkdownIt', markdown) } - async enhancedMarkdownToHTML (markdown: string) { + private async render (name: keyof MarkdownParsers, markdown: string) { if (!markdown) return '' - if (!this.enhancedMarkdownIt) { - this.enhancedMarkdownIt = await this.createMarkdownIt(MarkdownService.ENHANCED_RULES) + const config = this.parsersConfig[ name ] + if (!this.markdownParsers[ name ]) { + this.markdownParsers[ name ] = await this.createMarkdownIt(config) } - const html = this.enhancedMarkdownIt.render(markdown) - return this.avoidTruncatedTags(html) - } + let html = this.markdownParsers[ name ].render(markdown) + html = this.avoidTruncatedTags(html) - async completeMarkdownToHTML (markdown: string) { - if (!markdown) return '' + console.log(html) - if (!this.completeMarkdownIt) { - this.completeMarkdownIt = await this.createMarkdownIt(MarkdownService.COMPLETE_RULES, true) - } + if (config.escape) return this.htmlRenderer.toSafeHtml(html) - const html = this.completeMarkdownIt.render(markdown) - return this.avoidTruncatedTags(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) } -- cgit v1.2.3