-
Notifications
You must be signed in to change notification settings - Fork 44
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(imsize): rework image styling to get more control #549
Conversation
plz, add more tests:
|
Problem: If we have an image with predefined dimensions in the markup (height and width), then when it is displayed in a container, it is assumed to be of those sizes. However, the CSS already sets The proposed solution in the pull request allows us to control how the image displays according to its proportions. When we resize the container, the image's width will decrease as before, but its height will now depend on an aspect-ratio parameter instead of the P.S. I also added some tests with percent sizes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that right proportions for image are more important than height+width from markup.
@@ -206,6 +206,27 @@ export const imageWithSize = (md: MarkdownIt): ParserInline.RuleInline => { | |||
if (height !== '') { | |||
token.attrs.push([ImsizeAttr.Height, height]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we still need height and width attributes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
transform/src/transform/plugins/changelog/index.ts
Lines 77 to 78 in bff7df9
const width = Number(imageToken.attrGet('width')); | |
const height = Number(imageToken.attrGet('height')); |
Alas, that's why we still need it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also noticed complaints about incorrect image appearance when the height was not proportional. However, I did not see any requests for a stretched image or an image with extra frames.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The proportions of the image are calculated based on the height and width specified in the markup. If the container is larger than these dimensions, the image will maintain its original height and width. If the width of the container is less than the width of the image, the image's width will adjust to 100% of the container's width and the height will be adjusted proportionally based on the original height and width values from the markup.
3cd0e21
to
3d1ab7a
Compare
6d3b490
to
09f1cbf
Compare
if (height !== '') { | ||
if (width !== '' && !heightWithPercent && !widthWithPercent) { | ||
style += `aspect-ratio: ${width} / ${height};height: auto;`; | ||
forcedSanitizeWhiteList['aspect-ratio'] = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to enforce the 'aspect-ratio' rule for inline CSS sanitizing. Otherwise, this rule may be ignored and some artifacts may occur.
6f8a7ae
to
648f539
Compare
src/transform/sanitize.ts
Outdated
export default function sanitize( | ||
html: string, | ||
options?: SanitizeOptions, | ||
forcedSanitizeCssWhiteList?: CssWhiteList, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I suggest replacing it with an interface that has an additional parameter for uniformity and future expansion of functionality.
html: string,
options?: SanitizeOptions,
additionalOptions?: SanitizeOptions,
- Then it will be:
if (forcedSanitizeCssWhiteList) {
sanitizeOptions.cssWhiteList = {
...sanitizeOptions.cssWhiteList,
...additionalOptions.cssWhiteList,
};
}
@@ -5,7 +5,11 @@ import type Token from 'markdown-it/lib/token'; | |||
import {ImsizeAttr} from './const'; | |||
import {parseImageSize} from './helpers'; | |||
|
|||
export const imageWithSize = (md: MarkdownIt): ParserInline.RuleInline => { | |||
export type ImsizeOptions = { | |||
inlineSizeStyling?: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe better is enableInlineStyling
59709e4
to
3aa9433
Compare
3aa9433
to
d6ce8fb
Compare
These changes are necessary in order to gain more control over the appearance of the images in cases where we need to maintain the proportions when resizing the container.
Now this is a bit bugged - when the height exceeds the expected one, we see empty space around the image from above and below