-
Notifications
You must be signed in to change notification settings - Fork 1
codeblocks have syntax highlighting #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'; | ||
|
|
@@ -92,17 +93,36 @@ 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 languages = [] | ||
| let doc = parser.parseFromString(parsed, 'text/html'); | ||
|
|
||
| // how do you map a list? | ||
| for(let code of doc.querySelectorAll('code')) { | ||
| languages.push(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? | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the zip supposed to be doing?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")) { | ||
|
|
@@ -284,3 +304,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> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem to be used anywhere.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is, it provides the gruvbox
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.