]>
Commit | Line | Data |
---|---|---|
c68e2b2d | 1 | import { getDefaultSanitizeOptions, getTextOnlySanitizeOptions, TEXT_WITH_HTML_RULES } from '@shared/core-utils' |
2539932e | 2 | |
c68e2b2d C |
3 | const defaultSanitizeOptions = getDefaultSanitizeOptions() |
4 | const textOnlySanitizeOptions = getTextOnlySanitizeOptions() | |
84bced65 RK |
5 | |
6 | const sanitizeHtml = require('sanitize-html') | |
7 | const markdownItEmoji = require('markdown-it-emoji/light') | |
8 | const MarkdownItClass = require('markdown-it') | |
84bced65 | 9 | |
2f194811 C |
10 | const markdownItForSafeHtml = new MarkdownItClass('default', { linkify: true, breaks: true, html: true }) |
11 | .enable(TEXT_WITH_HTML_RULES) | |
12 | .use(markdownItEmoji) | |
13 | ||
14 | const markdownItForPlainText = new MarkdownItClass('default', { linkify: false, breaks: true, html: false }) | |
15 | .use(markdownItEmoji) | |
16 | .use(plainTextPlugin) | |
84bced65 | 17 | |
228d8e8e | 18 | const toSafeHtml = (text: string) => { |
46460547 C |
19 | if (!text) return '' |
20 | ||
84bced65 RK |
21 | // Restore line feed |
22 | const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n') | |
23 | ||
24 | // Convert possible markdown (emojis, emphasis and lists) to html | |
2f194811 | 25 | const html = markdownItForSafeHtml.render(textWithLineFeed) |
84bced65 RK |
26 | |
27 | // Convert to safe Html | |
c68e2b2d | 28 | return sanitizeHtml(html, defaultSanitizeOptions) |
84bced65 RK |
29 | } |
30 | ||
c68e2b2d | 31 | const mdToOneLinePlainText = (text: string) => { |
46460547 C |
32 | if (!text) return '' |
33 | ||
2f194811 | 34 | markdownItForPlainText.render(text) |
a073c912 RK |
35 | |
36 | // Convert to safe Html | |
2f194811 | 37 | return sanitizeHtml(markdownItForPlainText.plainText, textOnlySanitizeOptions) |
a073c912 RK |
38 | } |
39 | ||
84bced65 RK |
40 | // --------------------------------------------------------------------------- |
41 | ||
42 | export { | |
a073c912 | 43 | toSafeHtml, |
c68e2b2d C |
44 | mdToOneLinePlainText |
45 | } | |
46 | ||
47 | // --------------------------------------------------------------------------- | |
48 | ||
49 | // Thanks: https://github.com/wavesheep/markdown-it-plain-text | |
50 | function plainTextPlugin (markdownIt: any) { | |
c68e2b2d C |
51 | function plainTextRule (state: any) { |
52 | const text = scan(state.tokens) | |
53 | ||
2f194811 | 54 | markdownIt.plainText = text |
c68e2b2d C |
55 | } |
56 | ||
57 | function scan (tokens: any[]) { | |
2f194811 | 58 | let lastSeparator = '' |
c68e2b2d C |
59 | let text = '' |
60 | ||
2f194811 | 61 | function buildSeparator (token: any) { |
c68e2b2d C |
62 | if (token.type === 'list_item_close') { |
63 | lastSeparator = ', ' | |
2f194811 C |
64 | } |
65 | ||
66 | if (token.tag === 'br' || token.type === 'paragraph_close') { | |
c68e2b2d | 67 | lastSeparator = ' ' |
2f194811 C |
68 | } |
69 | } | |
70 | ||
71 | for (const token of tokens) { | |
72 | buildSeparator(token) | |
73 | ||
74 | if (token.type !== 'inline') continue | |
75 | ||
76 | for (const child of token.children) { | |
77 | buildSeparator(child) | |
78 | ||
79 | if (!child.content) continue | |
80 | ||
81 | text += lastSeparator + child.content | |
82 | lastSeparator = '' | |
c68e2b2d C |
83 | } |
84 | } | |
85 | ||
86 | return text | |
87 | } | |
88 | ||
89 | markdownIt.core.ruler.push('plainText', plainTextRule) | |
84bced65 | 90 | } |