summaryrefslogtreecommitdiff
path: root/packages/markdown/remark/src
diff options
context:
space:
mode:
authorGravatar bluwy <bjornlu.dev@gmail.com> 2024-10-10 21:28:25 +0800
committerGravatar bluwy <bjornlu.dev@gmail.com> 2024-10-10 21:28:25 +0800
commit9fc86e543a6e4d0c6d4c6acaf7502f00a11da270 (patch)
tree33d8508cf7a667d55a92475774e58510f5b68210 /packages/markdown/remark/src
parent15fa9babf31a9b8ab8fc8e611c931c178137e2f9 (diff)
parent582f12e1f6f99b54865a0b24d804ee0924f4ef55 (diff)
downloadastro-9fc86e543a6e4d0c6d4c6acaf7502f00a11da270.tar.gz
astro-9fc86e543a6e4d0c6d4c6acaf7502f00a11da270.tar.zst
astro-9fc86e543a6e4d0c6d4c6acaf7502f00a11da270.zip
Merge branch 'main' into next
Diffstat (limited to 'packages/markdown/remark/src')
-rw-r--r--packages/markdown/remark/src/index.ts2
-rw-r--r--packages/markdown/remark/src/rehype-shiki.ts1
-rw-r--r--packages/markdown/remark/src/shiki.ts20
-rw-r--r--packages/markdown/remark/src/types.ts2
4 files changed, 17 insertions, 8 deletions
diff --git a/packages/markdown/remark/src/index.ts b/packages/markdown/remark/src/index.ts
index dd7d0444a..de13523fe 100644
--- a/packages/markdown/remark/src/index.ts
+++ b/packages/markdown/remark/src/index.ts
@@ -43,6 +43,7 @@ export const markdownConfigDefaults: Required<AstroMarkdownOptions> = {
themes: {},
wrap: false,
transformers: [],
+ langAlias: {},
},
remarkPlugins: [],
rehypePlugins: [],
@@ -143,7 +144,6 @@ export async function createMarkdownProcessor(
// Ensure that the error message contains the input filename
// to make it easier for the user to fix the issue
err = prefixError(err, `Failed to parse Markdown file "${vfile.path}"`);
- // eslint-disable-next-line no-console
console.error(err);
throw err;
});
diff --git a/packages/markdown/remark/src/rehype-shiki.ts b/packages/markdown/remark/src/rehype-shiki.ts
index 9344ddbb9..43b38f095 100644
--- a/packages/markdown/remark/src/rehype-shiki.ts
+++ b/packages/markdown/remark/src/rehype-shiki.ts
@@ -12,6 +12,7 @@ export const rehypeShiki: Plugin<[ShikiConfig?], Root> = (config) => {
langs: config?.langs,
theme: config?.theme,
themes: config?.themes,
+ langAlias: config?.langAlias,
});
const highlighter = await highlighterAsync;
diff --git a/packages/markdown/remark/src/shiki.ts b/packages/markdown/remark/src/shiki.ts
index fe46b60c2..ed4daf527 100644
--- a/packages/markdown/remark/src/shiki.ts
+++ b/packages/markdown/remark/src/shiki.ts
@@ -1,6 +1,7 @@
import type { Properties, Root } from 'hast';
import {
type BundledLanguage,
+ type HighlighterCoreOptions,
type LanguageRegistration,
type ShikiTransformer,
type ThemeRegistration,
@@ -28,6 +29,7 @@ export interface CreateShikiHighlighterOptions {
langs?: LanguageRegistration[];
theme?: ThemePresets | ThemeRegistration | ThemeRegistrationRaw;
themes?: Record<string, ThemePresets | ThemeRegistration | ThemeRegistrationRaw>;
+ langAlias?: HighlighterCoreOptions['langAlias'];
}
export interface ShikiHighlighterHighlightOptions {
@@ -63,17 +65,21 @@ export interface ShikiHighlighterHighlightOptions {
let _cssVariablesTheme: ReturnType<typeof createCssVariablesTheme>;
const cssVariablesTheme = () =>
_cssVariablesTheme ??
- (_cssVariablesTheme = createCssVariablesTheme({ variablePrefix: '--astro-code-' }));
+ (_cssVariablesTheme = createCssVariablesTheme({
+ variablePrefix: '--astro-code-',
+ }));
export async function createShikiHighlighter({
langs = [],
theme = 'github-dark',
themes = {},
+ langAlias = {},
}: CreateShikiHighlighterOptions = {}): Promise<ShikiHighlighter> {
theme = theme === 'css-variables' ? cssVariablesTheme() : theme;
const highlighter = await createHighlighter({
langs: ['plaintext', ...langs],
+ langAlias,
themes: Object.values(themes).length ? Object.values(themes) : [theme],
});
@@ -83,14 +89,16 @@ export async function createShikiHighlighter({
options: ShikiHighlighterHighlightOptions,
to: 'hast' | 'html',
) {
+ const resolvedLang = langAlias[lang] ?? lang;
const loadedLanguages = highlighter.getLoadedLanguages();
- if (!isSpecialLang(lang) && !loadedLanguages.includes(lang)) {
+ if (!isSpecialLang(lang) && !loadedLanguages.includes(resolvedLang)) {
try {
- await highlighter.loadLanguage(lang as BundledLanguage);
+ await highlighter.loadLanguage(resolvedLang as BundledLanguage);
} catch (_err) {
- // eslint-disable-next-line no-console
- console.warn(`[Shiki] The language "${lang}" doesn't exist, falling back to "plaintext".`);
+ const langStr =
+ lang === resolvedLang ? `"${lang}"` : `"${lang}" (aliased to "${resolvedLang}")`;
+ console.warn(`[Shiki] The language ${langStr} doesn't exist, falling back to "plaintext".`);
lang = 'plaintext';
}
}
@@ -147,7 +155,7 @@ export async function createShikiHighlighter({
// Add "user-select: none;" for "+"/"-" diff symbols.
// Transform `<span class="line"><span style="...">+ something</span></span>
// into `<span class="line"><span style="..."><span style="user-select: none;">+</span> something</span></span>`
- if (lang === 'diff') {
+ if (resolvedLang === 'diff') {
const innerSpanNode = node.children[0];
const innerSpanTextNode =
innerSpanNode?.type === 'element' && innerSpanNode.children?.[0];
diff --git a/packages/markdown/remark/src/types.ts b/packages/markdown/remark/src/types.ts
index 6134bdf8a..e6a9d362b 100644
--- a/packages/markdown/remark/src/types.ts
+++ b/packages/markdown/remark/src/types.ts
@@ -36,7 +36,7 @@ export type RemarkRehype = RemarkRehypeOptions;
export type ThemePresets = BuiltinTheme | 'css-variables';
export interface ShikiConfig
- extends Pick<CreateShikiHighlighterOptions, 'langs' | 'theme' | 'themes'>,
+ extends Pick<CreateShikiHighlighterOptions, 'langs' | 'theme' | 'themes' | 'langAlias'>,
Pick<ShikiHighlighterHighlightOptions, 'defaultColor' | 'wrap' | 'transformers'> {}
export interface AstroMarkdownOptions {