ActivityPub-federated video streaming platform using P2P directly in your web browser
Handle HTML in account descriptions
Chocobozzz7e049ea4673f
diff --git a/client/src/app/+accounts/account-about/account-about.component.ts b/client/src/app/+accounts/account-about/account-about.component.tsindex ce22d3c2eb..c65bad3f29 100644--- a/client/src/app/+accounts/account-about/account-about.component.ts+++ b/client/src/app/+accounts/account-about/account-about.component.ts@@ -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)})}diff --git a/client/src/app/shared/renderer/markdown.service.ts b/client/src/app/shared/renderer/markdown.service.tsindex 9aec3b4bc8..09bbbfb7ef 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 functionconst 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)}