Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to new shiki token names #11661

Merged
merged 3 commits into from
Aug 9, 2024
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
10 changes: 10 additions & 0 deletions .changeset/quick-ads-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@astrojs/markdown-remark': major
---

Renames the following CSS variables theme color token names to better align with the Shiki v1 defaults:

- `--astro-code-color-text` => `--astro-code-foreground`
- `--astro-code-color-background` => `--astro-code-background`

You can perform a global find and replace in your project to migrate to the new token names.
6 changes: 3 additions & 3 deletions packages/astro/test/astro-component-code.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ describe('<Code>', () => {
.map((i, f) => (f.attribs ? f.attribs.style : 'no style found'))
.toArray(),
[
'background-color:var(--astro-code-color-background);color:var(--astro-code-color-text); overflow-x: auto;',
'background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;',
'color:var(--astro-code-token-constant)',
'color:var(--astro-code-token-function)',
'color:var(--astro-code-color-text)',
'color:var(--astro-code-foreground)',
'color:var(--astro-code-token-string-expression)',
'color:var(--astro-code-color-text)',
'color:var(--astro-code-foreground)',
]
);
});
Expand Down
30 changes: 0 additions & 30 deletions packages/markdown/remark/src/shiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
getHighlighter,
isSpecialLang,
} from 'shiki';
import { visit } from 'unist-util-visit';
import type { ShikiConfig } from './types.js';

export interface ShikiHighlighter {
Expand All @@ -23,16 +22,6 @@ export interface ShikiHighlighter {
): Promise<string>;
}

// TODO: Remove this special replacement in Astro 5
const ASTRO_COLOR_REPLACEMENTS: Record<string, string> = {
'--astro-code-foreground': '--astro-code-color-text',
'--astro-code-background': '--astro-code-color-background',
};
const COLOR_REPLACEMENT_REGEX = new RegExp(
`${Object.keys(ASTRO_COLOR_REPLACEMENTS).join('|')}`,
'g'
);

let _cssVariablesTheme: ReturnType<typeof createCssVariablesTheme>;
const cssVariablesTheme = () =>
_cssVariablesTheme ??
Expand Down Expand Up @@ -145,21 +134,6 @@ export async function createShikiHighlighter({
return node.children[0] as typeof node;
}
},
root(node) {
if (Object.values(themes).length) {
return;
}

const themeName = typeof theme === 'string' ? theme : theme.name;
if (themeName === 'css-variables') {
// Replace special color tokens to CSS variables
visit(node as any, 'element', (child) => {
if (child.properties?.style) {
child.properties.style = replaceCssVariables(child.properties.style);
}
});
}
},
},
...transformers,
],
Expand All @@ -171,7 +145,3 @@ export async function createShikiHighlighter({
function normalizePropAsString(value: Properties[string]): string | null {
return Array.isArray(value) ? value.join(' ') : (value as string | null);
}

function replaceCssVariables(str: string) {
return str.replace(COLOR_REPLACEMENT_REGEX, (match) => ASTRO_COLOR_REPLACEMENTS[match] || match);
}
Loading