aboutsummaryrefslogtreecommitdiff
path: root/packages/markdown/remark/src
diff options
context:
space:
mode:
authorGravatar horo <143025439+horo-fox@users.noreply.github.com> 2023-11-08 23:42:05 +0900
committerGravatar GitHub <noreply@github.com> 2023-11-08 22:42:05 +0800
commitc5010aad3475669648dc939e00f88bbb52489d0d (patch)
tree07d31ee297ac7c69c06519b9cea00d26fd7cfb4a /packages/markdown/remark/src
parent1ecc9aa3240b79a3879b1329aa4f671d80e87649 (diff)
downloadastro-c5010aad3475669648dc939e00f88bbb52489d0d.tar.gz
astro-c5010aad3475669648dc939e00f88bbb52489d0d.tar.zst
astro-c5010aad3475669648dc939e00f88bbb52489d0d.zip
Light/dark theming for shikiji's codeblocks (#8903)
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Diffstat (limited to 'packages/markdown/remark/src')
-rw-r--r--packages/markdown/remark/src/index.ts1
-rw-r--r--packages/markdown/remark/src/remark-shiki.ts11
-rw-r--r--packages/markdown/remark/src/types.ts1
3 files changed, 11 insertions, 2 deletions
diff --git a/packages/markdown/remark/src/index.ts b/packages/markdown/remark/src/index.ts
index 89c9ca8bd..61f97072b 100644
--- a/packages/markdown/remark/src/index.ts
+++ b/packages/markdown/remark/src/index.ts
@@ -39,6 +39,7 @@ export const markdownConfigDefaults: Omit<Required<AstroMarkdownOptions>, 'draft
shikiConfig: {
langs: [],
theme: 'github-dark',
+ experimentalThemes: {},
wrap: false,
},
remarkPlugins: [],
diff --git a/packages/markdown/remark/src/remark-shiki.ts b/packages/markdown/remark/src/remark-shiki.ts
index bf3dd0b78..4eaae5ff2 100644
--- a/packages/markdown/remark/src/remark-shiki.ts
+++ b/packages/markdown/remark/src/remark-shiki.ts
@@ -30,9 +30,15 @@ const highlighterCacheAsync = new Map<string, Promise<Highlighter>>();
export function remarkShiki({
langs = [],
theme = 'github-dark',
+ experimentalThemes = {},
wrap = false,
}: ShikiConfig = {}): ReturnType<RemarkPlugin> {
+ const themes = experimentalThemes;
+
const cacheId =
+ Object.values(themes)
+ .map((t) => (typeof t === 'string' ? t : t.name ?? ''))
+ .join(',') +
(typeof theme === 'string' ? theme : theme.name ?? '') +
langs.map((l) => l.name ?? (l as any).id).join(',');
@@ -40,7 +46,7 @@ export function remarkShiki({
if (!highlighterAsync) {
highlighterAsync = getHighlighter({
langs: langs.length ? langs : Object.keys(bundledLanguages),
- themes: [theme],
+ themes: Object.values(themes).length ? Object.values(themes) : [theme],
});
highlighterCacheAsync.set(cacheId, highlighterAsync);
}
@@ -64,7 +70,8 @@ export function remarkShiki({
lang = 'plaintext';
}
- let html = highlighter.codeToHtml(node.value, { lang, theme });
+ let themeOptions = Object.values(themes).length ? { themes } : { theme };
+ let html = highlighter.codeToHtml(node.value, { ...themeOptions, lang });
// Q: Couldn't these regexes match on a user's inputted code blocks?
// A: Nope! All rendered HTML is properly escaped.
diff --git a/packages/markdown/remark/src/types.ts b/packages/markdown/remark/src/types.ts
index 4abcf578d..7038e2425 100644
--- a/packages/markdown/remark/src/types.ts
+++ b/packages/markdown/remark/src/types.ts
@@ -42,6 +42,7 @@ export type RemarkRehype = Omit<RemarkRehypeOptions, 'handlers' | 'unknownHandle
export interface ShikiConfig {
langs?: LanguageRegistration[];
theme?: BuiltinTheme | ThemeRegistration | ThemeRegistrationRaw;
+ experimentalThemes?: Record<string, BuiltinTheme | ThemeRegistration | ThemeRegistrationRaw>;
wrap?: boolean | null;
}