Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions components/Message.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<script lang="ts" setup>
import DOMPurify from 'dompurify';
import { parse } from 'marked';
import { codeToHtml } from 'shiki'
import type { MessageProps } from '~/types/props';
import MessageMedia from './MessageMedia.vue';
import MessageReply from './UserInterface/MessageReply.vue';
Expand Down Expand Up @@ -92,17 +93,30 @@ const hasEmbed = ref(false);
const sanitized = ref<string>();

onMounted(async () => {
const parsed = await parse(props.text, { gfm: true });
let t = props.text.replaceAll("<br>", "\n").replaceAll("\xa0", "");
let parsed = await parse(t, { gfm: true });
const parser = new DOMParser();
let doc = parser.parseFromString(parsed, 'text/html');
let languages = Array.from(doc.querySelectorAll('code')).map(code => code.className.replace("language-", ""));
sanitized.value = DOMPurify.sanitize(parsed, {
ALLOWED_TAGS: [
"strong", "em", "br", "blockquote",
"code", "ul", "ol", "li", "a", "h1",
"h2", "h3", "h4", "h5", "h6"
"h2", "h3", "h4", "h5", "h6", "pre",
],
ALLOW_DATA_ATTR: false,
ALLOW_SELF_CLOSE_IN_ATTR: false,
ALLOWED_ATTR: ["href"]
});

doc = parser.parseFromString(sanitized.value, 'text/html');
// how do you zip a list?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (language, codeblock) in zip(languages, doc.queryselectorall('code'))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the zip supposed to be doing?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iterates both at the same time, i.e

let i = 0;
for i in 0..languages.len() {
	let (language, codeblock) = (languages[i], codeblocks[i])
}

languages.reverse()
for(let code of doc.querySelectorAll('code')) {
code.innerHTML = await codeToHtml(code.innerHTML, { lang: languages.pop()!, theme: "gruvbox-dark-medium"});
}
sanitized.value = new XMLSerializer().serializeToString(doc);

console.log("adding listeners")
await nextTick();
if (messageElement.value?.classList.contains("grouped-message")) {
Expand Down Expand Up @@ -284,3 +298,13 @@ function getDayDifference(date1: Date, date2: Date) {
padding-left: 1em;
}
</style>
<style>
.shiki {
padding: 10px;
margin: 0px !important;
box-sizing: border-box;
border-radius: 10px;
width: 98%;
overflow-wrap: anywhere;
}
</style>
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"nuxt": "^3.17.0",
"pinia": "^3.0.2",
"pinia-plugin-persistedstate": "^4.2.0",
"shiki": "^3.8.1",
"tm-themes": "^1.10.7",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be used anywhere.

Copy link
Author

@bend-n bend-n Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is, it provides the gruvbox

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah

"typescript": "^5.8.3",
"vue": "^3.5.13",
"vue-router": "^4.5.1",
Expand Down
Loading