summaryrefslogtreecommitdiff
path: root/packages/markdown/remark/src/remark-shiki.ts
blob: 6cd3861e5cf1f61306fbef5e7f724288d886a5fb (plain) (blame)
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
import type * as shiki from 'shiki';
import { getHighlighter } from 'shiki';
import { visit } from 'unist-util-visit';
import type { RemarkPlugin, ShikiConfig } from './types.js';

/**
 * getHighlighter() is the most expensive step of Shiki. Instead of calling it on every page,
 * cache it here as much as possible. Make sure that your highlighters can be cached, state-free.
 * We make this async, so that multiple calls to parse markdown still share the same highlighter.
 */
const highlighterCacheAsync = new Map<string, Promise<shiki.Highlighter>>();

export function remarkShiki({
	langs = [],
	theme = 'github-dark',
	wrap = false,
}: ShikiConfig = {}): ReturnType<RemarkPlugin> {
	const cacheID: string = typeof theme === 'string' ? theme : theme.name;
	let highlighterAsync = highlighterCacheAsync.get(cacheID);
	if (!highlighterAsync) {
		highlighterAsync = getHighlighter({ theme }).then((hl) => {
			hl.setColorReplacements({
				'#000001': 'var(--astro-code-color-text)',
				'#000002': 'var(--astro-code-color-background)',
				'#000004': 'var(--astro-code-token-constant)',
				'#000005': 'var(--astro-code-token-string)',
				'#000006': 'var(--astro-code-token-comment)',
				'#000007': 'var(--astro-code-token-keyword)',
				'#000008': 'var(--astro-code-token-parameter)',
				'#000009': 'var(--astro-code-token-function)',
				'#000010': 'var(--astro-code-token-string-expression)',
				'#000011': 'var(--astro-code-token-punctuation)',
				'#000012': 'var(--astro-code-token-link)',
			});
			return hl;
		});
		highlighterCacheAsync.set(cacheID, highlighterAsync);
	}

	let highlighter: shiki.Highlighter;

	return async (tree: any) => {
		// Lazily assign the highlighter as async can only happen within this function,
		// and not on `remarkShiki` directly.
		if (!highlighter) {
			highlighter = await highlighterAsync!;

			// NOTE: There may be a performance issue here for large sites that use `lang`.
			// Since this will be called on every page load. Unclear how to fix this.
			for (const lang of langs) {
				await highlighter.loadLanguage(lang);
			}
		}

		visit(tree, 'code', (node) => {
			let lang: string;

			if (typeof node.lang === 'string') {
				const langExists = highlighter.getLoadedLanguages().includes(node.lang);
				if (langExists) {
					lang = node.lang;
				} else {
					// eslint-disable-next-line no-console
					console.warn(`The language "${node.lang}" doesn't exist, falling back to plaintext.`);
					lang = 'plaintext';
				}
			} else {
				lang = 'plaintext';
			}

			let html = highlighter.codeToHtml(node.value, { lang });

			// Q: Couldn't these regexes match on a user's inputted code blocks?
			// A: Nope! All rendered HTML is properly escaped.
			// Ex. If a user typed `<span class="line"` into a code block,
			// It would become this before hitting our regexes:
			// &lt;span class=&quot;line&quot;

			// Replace "shiki" class naming with "astro" and add "is:raw".
			html = html.replace(/<pre class="(.*?)shiki(.*?)"/, `<pre is:raw class="$1astro-code$2"`);
			// Add "user-select: none;" for "+"/"-" diff symbols
			if (node.lang === 'diff') {
				html = html.replace(
					/<span class="line"><span style="(.*?)">([\+|\-])/g,
					'<span class="line"><span style="$1"><span style="user-select: none;">$2</span>'
				);
			}
			// Handle code wrapping
			// if wrap=null, do nothing.
			if (wrap === false) {
				html = html.replace(/style="(.*?)"/, 'style="$1; overflow-x: auto;"');
			} else if (wrap === true) {
				html = html.replace(
					/style="(.*?)"/,
					'style="$1; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;"'
				);
			}

			node.type = 'html';
			node.value = html;
			node.children = [];
		});
	};
}