generated from fuzdev/fuz_template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.svelte
More file actions
172 lines (159 loc) · 5.55 KB
/
Code.svelte
File metadata and controls
172 lines (159 loc) · 5.55 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
167
168
169
170
171
172
<script lang="ts">
import type {Snippet} from 'svelte';
import {DEV} from 'esm-env';
import type {SvelteHTMLElements} from 'svelte/elements';
import {syntax_styler_global} from './syntax_styler_global.js';
import type {SyntaxStyler, SyntaxGrammar} from './syntax_styler.js';
const {
content,
dangerous_raw_html,
lang = 'svelte',
grammar,
inline = false,
wrap = false,
nomargin = false,
syntax_styler = syntax_styler_global,
children,
...rest
}: SvelteHTMLElements['code'] &
(
| {
/** The source code to syntax highlight. */
content: string;
dangerous_raw_html?: undefined;
}
| {
content?: undefined;
/**
* Pre-highlighted HTML from the `svelte_preprocess_fuz_code` preprocessor.
* When provided, skips runtime syntax highlighting entirely.
*
* Named `dangerous_raw_html` to signal that it bypasses sanitization,
* matching the `{@html}` pattern already used by this component.
*/
dangerous_raw_html: string;
}
) & {
/**
* Language identifier (e.g., 'ts', 'css', 'html', 'json', 'svelte', 'md').
*
* **Purpose:**
* - When `grammar` is not provided, used to look up the grammar via `syntax_styler.get_lang(lang)`
* - Used for metadata: sets the `data-lang` attribute and determines `language_supported`
*
* **Special values:**
* - `null` - Explicitly disables syntax highlighting (content rendered as plain text)
* - `undefined` - Falls back to default ('svelte')
*
* **Relationship with `grammar`:**
* - If both `lang` and `grammar` are provided, `grammar` takes precedence for tokenization
* - However, `lang` is still used for the `data-lang` attribute and language detection
*
* @default 'svelte'
*/
lang?: string | null;
/**
* Optional custom grammar object for syntax tokenization.
*
* **When to use:**
* - To provide a custom language definition not registered in `syntax_styler.langs`
* - To use a modified/extended version of an existing grammar
* - For one-off grammar variations without registering globally
*
* **Behavior:**
* - When provided, this grammar is used for tokenization instead of looking up via `lang`
* - Enables highlighting even if `lang` is not in the registry (useful for custom languages)
* - The `lang` parameter is still used for metadata (data-lang attribute)
* - When undefined, the grammar is automatically looked up via `syntax_styler.get_lang(lang)`
*
* @default undefined (uses grammar from `syntax_styler.langs[lang]`)
*/
grammar?: SyntaxGrammar | undefined;
/**
* Whether to render as inline code or block code.
* Controls display via CSS classes.
*
* @default false
*/
inline?: boolean;
/**
* Whether to wrap long lines in block code.
* Sets `white-space: pre-wrap` instead of `white-space: pre`.
*
* **Behavior:**
* - Wraps at whitespace (spaces, newlines)
* - Long tokens without spaces (URLs, hashes) will still scroll horizontally
* - Default `false` provides traditional code block behavior
*
* Only affects block code (ignored for inline mode).
*
* @default false
*/
wrap?: boolean;
/**
* Whether to disable the default margin-bottom on block code.
* Block code has `margin-bottom: var(--space_lg)` by default when not `:last-child`.
*
* @default false
*/
nomargin?: boolean;
/**
* Custom SyntaxStyler instance to use for highlighting.
* Allows using a different styler with custom grammars or configuration.
*
* @default syntax_styler_global
*/
syntax_styler?: SyntaxStyler;
/**
* Optional snippet to customize how the highlighted markup is rendered.
* Receives the generated HTML string as a parameter.
*/
children?: Snippet<[markup: string]>;
} = $props();
const language_supported = $derived(lang !== null && !!syntax_styler.langs[lang]);
const highlighting_disabled = $derived(lang === null || (!language_supported && !grammar));
// DEV-only validation warnings
if (DEV) {
$effect(() => {
if (dangerous_raw_html != null) return;
if (lang && !language_supported && !grammar) {
const langs = Object.keys(syntax_styler.langs).join(', ');
// eslint-disable-next-line no-console
console.error(
`[Code] Language "${lang}" is not supported and no custom grammar provided. ` +
`Highlighting disabled. Supported: ${langs}`,
);
}
});
}
// Generate HTML markup for syntax highlighting
const html_content = $derived.by(() => {
if (dangerous_raw_html != null) return dangerous_raw_html;
if (!content || highlighting_disabled) return '';
return syntax_styler.stylize(content, lang!, grammar); // ! is safe bc of the `highlighting_disabled` calculation
});
</script>
<!-- eslint-disable svelte/no-at-html-tags -->
<code {...rest} class:inline class:wrap class:nomargin data-lang={lang}
>{#if highlighting_disabled && dangerous_raw_html == null}{content}{:else if children}{@render children(
html_content,
)}{:else}{@html html_content}{/if}</code
>
<style>
/* inline code inherits fuz_css defaults: pre-wrap, inline-block, baseline alignment */
code:not(.inline) {
/* block code: traditional no-wrap, horizontal scroll */
white-space: pre;
padding: var(--space_xs3) var(--space_xs);
display: block;
overflow: auto;
max-width: 100%;
}
code.wrap:not(.inline) {
/* unset what we set above, otherwise rely on fuz_css base styles */
white-space: pre-wrap;
}
code:not(.inline):not(.nomargin):not(:last-child) {
margin-bottom: var(--space_lg);
}
</style>