X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fmarkdown.ts;h=ebf479b98c1c8208f2dbf3ba3234019a29686ca7;hb=0882c8e6509b2a4ea48f6c48ecb2aa4aa371500a;hp=c8fb31c8c78b30ced2d326520cbe81a0ced20e6f;hpb=84bced652cd72aad852914a4a734c47dd0002fef;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/markdown.ts b/server/helpers/markdown.ts index c8fb31c8c..ebf479b98 100644 --- a/server/helpers/markdown.ts +++ b/server/helpers/markdown.ts @@ -1,4 +1,6 @@ -import { SANITIZE_OPTIONS, TEXT_WITH_HTML_RULES } from '@shared/core-utils' +import { getSanitizeOptions, TEXT_WITH_HTML_RULES } from '@shared/core-utils' + +const sanitizeOptions = getSanitizeOptions() const sanitizeHtml = require('sanitize-html') const markdownItEmoji = require('markdown-it-emoji/light') @@ -9,6 +11,8 @@ markdownIt.enable(TEXT_WITH_HTML_RULES) markdownIt.use(markdownItEmoji) const toSafeHtml = text => { + if (!text) return '' + // Restore line feed const textWithLineFeed = text.replace(//g, '\r\n') @@ -16,11 +20,26 @@ const toSafeHtml = text => { const html = markdownIt.render(textWithLineFeed) // Convert to safe Html - return sanitizeHtml(html, SANITIZE_OPTIONS) + return sanitizeHtml(html, sanitizeOptions) +} + +const mdToPlainText = text => { + if (!text) return '' + + // Convert possible markdown (emojis, emphasis and lists) to html + const html = markdownIt.render(text) + + // Convert to safe Html + const safeHtml = sanitizeHtml(html, sanitizeOptions) + + return safeHtml.replace(/<[^>]+>/g, '') + .replace(/\n$/, '') + .replace(/\n/g, ', ') } // --------------------------------------------------------------------------- export { - toSafeHtml + toSafeHtml, + mdToPlainText }