Last active
September 17, 2024 14:50
-
-
Save daliborgogic/60fd819f214be5033243b0f8c69d2566 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DOMPurify from 'dompurify' | |
export default defineNuxtPlugin(async (nuxtApp) => { | |
let JSDOM | |
if (import.meta.server) { | |
const { JSDOM: dom } = await import('jsdom') | |
JSDOM = dom | |
} | |
nuxtApp.vueApp.directive('sanitize', { | |
mounted: (el, binding) => { | |
el.innerHTML = DOMPurify.sanitize(binding.value) | |
}, | |
updated: (el, binding) => { | |
el.innerHTML = DOMPurify.sanitize(binding.value) | |
}, | |
getSSRProps(binding) { | |
return { | |
innerHTML: DOMPurify(new JSDOM('').window).sanitize(binding.value) | |
} | |
} | |
}) | |
}) |
Author
daliborgogic
commented
Sep 14, 2024
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment