Skip to content

Commit 77232b2

Browse files
committed
feature: auto-discovery of images in markdown (see #4744)
1 parent 48aedff commit 77232b2

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ export function findPreviewImgEl(doc: Document): Element | undefined {
7979
return img;
8080
}
8181
}
82+
83+
// as a last resort, just use the auto-discovered image from the lua
84+
// filter chain, if it exists
85+
const autoImg = doc.querySelector("img.quarto-discovered-preview-image");
86+
if (autoImg) {
87+
return autoImg;
88+
}
8289
}
8390

8491
// The general words per minute that we should use.

src/resources/filters/main.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ import("./quarto-pre/table-captions.lua")
148148
import("./quarto-pre/table-colwidth.lua")
149149
import("./quarto-pre/table-rawhtml.lua")
150150
import("./quarto-pre/theorems.lua")
151+
import("./quarto-pre/discover_preview_images.lua")
151152

152153
import("./customnodes/decoratedcodeblock.lua")
153154

@@ -218,7 +219,8 @@ local quartoPre = {
218219
}) },
219220
{ name = "pre-quartoPreMetaInject", filter = quartoPreMetaInject() },
220221
{ name = "pre-writeResults", filter = writeResults() },
221-
{ name = "pre-projectPaths", filter = projectPaths()},
222+
{ name = "pre-projectPaths", filter = projectPaths() },
223+
{ name = "pre-discover_preview_images", filter = discover_preview_images() }
222224
}
223225

224226
local quartoPost = {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- discover_preview_images.lua
2+
-- Copyright (C) 2023 Posit Software, PBC
3+
4+
local set = false
5+
function discover_preview_images()
6+
return {
7+
Image = function(el)
8+
if set then
9+
return nil
10+
end
11+
set = true
12+
el.classes:insert("quarto-discovered-preview-image")
13+
return el
14+
end
15+
}
16+
end

0 commit comments

Comments
 (0)