forked from SciSharp/BotSharp-UI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkdown.svelte
More file actions
166 lines (153 loc) · 5 KB
/
Copy pathMarkdown.svelte
File metadata and controls
166 lines (153 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<script>
import { onMount } from 'svelte';
import { marked } from 'marked';
import { replaceMarkdown, replaceNewLine } from '$lib/helpers/http';
import 'overlayscrollbars/overlayscrollbars.css';
import { OverlayScrollbars } from 'overlayscrollbars';
import { v4 as uuidv4 } from 'uuid';
let {
/** @type {string} */
text = '',
/** @type {string} */
containerClasses = '',
/** @type {string} */
containerStyles = '',
/** @type {boolean} */
rawText = false,
/** @type {boolean} */
scrollable = false
} = $props();
const scrollbarId = `markdown-scrollbar-${uuidv4()}`;
const options = {
scrollbars: {
visibility: 'auto',
autoHide: 'move',
autoHideDelay: 100,
dragScroll: true,
clickScroll: false,
theme: 'os-theme-light',
pointers: ['mouse', 'touch', 'pen']
}
};
onMount(() => {
if (scrollable) {
initScrollbar();
}
});
function initScrollbar() {
const elem = document.querySelector(`#${scrollbarId}`);
if (elem) {
// @ts-ignore
const scrollbar = OverlayScrollbars(elem, options);
}
}
let innerText = $derived.by(() => {
const normalizedText = typeof text !== 'string' ? `${JSON.stringify(text)}` : text;
const markedText = !rawText
? replaceNewLine(marked(replaceMarkdown(normalizedText || ''))?.toString())
: marked(normalizedText || '', { breaks: true })?.toString();
if (!!markedText && markedText.endsWith('<br>')) {
const idx = markedText.lastIndexOf('<br>');
return markedText.substring(0, idx);
} else {
return markedText;
}
});
</script>
<div
id={`${scrollbarId}`}
class={`markdown-container markdown-lite ${containerClasses || 'text-white'}`}
style={`${containerStyles}`}
>
{@html innerText}
<!-- <SvelteMarkdown
source={innerText}
renderers={{
code: CodeBlock
}}
/> -->
</div>
<style>
/* Ported from src/lib/scss/custom/components/_markdown.scss. The HTML
rendered here is injected via {@html} from `marked`, so Svelte's CSS
scoping cannot hash the inner nodes — every selector that targets the
rendered markup is wrapped in :global(). */
.markdown-container {
overflow-x: auto;
scrollbar-width: thin;
}
.markdown-container :global(pre) {
-ms-overflow-style: none;
white-space: pre-wrap;
margin-top: 1em;
margin-bottom: 1em;
}
.markdown-container :global(pre::-webkit-scrollbar) {
display: none;
}
/* Language-typed code blocks emitted by `marked` get the inverted-card
treatment (dark surface, light text) regardless of the variant the
Markdown.svelte parent uses, so the code stays legible inside both
`markdown-lite` and `markdown-dark` containers. */
.markdown-container :global(pre:has(.language-sql)),
.markdown-container :global(pre:has(.language-java)),
.markdown-container :global(pre:has(.language-javascript)),
.markdown-container :global(pre:has(.language-typescript)),
.markdown-container :global(pre:has(.language-csharp)),
.markdown-container :global(pre:has(.language-python)),
.markdown-container :global(pre:has(.language-json)) {
background-color: black;
color: white;
border-radius: 5px;
padding: 5px 10px;
}
.markdown-container :global(table) {
margin-top: 1em;
margin-bottom: 1em;
border-radius: 5px;
}
.markdown-container :global(table th),
.markdown-container :global(table td) {
padding: 3px 5px;
}
.markdown-container :global(p) {
margin-top: 0;
margin-bottom: 0;
}
/* Tailwind's Preflight (app.css) resets ol/ul to `list-style: none` and
zero padding in @layer base. These unlayered rules restore the markers
for rendered markdown; call sites may still override padding/margins. */
.markdown-container :global(strong) {
font-weight: 700;
}
.markdown-container :global(ul) {
list-style: disc outside;
padding-left: 1.5rem;
}
.markdown-container :global(ol) {
list-style: decimal outside;
padding-left: 1.5rem;
}
/* Variant: `markdown-lite` — used when the container sits on a dark
surface (e.g. the chat thread's avatar/tool rows). Borders and links
are rendered in pure white. */
.markdown-lite :global(table th),
.markdown-lite :global(table td) {
border: 1px solid white;
}
.markdown-lite :global(a) {
color: white;
}
/* Variant: `markdown-dark` — used when the container sits on a light
surface (e.g. the assistant bubble's white card). Borders and links
use the primary accent. When both variants are applied to the same
container (chat-box's `markdown-dark cb-md-dark` markdown-lite combo),
these rules ship last in source order and win on equal specificity. */
.markdown-dark :global(table th),
.markdown-dark :global(table td) {
border: 1px solid var(--color-primary);
}
.markdown-dark :global(a) {
color: var(--color-primary);
}
</style>