Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions docs/src/lib/components/code/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const carta = new Carta({
}
});

export const highlighter = await carta.highlighter();

/**
* Highlights the code blocks.
* @param codeBlocks The code blocks to highlight
Expand All @@ -39,22 +41,23 @@ export async function highlightCodeBlocks<T extends string>(
): Promise<HighlightedCodeBlocks<T>> {
const highlightedCodeBlocks: HighlightedCodeBlocks<T> = {} as HighlightedCodeBlocks<T>;

const highlighter = await carta.highlighter();
if (!highlighter) {
throw new Error('Failed to get highlighter');
}

const loadedLanguages = highlighter.getLoadedLanguages();
const shiki = highlighter.shikiHighlighter();

const loadedLanguages = shiki.getLoadedLanguages();

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

if (isBundleLanguage(codeBlock.lang) && !loadedLanguages.includes(codeBlock.lang)) {
await highlighter.loadLanguage(codeBlock.lang);
await shiki.loadLanguage(codeBlock.lang);
loadedLanguages.push(codeBlock.lang);
}

const html = highlighter.codeToHtml(codeBlock.code, {
const html = shiki.codeToHtml(codeBlock.code, {
lang: codeBlock.lang,
theme: 'houston'
});
Expand Down
4 changes: 3 additions & 1 deletion docs/src/routes/api/utilities/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ const codeBlocks = {
lang: 'ts',
code: deindent`
const highlighter = await carta.highlighter();
const userTheme = carta.theme;`
const shiki = highlighter.shikiHighlighter();
const html = await shiki.codeToHtml('console.log('Hello World!')', { lang: 'js' });
`
},
isBundleLanguage: {
lang: 'ts',
Expand Down
2 changes: 1 addition & 1 deletion docs/src/routes/api/utilities/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<code>Carta.highlighter</code>
</h2>

<p>Get the Shiki highlighter.</p>
<p>Get the highlighter used by Carta.</p>

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

Expand Down
6 changes: 3 additions & 3 deletions docs/src/routes/plugins/math/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { highlightCodeBlocks } from '$lib/components/code';
import { highlightCodeBlocks, highlighter } from '$lib/components/code';
import { deindent } from '$lib/utils';

const codeBlocks = {
Expand Down Expand Up @@ -39,11 +39,11 @@ const codeBlocks = {
<MarkdownEditor {carta} />`
},
usageInline: {
lang: 'cartamd',
lang: highlighter?.settings.langHash as string,
code: 'Pythagorean theorem: $a^2+b^2=c^2$'
},
usageBlock: {
lang: 'cartamd',
lang: highlighter?.settings.langHash as string,
code: deindent`
**Laplace** transform:
$$
Expand Down
21 changes: 8 additions & 13 deletions packages/carta-md/src/lib/internal/carta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ export interface Plugin {
onLoad?: (data: { carta: Carta }) => void;
}

const USE_HIGHLIGHTER =
BROWSER ||
// Replaced at build time to tree-shake shiki on the server, if specified
typeof __ENABLE_CARTA_SSR_HIGHLIGHTER__ === 'undefined' ||
__ENABLE_CARTA_SSR_HIGHLIGHTER__ === true;

export class Carta {
public readonly sanitizer?: (html: string) => string;
public readonly historyOptions?: TextAreaHistoryOptions;
Expand Down Expand Up @@ -233,13 +239,7 @@ export class Carta {

public async highlighter(): Promise<Highlighter | undefined> {
if (this.mHighlighter) return this.mHighlighter;
if (
!BROWSER &&
// Replaced at build time to tree-shake shiki on the server, if specified
typeof __ENABLE_CARTA_SSR_HIGHLIGHTER__ !== 'undefined' &&
__ENABLE_CARTA_SSR_HIGHLIGHTER__ === false
)
return;
if (!USE_HIGHLIGHTER) return;

this.mHighlighter = (async () => {
const hl = await import('./highlight');
Expand Down Expand Up @@ -434,12 +434,7 @@ export class Carta {
* @returns Rendered html.
*/
public async render(markdown: string): Promise<string> {
if (
BROWSER ||
// Replaced at build time to tree-shake shiki on the server, if specified
typeof __ENABLE_CARTA_SSR_HIGHLIGHTER__ === 'undefined' ||
__ENABLE_CARTA_SSR_HIGHLIGHTER__ === true
) {
if (USE_HIGHLIGHTER) {
const hl = await import('./highlight');
const { loadNestedLanguages } = hl;

Expand Down
18 changes: 1 addition & 17 deletions packages/carta-md/src/lib/internal/components/Input.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,8 @@
const highlight = async (text: string) => {
const highlighter = await carta.highlighter();
if (!highlighter) return;
let html: string;

const hl = await import('$lib/internal/highlight');
const { isSingleTheme } = hl;

if (isSingleTheme(highlighter.theme)) {
// Single theme
html = highlighter.codeToHtml(text, {
lang: highlighter.lang,
theme: highlighter.theme
});
} else {
// Dual theme
html = highlighter.codeToHtml(text, {
lang: highlighter.lang,
themes: highlighter.theme
});
}
const html = highlighter.codeToHtml(text);

if (carta.sanitizer) {
highlighted = carta.sanitizer(html);
Expand Down
Loading