From 9ff36c2d70956d2775d207c7809adb6fe7f2f2a5 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 17 Nov 2020 14:34:09 +0100 Subject: Refactor markdown/sanitize html code --- shared/core-utils/index.ts | 4 ++++ shared/core-utils/renderer/html.ts | 21 +++++++++++++++++++++ shared/core-utils/renderer/index.ts | 2 ++ shared/core-utils/renderer/markdown.ts | 23 +++++++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 shared/core-utils/renderer/html.ts create mode 100644 shared/core-utils/renderer/index.ts create mode 100644 shared/core-utils/renderer/markdown.ts (limited to 'shared') diff --git a/shared/core-utils/index.ts b/shared/core-utils/index.ts index 54e233522..42d7cab1d 100644 --- a/shared/core-utils/index.ts +++ b/shared/core-utils/index.ts @@ -1,3 +1,7 @@ +export * from './abuse' +export * from './i18n' export * from './logs' export * from './miscs' export * from './plugins' +export * from './renderer' +export * from './users' diff --git a/shared/core-utils/renderer/html.ts b/shared/core-utils/renderer/html.ts new file mode 100644 index 000000000..37ae5147c --- /dev/null +++ b/shared/core-utils/renderer/html.ts @@ -0,0 +1,21 @@ +export const SANITIZE_OPTIONS = { + allowedTags: [ 'a', 'p', 'span', 'br', 'strong', 'em', 'ul', 'ol', 'li' ], + allowedSchemes: [ 'http', 'https' ], + allowedAttributes: { + a: [ 'href', 'class', 'target', 'rel' ] + }, + transformTags: { + a: (tagName, attribs) => { + let rel = 'noopener noreferrer' + if (attribs.rel === 'me') rel += ' me' + + return { + tagName, + attribs: Object.assign(attribs, { + target: '_blank', + rel + }) + } + } + } +} diff --git a/shared/core-utils/renderer/index.ts b/shared/core-utils/renderer/index.ts new file mode 100644 index 000000000..0ad29d782 --- /dev/null +++ b/shared/core-utils/renderer/index.ts @@ -0,0 +1,2 @@ +export * from './markdown' +export * from './html' diff --git a/shared/core-utils/renderer/markdown.ts b/shared/core-utils/renderer/markdown.ts new file mode 100644 index 000000000..dff746d87 --- /dev/null +++ b/shared/core-utils/renderer/markdown.ts @@ -0,0 +1,23 @@ +export const TEXT_RULES = [ + 'linkify', + 'autolink', + 'emphasis', + 'link', + 'newline', + 'list' +] + +export const TEXT_WITH_HTML_RULES = TEXT_RULES.concat([ + 'html_inline', + 'html_block' +]) + +export const ENHANCED_RULES = TEXT_RULES.concat([ 'image' ]) +export const ENHANCED_WITH_HTML_RULES = TEXT_WITH_HTML_RULES.concat([ 'image' ]) + +export const COMPLETE_RULES = ENHANCED_WITH_HTML_RULES.concat([ + 'block', + 'inline', + 'heading', + 'paragraph' +]) -- cgit v1.2.3