X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fmarkdown.ts;h=a20ac22d4406b1e04be4c7774a26a8c1acd03310;hb=d3fcf1c57ab898e18654910e880875a911fbd128;hp=2126bb7526d00054cc5131e5aa56e4f069f2f87b;hpb=4024c44f9027a32809931de0692d40d001df721c;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/markdown.ts b/server/helpers/markdown.ts index 2126bb752..a20ac22d4 100644 --- a/server/helpers/markdown.ts +++ b/server/helpers/markdown.ts @@ -1,43 +1,90 @@ -import { SANITIZE_OPTIONS, TEXT_WITH_HTML_RULES } from '@shared/core-utils' +import { getDefaultSanitizeOptions, getTextOnlySanitizeOptions, TEXT_WITH_HTML_RULES } from '@shared/core-utils' + +const defaultSanitizeOptions = getDefaultSanitizeOptions() +const textOnlySanitizeOptions = getTextOnlySanitizeOptions() 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 markdownItForSafeHtml = new MarkdownItClass('default', { linkify: true, breaks: true, html: true }) + .enable(TEXT_WITH_HTML_RULES) + .use(markdownItEmoji) + +const markdownItForPlainText = new MarkdownItClass('default', { linkify: false, breaks: true, html: false }) + .use(markdownItEmoji) + .use(plainTextPlugin) -const toSafeHtml = text => { +const toSafeHtml = (text: string) => { if (!text) return '' // Restore line feed const textWithLineFeed = text.replace(//g, '\r\n') // Convert possible markdown (emojis, emphasis and lists) to html - const html = markdownIt.render(textWithLineFeed) + const html = markdownItForSafeHtml.render(textWithLineFeed) // Convert to safe Html - return sanitizeHtml(html, SANITIZE_OPTIONS) + return sanitizeHtml(html, defaultSanitizeOptions) } -const mdToPlainText = text => { +const mdToOneLinePlainText = (text: string) => { if (!text) return '' - // Convert possible markdown (emojis, emphasis and lists) to html - const html = markdownIt.render(text) + markdownItForPlainText.render(text) // Convert to safe Html - const safeHtml = sanitizeHtml(html, SANITIZE_OPTIONS) - - return safeHtml.replace(/<[^>]+>/g, '') - .replace(/\n$/, '') - .replace('\n', ', ') + return sanitizeHtml(markdownItForPlainText.plainText, textOnlySanitizeOptions) } // --------------------------------------------------------------------------- export { toSafeHtml, - mdToPlainText + mdToOneLinePlainText +} + +// --------------------------------------------------------------------------- + +// Thanks: https://github.com/wavesheep/markdown-it-plain-text +function plainTextPlugin (markdownIt: any) { + function plainTextRule (state: any) { + const text = scan(state.tokens) + + markdownIt.plainText = text + } + + function scan (tokens: any[]) { + let lastSeparator = '' + let text = '' + + function buildSeparator (token: any) { + if (token.type === 'list_item_close') { + lastSeparator = ', ' + } + + if (token.tag === 'br' || token.type === 'paragraph_close') { + lastSeparator = ' ' + } + } + + for (const token of tokens) { + buildSeparator(token) + + if (token.type !== 'inline') continue + + for (const child of token.children) { + buildSeparator(child) + + if (!child.content) continue + + text += lastSeparator + child.content + lastSeparator = '' + } + } + + return text + } + + markdownIt.core.ruler.push('plainText', plainTextRule) }