Skip to content

Commit c9ca882

Browse files
committed
Merge branch 'master' of https://github.com/BearToCode/carta
2 parents 5d911c4 + e8f214d commit c9ca882

File tree

15 files changed

+387
-216
lines changed

15 files changed

+387
-216
lines changed

docs/src/lib/components/code/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const carta = new Carta({
2929
}
3030
});
3131

32+
export const highlighter = await carta.highlighter();
33+
3234
/**
3335
* Highlights the code blocks.
3436
* @param codeBlocks The code blocks to highlight
@@ -39,22 +41,23 @@ export async function highlightCodeBlocks<T extends string>(
3941
): Promise<HighlightedCodeBlocks<T>> {
4042
const highlightedCodeBlocks: HighlightedCodeBlocks<T> = {} as HighlightedCodeBlocks<T>;
4143

42-
const highlighter = await carta.highlighter();
4344
if (!highlighter) {
4445
throw new Error('Failed to get highlighter');
4546
}
4647

47-
const loadedLanguages = highlighter.getLoadedLanguages();
48+
const shiki = highlighter.shikiHighlighter();
49+
50+
const loadedLanguages = shiki.getLoadedLanguages();
4851

4952
for (const key in codeBlocks) {
5053
const codeBlock = codeBlocks[key];
5154

5255
if (isBundleLanguage(codeBlock.lang) && !loadedLanguages.includes(codeBlock.lang)) {
53-
await highlighter.loadLanguage(codeBlock.lang);
56+
await shiki.loadLanguage(codeBlock.lang);
5457
loadedLanguages.push(codeBlock.lang);
5558
}
5659

57-
const html = highlighter.codeToHtml(codeBlock.code, {
60+
const html = shiki.codeToHtml(codeBlock.code, {
5861
lang: codeBlock.lang,
5962
theme: 'houston'
6063
});

docs/src/routes/api/utilities/+page.server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ const codeBlocks = {
3535
lang: 'ts',
3636
code: deindent`
3737
const highlighter = await carta.highlighter();
38-
const userTheme = carta.theme;`
38+
const shiki = highlighter.shikiHighlighter();
39+
const html = await shiki.codeToHtml('console.log('Hello World!')', { lang: 'js' });
40+
`
3941
},
4042
isBundleLanguage: {
4143
lang: 'ts',

docs/src/routes/api/utilities/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<code>Carta.highlighter</code>
4747
</h2>
4848

49-
<p>Get the Shiki highlighter.</p>
49+
<p>Get the highlighter used by Carta.</p>
5050

5151
<Code code={data.codeBlocks.carta_highlighter} />
5252

docs/src/routes/plugins/math/+page.server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { highlightCodeBlocks } from '$lib/components/code';
1+
import { highlightCodeBlocks, highlighter } from '$lib/components/code';
22
import { deindent } from '$lib/utils';
33

44
const codeBlocks = {
@@ -39,11 +39,11 @@ const codeBlocks = {
3939
<MarkdownEditor {carta} />`
4040
},
4141
usageInline: {
42-
lang: 'cartamd',
42+
lang: highlighter?.settings.langHash as string,
4343
code: 'Pythagorean theorem: $a^2+b^2=c^2$'
4444
},
4545
usageBlock: {
46-
lang: 'cartamd',
46+
lang: highlighter?.settings.langHash as string,
4747
code: deindent`
4848
**Laplace** transform:
4949
$$

packages/carta-md/src/lib/internal/carta.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ export interface Plugin {
194194
onLoad?: (data: { carta: Carta }) => void;
195195
}
196196

197+
const USE_HIGHLIGHTER =
198+
BROWSER ||
199+
// Replaced at build time to tree-shake shiki on the server, if specified
200+
typeof __ENABLE_CARTA_SSR_HIGHLIGHTER__ === 'undefined' ||
201+
__ENABLE_CARTA_SSR_HIGHLIGHTER__ === true;
202+
197203
export class Carta {
198204
public readonly sanitizer?: (html: string) => string;
199205
public readonly historyOptions?: TextAreaHistoryOptions;
@@ -233,13 +239,7 @@ export class Carta {
233239

234240
public async highlighter(): Promise<Highlighter | undefined> {
235241
if (this.mHighlighter) return this.mHighlighter;
236-
if (
237-
!BROWSER &&
238-
// Replaced at build time to tree-shake shiki on the server, if specified
239-
typeof __ENABLE_CARTA_SSR_HIGHLIGHTER__ !== 'undefined' &&
240-
__ENABLE_CARTA_SSR_HIGHLIGHTER__ === false
241-
)
242-
return;
242+
if (!USE_HIGHLIGHTER) return;
243243

244244
this.mHighlighter = (async () => {
245245
const hl = await import('./highlight');
@@ -434,12 +434,7 @@ export class Carta {
434434
* @returns Rendered html.
435435
*/
436436
public async render(markdown: string): Promise<string> {
437-
if (
438-
BROWSER ||
439-
// Replaced at build time to tree-shake shiki on the server, if specified
440-
typeof __ENABLE_CARTA_SSR_HIGHLIGHTER__ === 'undefined' ||
441-
__ENABLE_CARTA_SSR_HIGHLIGHTER__ === true
442-
) {
437+
if (USE_HIGHLIGHTER) {
443438
const hl = await import('./highlight');
444439
const { loadNestedLanguages } = hl;
445440

packages/carta-md/src/lib/internal/components/Input.svelte

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,8 @@
109109
const highlight = async (text: string) => {
110110
const highlighter = await carta.highlighter();
111111
if (!highlighter) return;
112-
let html: string;
113112
114-
const hl = await import('$lib/internal/highlight');
115-
const { isSingleTheme } = hl;
116-
117-
if (isSingleTheme(highlighter.theme)) {
118-
// Single theme
119-
html = highlighter.codeToHtml(text, {
120-
lang: highlighter.lang,
121-
theme: highlighter.theme
122-
});
123-
} else {
124-
// Dual theme
125-
html = highlighter.codeToHtml(text, {
126-
lang: highlighter.lang,
127-
themes: highlighter.theme
128-
});
129-
}
113+
const html = highlighter.codeToHtml(text);
130114
131115
if (carta.sanitizer) {
132116
highlighted = carta.sanitizer(html);

0 commit comments

Comments
 (0)