mirror of
https://github.com/withastro/astro.git
synced 2025-02-17 22:44:24 -05:00
normalize class property (#9723)
* normalize class property * Add changeset * properly type return value * normalize styles, rename function * properly describe change Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev> --------- Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
This commit is contained in:
parent
ad01642517
commit
2f81cffa9d
2 changed files with 12 additions and 3 deletions
5
.changeset/nervous-dodos-compete.md
Normal file
5
.changeset/nervous-dodos-compete.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"@astrojs/markdown-remark": patch
|
||||
---
|
||||
|
||||
Fixes a case where transformers wouldn't work on the `class` property
|
|
@ -1,5 +1,6 @@
|
|||
import { bundledLanguages, createCssVariablesTheme, getHighlighter } from 'shikiji';
|
||||
import { visit } from 'unist-util-visit';
|
||||
import type { Properties } from 'hast';
|
||||
import type { ShikiConfig } from './types.js';
|
||||
|
||||
export interface ShikiHighlighter {
|
||||
|
@ -61,9 +62,8 @@ export async function createShikiHighlighter({
|
|||
node.tagName = 'code';
|
||||
}
|
||||
|
||||
// Cast to string as shikiji will always pass them as strings instead of any other types
|
||||
const classValue = (node.properties.class as string) ?? '';
|
||||
const styleValue = (node.properties.style as string) ?? '';
|
||||
const classValue = normalizePropAsString(node.properties.class) ?? '';
|
||||
const styleValue = normalizePropAsString(node.properties.style) ?? '';
|
||||
|
||||
// Replace "shiki" class naming with "astro-code"
|
||||
node.properties.class = classValue.replace(/shiki/g, 'astro-code');
|
||||
|
@ -129,6 +129,10 @@ export async function createShikiHighlighter({
|
|||
};
|
||||
}
|
||||
|
||||
function normalizePropAsString(value: Properties[string]): string | null {
|
||||
return Array.isArray(value) ? value.join(' ') : (value as string | null);
|
||||
}
|
||||
|
||||
/**
|
||||
* shiki -> shikiji compat as we need to manually replace it
|
||||
* @internal Exported for error overlay use only
|
||||
|
|
Loading…
Add table
Reference in a new issue