diff options
author | Chocobozzz <me@florianbigard.com> | 2020-11-17 14:34:09 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2020-11-17 14:34:09 +0100 |
commit | 9ff36c2d70956d2775d207c7809adb6fe7f2f2a5 (patch) | |
tree | c9058f8210d9674b69307c4f7c5ccde85a1ba597 /shared | |
parent | 9afb5c10e5935e667e33219bdbd775e9ed1b4330 (diff) | |
download | PeerTube-9ff36c2d70956d2775d207c7809adb6fe7f2f2a5.tar.gz PeerTube-9ff36c2d70956d2775d207c7809adb6fe7f2f2a5.tar.zst PeerTube-9ff36c2d70956d2775d207c7809adb6fe7f2f2a5.zip |
Refactor markdown/sanitize html code
Diffstat (limited to 'shared')
-rw-r--r-- | shared/core-utils/index.ts | 4 | ||||
-rw-r--r-- | shared/core-utils/renderer/html.ts | 21 | ||||
-rw-r--r-- | shared/core-utils/renderer/index.ts | 2 | ||||
-rw-r--r-- | shared/core-utils/renderer/markdown.ts | 23 |
4 files changed, 50 insertions, 0 deletions
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 @@ | |||
1 | export * from './abuse' | ||
2 | export * from './i18n' | ||
1 | export * from './logs' | 3 | export * from './logs' |
2 | export * from './miscs' | 4 | export * from './miscs' |
3 | export * from './plugins' | 5 | export * from './plugins' |
6 | export * from './renderer' | ||
7 | 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 @@ | |||
1 | export const SANITIZE_OPTIONS = { | ||
2 | allowedTags: [ 'a', 'p', 'span', 'br', 'strong', 'em', 'ul', 'ol', 'li' ], | ||
3 | allowedSchemes: [ 'http', 'https' ], | ||
4 | allowedAttributes: { | ||
5 | a: [ 'href', 'class', 'target', 'rel' ] | ||
6 | }, | ||
7 | transformTags: { | ||
8 | a: (tagName, attribs) => { | ||
9 | let rel = 'noopener noreferrer' | ||
10 | if (attribs.rel === 'me') rel += ' me' | ||
11 | |||
12 | return { | ||
13 | tagName, | ||
14 | attribs: Object.assign(attribs, { | ||
15 | target: '_blank', | ||
16 | rel | ||
17 | }) | ||
18 | } | ||
19 | } | ||
20 | } | ||
21 | } | ||
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 @@ | |||
1 | export * from './markdown' | ||
2 | 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 @@ | |||
1 | export const TEXT_RULES = [ | ||
2 | 'linkify', | ||
3 | 'autolink', | ||
4 | 'emphasis', | ||
5 | 'link', | ||
6 | 'newline', | ||
7 | 'list' | ||
8 | ] | ||
9 | |||
10 | export const TEXT_WITH_HTML_RULES = TEXT_RULES.concat([ | ||
11 | 'html_inline', | ||
12 | 'html_block' | ||
13 | ]) | ||
14 | |||
15 | export const ENHANCED_RULES = TEXT_RULES.concat([ 'image' ]) | ||
16 | export const ENHANCED_WITH_HTML_RULES = TEXT_WITH_HTML_RULES.concat([ 'image' ]) | ||
17 | |||
18 | export const COMPLETE_RULES = ENHANCED_WITH_HTML_RULES.concat([ | ||
19 | 'block', | ||
20 | 'inline', | ||
21 | 'heading', | ||
22 | 'paragraph' | ||
23 | ]) | ||