Skip to content

Commit cf4e951

Browse files
authored
Merge pull request #11226 from quarto-dev/bugfix/10311
Loosen image auto-discovery for OpenGraph
2 parents d1b4059 + 0a19859 commit cf4e951

File tree

13 files changed

+161
-13
lines changed

13 files changed

+161
-13
lines changed

news/changelog-1.6.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ All changes included in 1.6:
8686
- ([#2671](https://github.com/quarto-dev/quarto-cli/issues/2671)): Ensure that `--output-dir` works across filesystem boundaries.
8787
- ([#8517](https://github.com/quarto-dev/quarto-cli/issues/8571)), ([#10829](https://github.com/quarto-dev/quarto-cli/issues/10829)): Allow listing categories with non-alphanumeric characters such as apostrophes, etc.
8888
- ([#8932](https://github.com/quarto-dev/quarto-cli/issues/8932)): Escape render ids in markdown pipeline to allow special characters in sidebars/navbars, etc.
89+
- ([#10311](https://github.com/quarto-dev/quarto-cli/issues/10311)): Loosen auto-discovery of images for OpenGraph cards.
8990
- ([#10567](https://github.com/quarto-dev/quarto-cli/issues/10567)): Generate breadcrumbs correctly for documents using a level-1 heading as the title.
9091
- ([#10616](https://github.com/quarto-dev/quarto-cli/issues/10268)): Add a `z-index` setting to the 'back to top' button to ensure it is always visible.
9192

src/project/types/website/listing/website-listing-shared.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ export function readRenderedContents(
564564

565565
// Find a preview image, if present
566566
const computePreviewImage = (): PreviewImage | undefined => {
567-
const previewImageEl = findPreviewImgEl(doc, false);
567+
const previewImageEl = findPreviewImgEl(doc);
568568
if (previewImageEl) {
569569
const previewImageSrc = getDecodedAttribute(previewImageEl, "src");
570570
if (previewImageSrc !== null) {

src/project/types/website/util/discover-meta.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ export function findDescription(doc: Document): string | undefined {
4141

4242
export function findPreviewImg(
4343
doc: Document,
44-
strict: boolean,
4544
): string | undefined {
46-
const imgEl = findPreviewImgEl(doc, strict);
45+
const imgEl = findPreviewImgEl(doc);
4746
if (imgEl) {
4847
const src = getDecodedAttribute(imgEl, "src");
4948
if (src !== null) {
@@ -58,7 +57,6 @@ export function findPreviewImg(
5857

5958
export function findPreviewImgEl(
6059
doc: Document,
61-
strict: boolean,
6260
): Element | undefined {
6361
// look for an image explicitly marked as the preview image (.class .preview-image)
6462
const match = doc.querySelector(`img.${kPreviewImgClass}`);
@@ -86,13 +84,14 @@ export function findPreviewImgEl(
8684
}
8785

8886
// as a last resort, just use the first _local_ image found within the document body
89-
if (!strict) {
90-
const autoImg = doc.querySelector("#quarto-document-content img");
91-
if (autoImg) {
92-
const src = autoImg.getAttribute("src");
93-
if (src && !src.startsWith("http:") && !src.startsWith("https:")) {
94-
return autoImg;
95-
}
87+
const autoImgs = Array.from(
88+
doc.querySelectorAll("#quarto-document-content img"),
89+
);
90+
for (const autoImgN of autoImgs) {
91+
const autoImg = autoImgN as Element;
92+
const src = getDecodedAttribute(autoImg, "src");
93+
if (src && !src.startsWith("http:") && !src.startsWith("https:")) {
94+
return autoImg;
9695
}
9796
}
9897
}

src/project/types/website/website-meta.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export function metadataHtmlPostProcessor(
180180

181181
// find a preview image if one is not provided
182182
if (metadata[kImage] === undefined && format.metadata[kImage] !== false) {
183-
metadata[kImage] = findPreviewImg(doc, true) ||
183+
metadata[kImage] = findPreviewImg(doc) ||
184184
websiteImage(project.config);
185185
}
186186

@@ -249,7 +249,6 @@ function opengraphMetadata(
249249
if (siteMeta && siteMeta[kTitle]) {
250250
metadata[kSiteName] = siteMeta[kTitle];
251251
}
252-
253252
// Read open graph data in
254253
if (openGraph && typeof openGraph === "object") {
255254
[
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
_site/
2+
.quarto
3+
/.quarto/
4+
*jupyter_cache/
5+
*.jupyter_cache*
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
project:
2+
type: website
3+
4+
website:
5+
title: "today"
6+
open-graph: true
7+
page-footer:
8+
center: |
9+
Footer image:
10+
[![](/profile.jpg)](https://quarto.org/)
11+
navbar:
12+
left:
13+
- href: index.qmd
14+
text: Home
15+
- posts.qmd
16+
17+
format:
18+
html:
19+
theme: cosmo
20+
toc: true
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: "Home"
3+
listing:
4+
contents:
5+
- "posts.qmd"
6+
type: grid
7+
sort: false
8+
_quarto:
9+
render-project: true
10+
---
11+
12+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: "Listing Example"
3+
listing:
4+
contents: posts
5+
_quarto:
6+
render-project: true
7+
---
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
date: '2024-07-20T20:47:56-04:00'
3+
title: "Yes, opengraph image"
4+
_quarto:
5+
render-project: true
6+
tests:
7+
html:
8+
ensureFileRegexMatches:
9+
-
10+
- '\<meta property="og:image" content="index_files'
11+
- []
12+
---
13+
14+
Testing images.
15+
16+
```{python}
17+
# | label: fig-polar
18+
# | fig-cap: "A line plot on a polar axis"
19+
# | classes: preview-image
20+
21+
22+
import numpy as np
23+
import matplotlib.pyplot as plt
24+
25+
r = np.arange(0, 2, 0.01)
26+
theta = 2 * np.pi * r
27+
fig, ax = plt.subplots(
28+
subplot_kw = {'projection': 'polar'}
29+
)
30+
ax.plot(theta, r)
31+
ax.set_rticks([0.5, 1, 1.5, 2])
32+
ax.grid(True)
33+
plt.show()
34+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
date: '2024-07-20T20:47:56-04:00'
3+
title: "This has no ogimage"
4+
_quarto:
5+
render-project: true
6+
tests:
7+
html:
8+
ensureFileRegexMatches:
9+
-
10+
- '\<meta property="og:image" content="index_files'
11+
- []
12+
---
13+
14+
Testing images.
15+
16+
```{python}
17+
#| label: fig-polar
18+
#| fig-cap: "A line plot on a polar axis"
19+
20+
21+
import numpy as np
22+
import matplotlib.pyplot as plt
23+
24+
r = np.arange(0, 2, 0.01)
25+
theta = 2 * np.pi * r
26+
fig, ax = plt.subplots(
27+
subplot_kw = {'projection': 'polar'}
28+
)
29+
ax.plot(theta, r)
30+
ax.set_rticks([0.5, 1, 1.5, 2])
31+
ax.grid(True)
32+
plt.show()
33+
```

0 commit comments

Comments
 (0)