8
8
</template >
9
9
10
10
<script lang="ts">
11
+ import Vue from ' vue'
12
+ import type { PropType } from ' vue'
11
13
import DOMPurify from ' dompurify'
12
14
import { marked } from ' marked'
13
- import type { PropType } from ' vue'
14
- import Vue from ' vue'
15
- import { transformPostToTemplate } from ' ../pages/post/readerExtensions'
15
+ import { markedRenderer , transformPostToHTML } from ' ../pages/post/readerExtensions'
16
16
import { getPhotoFromIPFS } from ' @/backend/getPhoto'
17
17
import ImagePopup from ' @/components/popups/Image.vue'
18
18
19
+ const ALLOWED_TAGS = [
20
+ ` pre ` ,
21
+ ` ipfsimage ` ,
22
+ ` p ` ,
23
+ ` code ` ,
24
+ ` ol ` ,
25
+ ` li ` ,
26
+ ` strong ` ,
27
+ ` em ` ,
28
+ ` u ` ,
29
+ ` del ` ,
30
+ ` blockquote ` ,
31
+ ` h1 ` ,
32
+ ` h2 ` ,
33
+ ` h3 ` ,
34
+ ` h4 ` ,
35
+ ` h5 ` ,
36
+ ` a ` ,
37
+ ` span ` ,
38
+ ]
39
+
40
+ const ALLOWED_ATTR = [` cid ` , ` alt ` , ` class ` , ` id ` , ` href ` ]
41
+
19
42
interface IData {
20
43
clickedImage: null | string
21
44
imageError: null | string
22
45
displayImagePopup: boolean
23
46
}
24
47
25
- function setupSanitization() {
26
- DOMPurify .addHook (` afterSanitizeAttributes ` , (node : Element ) => {
27
- // set all elements owning target to target=_blank
28
- if (node .getAttribute (` target ` )) {
29
- node .setAttribute (` target ` , ` _blank ` )
30
- node .setAttribute (` rel ` , ` noopener ` )
31
- }
32
- })
33
- }
34
-
35
- function sanitizeHTML(input : string ) {
36
- return DOMPurify .sanitize (input , {
37
- USE_PROFILES: { html: true },
38
- ALLOWED_TAGS: [` pre ` ],
39
- ADD_ATTR: [` cid ` ],
40
- ADD_TAGS: [` ipfsimage ` ],
41
- FORBID_ATTR: [` style ` ],
42
- FORBID_TAGS: [` style ` , ` img ` ],
43
- })
44
- }
45
-
46
48
export default Vue .extend ({
47
49
components: {
48
50
ImagePopup ,
@@ -67,17 +69,28 @@ export default Vue.extend({
67
69
computed: {
68
70
htmlContent() {
69
71
const html = marked .parse (this .content )
70
- const sanitizedHtml = sanitizeHTML (html )
71
- const transformedHtml = transformPostToTemplate (sanitizedHtml , this .postImages )
72
- return transformedHtml
72
+ const sanitizedHtml = DOMPurify .sanitize (html , {
73
+ ALLOWED_TAGS ,
74
+ ALLOWED_ATTR ,
75
+ })
76
+ return transformPostToHTML (sanitizedHtml , this .postImages )
73
77
},
74
78
},
79
+ created() {
80
+ marked .use ({ renderer: markedRenderer })
81
+ DOMPurify .addHook (` afterSanitizeAttributes ` , (node : Element ) => {
82
+ // set all elements owning target and all links to target=_blank
83
+ if (node .getAttribute (` target ` ) || node .tagName === ` A ` ) {
84
+ node .setAttribute (` target ` , ` _blank ` )
85
+ node .setAttribute (` rel ` , ` noopener noreferrer ` )
86
+ }
87
+ })
88
+ },
75
89
mounted() {
76
90
const images = this .$el .querySelectorAll (` img ` )
77
91
images .forEach ((image ) => {
78
92
this .lazyLoad (image )
79
93
})
80
- setupSanitization ()
81
94
},
82
95
methods: {
83
96
openImagePopup(image : HTMLImageElement ) {
0 commit comments