From 84bced652cd72aad852914a4a734c47dd0002fef Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Sun, 11 Apr 2021 15:06:36 +0200 Subject: render markdown and plainify descriptions on previews --- server/helpers/markdown.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 server/helpers/markdown.ts (limited to 'server/helpers/markdown.ts') diff --git a/server/helpers/markdown.ts b/server/helpers/markdown.ts new file mode 100644 index 000000000..c8fb31c8c --- /dev/null +++ b/server/helpers/markdown.ts @@ -0,0 +1,26 @@ +import { SANITIZE_OPTIONS, TEXT_WITH_HTML_RULES } from '@shared/core-utils' + +const sanitizeHtml = require('sanitize-html') +const markdownItEmoji = require('markdown-it-emoji/light') +const MarkdownItClass = require('markdown-it') +const markdownIt = new MarkdownItClass('default', { linkify: true, breaks: true, html: true }) + +markdownIt.enable(TEXT_WITH_HTML_RULES) +markdownIt.use(markdownItEmoji) + +const toSafeHtml = text => { + // Restore line feed + const textWithLineFeed = text.replace(//g, '\r\n') + + // Convert possible markdown (emojis, emphasis and lists) to html + const html = markdownIt.render(textWithLineFeed) + + // Convert to safe Html + return sanitizeHtml(html, SANITIZE_OPTIONS) +} + +// --------------------------------------------------------------------------- + +export { + toSafeHtml +} -- cgit v1.2.3