Skip to content

Commit 31fab68

Browse files
authored
feat: format numbers in code highlighting with locale string (#69)
Added: - Formatting numbers in topic last message highlight
1 parent c1f5b58 commit 31fab68

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/renderer/src/components/tap-topics/CodeHighlight.vue

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { formatCode } from '@renderer/assets/js/format-code'
44
import json from 'highlight.js/lib/languages/json'
55
import yaml from 'highlight.js/lib/languages/yaml'
66
import xml from 'highlight.js/lib/languages/xml'
7+
import { ref, watch, nextTick } from 'vue'
78
import hljs from 'highlight.js/lib/core'
8-
import { ref, watch } from 'vue'
99
1010
hljs.registerLanguage('json', json)
1111
hljs.registerLanguage('xml', xml)
@@ -21,6 +21,7 @@ const props = defineProps<Props>()
2121
useHighlightTheme()
2222
2323
const highlighted = ref('')
24+
const codeTextRef = ref<HTMLElement>()
2425
2526
watch(
2627
[() => props.code, () => props.language],
@@ -32,13 +33,26 @@ watch(
3233
highlighted.value = hljs.highlight(code, {
3334
language: props.language
3435
}).value
36+
37+
nextTick(() => {
38+
if (!codeTextRef.value) return
39+
40+
codeTextRef.value.querySelectorAll('span.hljs-number').forEach((el) => {
41+
const text = el.textContent?.trim() ?? ''
42+
43+
if (/^-?\d+(\.\d+)?$/.test(text)) {
44+
const num = parseFloat(text)
45+
el.textContent = num.toLocaleString(undefined, { maximumFractionDigits: 12 })
46+
}
47+
})
48+
})
3549
},
3650
{ immediate: true }
3751
)
3852
</script>
3953

4054
<template>
41-
<span v-if="language !== 'raw'" class="code-text" v-html="highlighted" />
55+
<span v-if="language !== 'raw'" ref="codeTextRef" class="code-text" v-html="highlighted" />
4256
<span v-else class="code-text" v-text="code" />
4357
</template>
4458

0 commit comments

Comments
 (0)