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(mdx-loader): read image dimensions when processing Markdown #6339

Merged
merged 6 commits into from
Jan 19, 2022

Conversation

Josh-Cena
Copy link
Collaborator

@Josh-Cena Josh-Cena commented Jan 14, 2022

Motivation

Fix #3081. If we read the image's dimensions and ensure that the generated DOM has the aspect ratio, the browser can leave enough whitespace.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

Get an incognito to bypass cache. Visit these two URLs:

@Josh-Cena Josh-Cena added the pr: polish This PR adds a very minor behavior improvement that users will enjoy. label Jan 14, 2022
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Jan 14, 2022
@netlify
Copy link

netlify bot commented Jan 14, 2022

✔️ [V2]

🔨 Explore the source changes: 884a466

🔍 Inspect the deploy log: https://app.netlify.com/sites/docusaurus-2/deploys/61e261d4a651c300088e07b1

😎 Browse the preview: https://deploy-preview-6339--docusaurus-2.netlify.app

@github-actions
Copy link

github-actions bot commented Jan 14, 2022

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 87
🟢 Accessibility 98
🟢 Best practices 93
🟢 SEO 100
🟢 PWA 92

Lighthouse ran on https://deploy-preview-6339--docusaurus-2.netlify.app/

@Josh-Cena Josh-Cena marked this pull request as draft January 14, 2022 10:39
@github-actions
Copy link

github-actions bot commented Jan 14, 2022

Size Change: -25 B (0%)

Total Size: 677 kB

ℹ️ View Unchanged
Filename Size Change
website/.docusaurus/globalData.json 41.6 kB 0 B
website/build/assets/css/styles.********.css 103 kB -25 B (0%)
website/build/assets/js/main.********.js 503 kB 0 B
website/build/index.html 29.6 kB 0 B

compressed-size-action

@Josh-Cena Josh-Cena marked this pull request as ready for review January 14, 2022 10:44
@@ -181,6 +181,3 @@ html[data-theme='dark'] img[src$='#gh-light-mode-only'] {
.test-marker-site-custom-css-unique-rule {
content: "site-custom-css-unique-rule";
}
.test-marker-site-custom-css-shared-rule {
max-width: 100%;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This test is removed. We can't guarantee what this rule will be merged into because other bundled CSS may change. We can only guarantee site CSS is inserted after client module CSS.

@Josh-Cena Josh-Cena added the status: awaiting review This PR is ready for review, will be merged after maintainers' approval label Jan 16, 2022
@slorber
Copy link
Collaborator

slorber commented Jan 19, 2022

Thanks 👍

I couldn't repro the issue on the prod blog post 🤷‍♂️ but still LGTM 👍

@slorber slorber merged commit e5801e4 into main Jan 19, 2022
@slorber slorber deleted the jc/read-image-dim branch January 19, 2022 11:45
@Josh-Cena Josh-Cena removed the status: awaiting review This PR is ready for review, will be merged after maintainers' approval label Jan 19, 2022
@Josh-Cena
Copy link
Collaborator Author

You probably have to both clear the cache and turn on Network throttling... Took me a while to reproduce.

@slorber
Copy link
Collaborator

slorber commented Jan 19, 2022

already tried that, didn't work, same effect on both links

@Mauriceac
Copy link

Hi!

I've been following this issue for some time. I'm very grateful for the "Read image dimensions when processing Markdown" fix. But it doesn't seem to work completely for me. After updating to the latest version (2.0.0-beta.15), I still have some problems landing on the appropriate header on a page with various images.

For example, if I put the URL directly in the web browser, the page opens up in the exact place.
But, if I use a link inside the documentation to go to the page, the page opens up about a quarter to half a page above (refreshing brings me to the right place).

Here is an example:

URL (works perfectly):
https://doc.cotalker.com/docs/documentation/admin/workflows/settings_panels/workflowgroup-create-edit#settings

Below is a blog page within my documentation with the above link in the form of a button. Pressing it will leave me above the indicated header (cache must be cleared or working in incognito mode):
https://doc.cotalker.com/blog/2021/10/03/taskview

Just in case, here is the repository the documentation is hosted in:
https://github.com/Cotalker/documentation

THANK YOU!

@slorber
Copy link
Collaborator

slorber commented Feb 2, 2022

@Mauriceac this is because you are not using markdown images:

https://github.com/Cotalker/documentation/blob/main/docs/documentation/admin/workflows/settings_panels/workflowgroup-create-edit.md

<img
  alt="edit workflow group"
  className="img_sizing item shadow--tl"
  src={useBaseUrl("img/admin_workflowgroup_edit_00a.png")}
/>

We can't easily automatically apply width/height to those images declared through JSX., particularly when the string can be provided in fancy JS ways such as:

<img
  alt="edit workflow group"
  className="img_sizing item shadow--tl"
  src={useBaseUrl("my-image-folder/" + imageIndex(3))}
/>

We'll try to provide an official Image component later:

<Img
  alt="edit workflow group"
  className="img_sizing item shadow--tl"
  src="img/admin_workflowgroup_edit_00a.png"
/>

Until then, if you want width/height to be applied, there's no other choice than using md images unfortunately.

You can try to write your own webpack loader that gives you the size of the image so that you can write something like

import myImage from "@site/static/img/admin_workflowgroup_edit_00a.png";

<img
  alt="edit workflow group"
  className="img_sizing item shadow--tl"
  src={myImage.url}
  width={myImage.width}
  height={myImage.height}
/>;

@Mauriceac
Copy link

Mauriceac commented Feb 2, 2022

@slorber Thank you very much for such a quick response and solution!
And thank you for making Docusaurus the best static-site generator!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: polish This PR adds a very minor behavior improvement that users will enjoy.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hashlinks from inside docusaurus only work a second time
4 participants