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

normalize class property #9723

Merged
merged 5 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/nervous-dodos-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/markdown-remark": patch
---

Normalize class property
8 changes: 7 additions & 1 deletion packages/markdown/remark/src/shiki.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -61,8 +62,9 @@ export async function createShikiHighlighter({
node.tagName = 'code';
}

const classValue = normalizeMaybeArray(node.properties.class) ?? '';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! This makes sense to me. Could we also handle this for style? I assume transformers could also change that to a non-string now.

Maybe we can rename and generalize the function as normalizePropAsString

// 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) ?? '';

// Replace "shiki" class naming with "astro-code"
Expand Down Expand Up @@ -129,6 +131,10 @@ export async function createShikiHighlighter({
};
}

function normalizeMaybeArray(value: Properties[string]): string | null {
return Array.isArray(value) ? value.join(' ') : (value as string);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If value is cast to string, when does the function return null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated @ematipico

}

/**
* shiki -> shikiji compat as we need to manually replace it
* @internal Exported for error overlay use only
Expand Down