Skip to content

Commit

Permalink
Merge branch 'master' of github.com:beyond-all-reason/bar-lobby
Browse files Browse the repository at this point in the history
  • Loading branch information
Jazcash committed Jun 13, 2023
2 parents 3b1b882 + 8b0d52b commit b29c049
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/renderer/components/misc/MarkDown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
</template>
<script lang="ts" setup>
import DOMPurify from "dompurify";
import { marked } from "marked";
import { marked, Renderer } from "marked";
const props = defineProps<{
text: string;
allowLinks?: boolean;
}>();
const markdownRenderer = new Renderer();
// Prevents marked from parsing #.
marked.use({
tokenizer: {
Expand All @@ -20,14 +23,29 @@ marked.use({
},
});
const allowedTags = ["p", "em", "b", "strong", "ul", "ol", "li", "code", "pre", "blockquote", "span", "del", "body"];
const allowedAttributes: string[] = [];
if (props.allowLinks) {
allowedTags.push("a");
allowedAttributes.push("href");
allowedAttributes.push("target");
markdownRenderer.link = (href, title, text) => {
return `<a href="${href}" target="_blank">${text}</a>`;
};
}
const sanitizeOptions: DOMPurify.Config = {
ALLOWED_ATTR: [],
ALLOWED_TAGS: ["p", "em", "b", "strong", "ul", "ol", "li", "code", "pre", "blockquote", "span", "del"],
ALLOWED_ATTR: allowedAttributes,
ALLOWED_TAGS: allowedTags,
};
const markdown = marked.parse(props.text);
const markdown = marked.parse(props.text, { renderer: markdownRenderer });
const processedText = DOMPurify.sanitize(markdown, sanitizeOptions);
const noLoss = markdown === processedText;
const noLoss = !props.allowLinks ? markdown === processedText : DOMPurify.removed.length === 0;
</script>
<style lang="scss" scoped>
.text {
Expand Down

0 comments on commit b29c049

Please sign in to comment.