diff --git a/news/changelog-1.6.md b/news/changelog-1.6.md
index d7eca33aac7..da667291a90 100644
--- a/news/changelog-1.6.md
+++ b/news/changelog-1.6.md
@@ -86,6 +86,7 @@ All changes included in 1.6:
- ([#2671](https://github.com/quarto-dev/quarto-cli/issues/2671)): Ensure that `--output-dir` works across filesystem boundaries.
- ([#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.
- ([#8932](https://github.com/quarto-dev/quarto-cli/issues/8932)): Escape render ids in markdown pipeline to allow special characters in sidebars/navbars, etc.
+- ([#10311](https://github.com/quarto-dev/quarto-cli/issues/10311)): Loosen auto-discovery of images for OpenGraph cards.
- ([#10567](https://github.com/quarto-dev/quarto-cli/issues/10567)): Generate breadcrumbs correctly for documents using a level-1 heading as the title.
- ([#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.
diff --git a/src/project/types/website/listing/website-listing-shared.ts b/src/project/types/website/listing/website-listing-shared.ts
index 35e90128848..a4fbbf8d7b0 100644
--- a/src/project/types/website/listing/website-listing-shared.ts
+++ b/src/project/types/website/listing/website-listing-shared.ts
@@ -564,7 +564,7 @@ export function readRenderedContents(
// Find a preview image, if present
const computePreviewImage = (): PreviewImage | undefined => {
- const previewImageEl = findPreviewImgEl(doc, false);
+ const previewImageEl = findPreviewImgEl(doc);
if (previewImageEl) {
const previewImageSrc = getDecodedAttribute(previewImageEl, "src");
if (previewImageSrc !== null) {
diff --git a/src/project/types/website/util/discover-meta.ts b/src/project/types/website/util/discover-meta.ts
index 2b67059cfef..0b9362b66aa 100644
--- a/src/project/types/website/util/discover-meta.ts
+++ b/src/project/types/website/util/discover-meta.ts
@@ -41,9 +41,8 @@ export function findDescription(doc: Document): string | undefined {
export function findPreviewImg(
doc: Document,
- strict: boolean,
): string | undefined {
- const imgEl = findPreviewImgEl(doc, strict);
+ const imgEl = findPreviewImgEl(doc);
if (imgEl) {
const src = getDecodedAttribute(imgEl, "src");
if (src !== null) {
@@ -58,7 +57,6 @@ export function findPreviewImg(
export function findPreviewImgEl(
doc: Document,
- strict: boolean,
): Element | undefined {
// look for an image explicitly marked as the preview image (.class .preview-image)
const match = doc.querySelector(`img.${kPreviewImgClass}`);
@@ -86,13 +84,14 @@ export function findPreviewImgEl(
}
// as a last resort, just use the first _local_ image found within the document body
- if (!strict) {
- const autoImg = doc.querySelector("#quarto-document-content img");
- if (autoImg) {
- const src = autoImg.getAttribute("src");
- if (src && !src.startsWith("http:") && !src.startsWith("https:")) {
- return autoImg;
- }
+ const autoImgs = Array.from(
+ doc.querySelectorAll("#quarto-document-content img"),
+ );
+ for (const autoImgN of autoImgs) {
+ const autoImg = autoImgN as Element;
+ const src = getDecodedAttribute(autoImg, "src");
+ if (src && !src.startsWith("http:") && !src.startsWith("https:")) {
+ return autoImg;
}
}
}
diff --git a/src/project/types/website/website-meta.ts b/src/project/types/website/website-meta.ts
index 4a924d4afd3..72976545089 100644
--- a/src/project/types/website/website-meta.ts
+++ b/src/project/types/website/website-meta.ts
@@ -180,7 +180,7 @@ export function metadataHtmlPostProcessor(
// find a preview image if one is not provided
if (metadata[kImage] === undefined && format.metadata[kImage] !== false) {
- metadata[kImage] = findPreviewImg(doc, true) ||
+ metadata[kImage] = findPreviewImg(doc) ||
websiteImage(project.config);
}
@@ -249,7 +249,6 @@ function opengraphMetadata(
if (siteMeta && siteMeta[kTitle]) {
metadata[kSiteName] = siteMeta[kTitle];
}
-
// Read open graph data in
if (openGraph && typeof openGraph === "object") {
[
diff --git a/tests/docs/smoke-all/2024/10/28/issue-10311/.gitignore b/tests/docs/smoke-all/2024/10/28/issue-10311/.gitignore
new file mode 100644
index 00000000000..460a87081a5
--- /dev/null
+++ b/tests/docs/smoke-all/2024/10/28/issue-10311/.gitignore
@@ -0,0 +1,5 @@
+_site/
+.quarto
+/.quarto/
+*jupyter_cache/
+*.jupyter_cache*
\ No newline at end of file
diff --git a/tests/docs/smoke-all/2024/10/28/issue-10311/_quarto.yml b/tests/docs/smoke-all/2024/10/28/issue-10311/_quarto.yml
new file mode 100644
index 00000000000..e8d96d54aa9
--- /dev/null
+++ b/tests/docs/smoke-all/2024/10/28/issue-10311/_quarto.yml
@@ -0,0 +1,20 @@
+project:
+ type: website
+
+website:
+ title: "today"
+ open-graph: true
+ page-footer:
+ center: |
+ Footer image:
+ [](https://quarto.org/)
+ navbar:
+ left:
+ - href: index.qmd
+ text: Home
+ - posts.qmd
+
+format:
+ html:
+ theme: cosmo
+ toc: true
\ No newline at end of file
diff --git a/tests/docs/smoke-all/2024/10/28/issue-10311/index.qmd b/tests/docs/smoke-all/2024/10/28/issue-10311/index.qmd
new file mode 100644
index 00000000000..8e746bd25a9
--- /dev/null
+++ b/tests/docs/smoke-all/2024/10/28/issue-10311/index.qmd
@@ -0,0 +1,12 @@
+---
+title: "Home"
+listing:
+ contents:
+ - "posts.qmd"
+ type: grid
+ sort: false
+_quarto:
+ render-project: true
+---
+
+
diff --git a/tests/docs/smoke-all/2024/10/28/issue-10311/posts.qmd b/tests/docs/smoke-all/2024/10/28/issue-10311/posts.qmd
new file mode 100644
index 00000000000..02c06061f4a
--- /dev/null
+++ b/tests/docs/smoke-all/2024/10/28/issue-10311/posts.qmd
@@ -0,0 +1,7 @@
+---
+title: "Listing Example"
+listing:
+ contents: posts
+_quarto:
+ render-project: true
+---
\ No newline at end of file
diff --git a/tests/docs/smoke-all/2024/10/28/issue-10311/posts/a-test-post-og/index.qmd b/tests/docs/smoke-all/2024/10/28/issue-10311/posts/a-test-post-og/index.qmd
new file mode 100644
index 00000000000..c57e4af4c96
--- /dev/null
+++ b/tests/docs/smoke-all/2024/10/28/issue-10311/posts/a-test-post-og/index.qmd
@@ -0,0 +1,34 @@
+---
+date: '2024-07-20T20:47:56-04:00'
+title: "Yes, opengraph image"
+_quarto:
+ render-project: true
+ tests:
+ html:
+ ensureFileRegexMatches:
+ -
+ - '\'
+ - []
+---
+
+This is a Quarto website.
+
+To learn more about Quarto websites visit .
diff --git a/tests/docs/smoke-all/2024/10/28/issue-5625/styles.css b/tests/docs/smoke-all/2024/10/28/issue-5625/styles.css
new file mode 100644
index 00000000000..2ddf50c7b42
--- /dev/null
+++ b/tests/docs/smoke-all/2024/10/28/issue-5625/styles.css
@@ -0,0 +1 @@
+/* css styles */