X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fcore%2Frenderer%2Fmarkdown.service.ts;h=36258ca9872f6f1a471b9ac83c8b57f5341a0148;hb=15a7eafb892441957ba7dd6fcbf556086fe5b2b3;hp=0c43bebabb2daaa6fcf6c44e5245f3e013946463;hpb=67ed6552b831df66713bac9e672738796128d33f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/core/renderer/markdown.service.ts b/client/src/app/core/renderer/markdown.service.ts index 0c43bebab..36258ca98 100644 --- a/client/src/app/core/renderer/markdown.service.ts +++ b/client/src/app/core/renderer/markdown.service.ts @@ -1,6 +1,13 @@ import * as MarkdownIt from 'markdown-it' -import { buildVideoLink } from 'src/assets/player/utils' import { Injectable } from '@angular/core' +import { buildVideoLink, decorateVideoLink } from '@shared/core-utils' +import { + COMPLETE_RULES, + ENHANCED_RULES, + ENHANCED_WITH_HTML_RULES, + TEXT_RULES, + TEXT_WITH_HTML_RULES +} from '@shared/core-utils/renderer/markdown' import { HtmlRendererService } from './html-renderer.service' type MarkdownParsers = { @@ -10,12 +17,15 @@ type MarkdownParsers = { enhancedMarkdownIt: MarkdownIt enhancedWithHTMLMarkdownIt: MarkdownIt - completeMarkdownIt: MarkdownIt + unsafeMarkdownIt: MarkdownIt + + customPageMarkdownIt: MarkdownIt } type MarkdownConfig = { rules: string[] html: boolean + breaks: boolean escape?: boolean } @@ -25,76 +35,91 @@ type MarkdownParserConfigs = { @Injectable() export class MarkdownService { - static TEXT_RULES = [ - 'linkify', - 'autolink', - 'emphasis', - 'link', - 'newline', - 'list' - ] - static TEXT_WITH_HTML_RULES = MarkdownService.TEXT_RULES.concat([ 'html_inline', 'html_block' ]) - - static ENHANCED_RULES = MarkdownService.TEXT_RULES.concat([ 'image' ]) - static ENHANCED_WITH_HTML_RULES = MarkdownService.TEXT_WITH_HTML_RULES.concat([ 'image' ]) - - static COMPLETE_RULES = MarkdownService.ENHANCED_WITH_HTML_RULES.concat([ 'block', 'inline', 'heading', 'paragraph' ]) - private markdownParsers: MarkdownParsers = { textMarkdownIt: null, textWithHTMLMarkdownIt: null, + enhancedMarkdownIt: null, enhancedWithHTMLMarkdownIt: null, - completeMarkdownIt: null + + unsafeMarkdownIt: null, + + customPageMarkdownIt: null } private parsersConfig: MarkdownParserConfigs = { - textMarkdownIt: { rules: MarkdownService.TEXT_RULES, html: false }, - textWithHTMLMarkdownIt: { rules: MarkdownService.TEXT_WITH_HTML_RULES, html: true, escape: true }, + textMarkdownIt: { rules: TEXT_RULES, breaks: true, html: false }, + textWithHTMLMarkdownIt: { rules: TEXT_WITH_HTML_RULES, breaks: true, html: true, escape: true }, - enhancedMarkdownIt: { rules: MarkdownService.ENHANCED_RULES, html: false }, - enhancedWithHTMLMarkdownIt: { rules: MarkdownService.ENHANCED_WITH_HTML_RULES, html: true, escape: true }, + enhancedMarkdownIt: { rules: ENHANCED_RULES, breaks: true, html: false }, + enhancedWithHTMLMarkdownIt: { rules: ENHANCED_WITH_HTML_RULES, breaks: true, html: true, escape: true }, - completeMarkdownIt: { rules: MarkdownService.COMPLETE_RULES, html: true } + unsafeMarkdownIt: { rules: COMPLETE_RULES, breaks: true, html: true, escape: false }, + + customPageMarkdownIt: { rules: COMPLETE_RULES, breaks: false, html: true, escape: true } } + private emojiModule: any + constructor (private htmlRenderer: HtmlRendererService) {} - textMarkdownToHTML (markdown: string, withHtml = false) { - if (withHtml) return this.render('textWithHTMLMarkdownIt', markdown) + textMarkdownToHTML (markdown: string, withHtml = false, withEmoji = false) { + if (withHtml) return this.render({ name: 'textWithHTMLMarkdownIt', markdown, withEmoji }) - return this.render('textMarkdownIt', markdown) + return this.render({ name: 'textMarkdownIt', markdown, withEmoji }) } - enhancedMarkdownToHTML (markdown: string, withHtml = false) { - if (withHtml) return this.render('enhancedWithHTMLMarkdownIt', markdown) + enhancedMarkdownToHTML (markdown: string, withHtml = false, withEmoji = false) { + if (withHtml) return this.render({ name: 'enhancedWithHTMLMarkdownIt', markdown, withEmoji }) + + return this.render({ name: 'enhancedMarkdownIt', markdown, withEmoji }) + } - return this.render('enhancedMarkdownIt', markdown) + unsafeMarkdownToHTML (markdown: string, _trustedInput: true) { + return this.render({ name: 'unsafeMarkdownIt', markdown, withEmoji: true }) } - completeMarkdownToHTML (markdown: string) { - return this.render('completeMarkdownIt', markdown) + customPageMarkdownToHTML (markdown: string, additionalAllowedTags: string[]) { + return this.render({ name: 'customPageMarkdownIt', markdown, withEmoji: true, additionalAllowedTags }) } - async processVideoTimestamps (html: string) { + processVideoTimestamps (videoShortUUID: string, html: string) { return html.replace(/((\d{1,2}):)?(\d{1,2}):(\d{1,2})/g, function (str, _, h, m, s) { const t = (3600 * +(h || 0)) + (60 * +(m || 0)) + (+(s || 0)) - const url = buildVideoLink({ startTime: t }) + + const url = decorateVideoLink({ + url: buildVideoLink({ shortUUID: videoShortUUID }), + startTime: t + }) return `${str}` }) } - private async render (name: keyof MarkdownParsers, markdown: string) { + private async render (options: { + name: keyof MarkdownParsers + markdown: string + withEmoji: boolean + additionalAllowedTags?: string[] + }) { + const { name, markdown, withEmoji, additionalAllowedTags } = options if (!markdown) return '' const config = this.parsersConfig[ name ] if (!this.markdownParsers[ name ]) { this.markdownParsers[ name ] = await this.createMarkdownIt(config) + + if (withEmoji) { + if (!this.emojiModule) { + this.emojiModule = (await import('markdown-it-emoji/light')).default + } + + this.markdownParsers[ name ].use(this.emojiModule) + } } let html = this.markdownParsers[ name ].render(markdown) html = this.avoidTruncatedTags(html) - if (config.escape) return this.htmlRenderer.toSafeHtml(html) + if (config.escape) return this.htmlRenderer.toSafeHtml(html, additionalAllowedTags) return html } @@ -103,7 +128,7 @@ export class MarkdownService { // FIXME: import('...') returns a struct module, containing a "default" field const MarkdownItClass: typeof import ('markdown-it') = (await import('markdown-it') as any).default - const markdownIt = new MarkdownItClass('zero', { linkify: true, breaks: true, html: config.html }) + const markdownIt = new MarkdownItClass('zero', { linkify: true, breaks: config.breaks, html: config.html }) for (const rule of config.rules) { markdownIt.enable(rule)