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

feat: Provide height with sizes attribute #901

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion src/runtime/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ function getSizes (ctx: ImageCTX, input: string, opts: ImageSizesOptions): Image
if (Object.keys(sizes).length > 1) {
// 'sizes path'
for (const key in sizes) {
const variant = getSizesVariant(key, String(sizes[key]), height, hwRatio, ctx)
const _height = sizes[key][1] ? parseSize(sizes[key][1]) : height

const variant = getSizesVariant(key, sizes[key][0], _height, hwRatio, ctx)
if (variant === undefined) {
continue
}
Expand Down
11 changes: 4 additions & 7 deletions src/runtime/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,14 @@ export function parseDensities (input: string | undefined = ''): number[] {
return densities.filter((value, index) => densities.indexOf(value) === index)
}

export function parseSizes (input: Record<string, string | number> | string): Record<string, string> {
const sizes: Record<string, string> = {}
export function parseSizes (input: Record<string, string | number> | string): Record<string, string[]> {
const sizes: Record<string, string[]> = {}
// string => object
if (typeof input === 'string') {
for (const entry of input.split(/[\s,]+/).filter(e => e)) {
const s = entry.split(':')
if (s.length !== 2) {
sizes[s[0].trim()] = s[0].trim()
} else {
sizes[s[0].trim()] = s[1].trim()
}

sizes[s[0].trim()] = (s.length !== 2 ? s[0].trim() : s[1].trim()).split('_')
}
} else {
Object.assign(sizes, input)
Expand Down
Loading