-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathcode-inline.gts
More file actions
62 lines (50 loc) · 1.4 KB
/
Copy pathcode-inline.gts
File metadata and controls
62 lines (50 loc) · 1.4 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
import Component from '@glimmer/component';
import { modifier } from 'ember-modifier';
import { htmlSafe, type SafeString } from '@ember/template';
import { tracked } from '@glimmer/tracking';
import { assert } from '@ember/debug';
import { codeToHtml } from 'shiki';
import { task } from 'ember-concurrency';
interface CodeInlineSignature {
Element: HTMLElement;
Args: {
code: string;
language?: string;
};
}
export default class CodeInline extends Component<CodeInlineSignature> {
@tracked prismCode: string | SafeString = '';
get code() {
const code = this.args.code;
assert(
"ember-prism's <CodeBlock/> and <CodeInline/> components require a `code` parameter to be passed in.",
code !== undefined,
);
return code;
}
get language() {
return this.args.language ?? 'markup';
}
setup = modifier(() => {
void this.generateCode.perform();
});
generateCode = task(async () => {
const code = this.code;
const language = this.language;
const htmlCode = await codeToHtml(code, {
lang: language,
theme: 'github-light-default',
});
this.prismCode = htmlSafe(htmlCode);
});
<template>
<div ...attributes {{this.setup}}>
{{~! ~}}{{this.prismCode}}{{~! ~}}
</div>
</template>
}
declare module '@glint/environment-ember-loose/registry' {
export default interface Registry {
CodeInline: typeof CodeInline;
}
}