From f1206e827475e4f552ded87925c648ec4c700650 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 10 Jan 2021 19:03:47 +0100 Subject: [PATCH] Add code highlighting for rendered user input text --- package.json | 1 + src/components/input/editor.vue | 6 ++++++ yarn.lock | 5 +++++ 3 files changed, 12 insertions(+) diff --git a/package.json b/package.json index 496f0c4ca..e534cc161 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "copy-to-clipboard": "3.3.1", "date-fns": "2.16.1", "dompurify": "2.2.6", + "highlight.js": "^10.5.0", "lodash": "4.17.20", "marked": "1.2.7", "register-service-worker": "1.7.2", diff --git a/src/components/input/editor.vue b/src/components/input/editor.vue index c651bd008..6c902aaf5 100644 --- a/src/components/input/editor.vue +++ b/src/components/input/editor.vue @@ -329,6 +329,11 @@ export default { return `` }, }, + highlight: function(code, language) { + const hljs = require('highlight.js'); + const validLanguage = hljs.getLanguage(language) ? language : 'plaintext'; + return hljs.highlight(validLanguage, code).value; + }, }) this.preview = DOMPurify.sanitize(marked(this.text)) @@ -406,6 +411,7 @@ export default {