Skip to content

Commit 11333ce

Browse files
authored
Fix embedded source files not copied when rendering (#16)
* Register embedded files as Quarto resources automatically Adds ensureResource() to resolve local file paths relative to the input document and call quarto.doc.add_resource(), so referenced files are copied to the output directory without manual YAML config. * Update FAQ to note automatic resource handling * Add audio to FAQ resource example and link shortcode pages to troubleshooting * Handle empty paths and strip query/fragment from resource paths
1 parent ef2fc6d commit 11333ce

6 files changed

Lines changed: 67 additions & 8 deletions

File tree

_extensions/embedio/embedio.lua

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,34 @@ local function checkFile(input)
4242
end
4343
end
4444

45+
-- Check if a path is a remote reference (URL, data URI, or anchor)
46+
local function isRemoteRef(path)
47+
return path:find("^%a+://") ~= nil or
48+
path:find("^data:") ~= nil or
49+
path:find("^#") ~= nil
50+
end
51+
52+
-- Register a local file as a Quarto resource so it is copied to the output
53+
local function ensureResource(file_path)
54+
if isVariableEmpty(file_path) or isRemoteRef(file_path) then
55+
return
56+
end
57+
58+
-- Strip query strings and fragments (e.g. audio.mp3#t=10)
59+
local resource_path = file_path:match("^[^%?#]+") or file_path
60+
61+
-- Resolve to an absolute path based on the input document's directory
62+
local abs_path
63+
if pandoc.path.is_absolute(resource_path) then
64+
abs_path = resource_path
65+
else
66+
local doc_dir = pandoc.path.directory(quarto.doc.input_file)
67+
abs_path = pandoc.path.normalize(pandoc.path.join({doc_dir, resource_path}))
68+
end
69+
70+
quarto.doc.add_resource(abs_path)
71+
end
72+
4573
-- Avoid duplicate class definitions
4674
local initializedSlideCSS = false
4775

@@ -104,6 +132,9 @@ local function html(args, kwargs, meta, raw_args)
104132
local full_screen_link = getOption(kwargs, "full-screen-link", "true")
105133
local class = getOption(kwargs, "class", "")
106134

135+
-- Ensure file is copied to the output directory
136+
ensureResource(file_name)
137+
107138
-- Define the template for embedding HTML files
108139
local template_html = [[
109140
<div%s>
@@ -130,6 +161,9 @@ local function revealjs(args, kwargs, meta, raw_args)
130161
local full_screen_link = getOption(kwargs, "full-screen-link", "true")
131162
local class = getOption(kwargs, "class", "")
132163

164+
-- Ensure file is copied to the output directory
165+
ensureResource(file_name)
166+
133167
-- Define the template for embedding Reveal.js slides
134168
local template_revealjs = [[
135169
<div%s>
@@ -157,6 +191,9 @@ local function audio(args, kwargs, meta)
157191
local input = pandoc.utils.stringify(args[1] or kwargs.file)
158192
checkFile(input)
159193

194+
-- Ensure file is copied to the output directory
195+
ensureResource(input)
196+
160197
-- Add class attribute if provided
161198
if class then
162199
table.insert(htmlTable, 'class="' .. class .. '">')
@@ -208,7 +245,10 @@ local function pdf(args, kwargs, meta)
208245
-- Supported options for now
209246
local pdf_file_name = pandoc.utils.stringify(args[1] or kwargs["file"])
210247
checkFile(pdf_file_name)
211-
248+
249+
-- Ensure file is copied to the output directory
250+
ensureResource(pdf_file_name)
251+
212252
local height = getOption(kwargs, "height", "600px")
213253
local width = getOption(kwargs, "width", "100%")
214254
local download_link = getOption(kwargs, "download-link", "true")

docs/qembedio-embed-audio.qmd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ For example, the above shortcode embeds the `my-audio.mp3` in `assets/` as:
2121
| `class` | `""` | Specifies the class attribute for the figure element. |
2222
| `download-link` | `"false"` | Specifies whether to include a download link. |
2323

24-
You may also omit specifying a `file` option. We'll automatically use the first parameter as the `file`
24+
You may also omit specifying a `file` option. We'll automatically use the first parameter as the `file`
25+
26+
:::{.callout-tip}
27+
Having trouble with files not appearing in the rendered output? See the [troubleshooting section](qembedio-faq.qmd#im-encountering-issues-with-embedding-files.-what-should-i-do) of the FAQ.
28+
:::

docs/qembedio-embed-html.qmd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ For example, the above shortcode embeds the `my-html-page.html` in `assets/` as:
2222
| `full-screen-link` | "true" | Add a link to view the embedded webpage in a new full screen browser window. |
2323
| `class` | None | Specifies the classes of the container wrapping the embedded webpage. |
2424

25-
You may also omit specifying a `file` option. We'll automatically use the first parameter as the `file`
25+
You may also omit specifying a `file` option. We'll automatically use the first parameter as the `file`
26+
27+
:::{.callout-tip}
28+
Having trouble with files not appearing in the rendered output? See the [troubleshooting section](qembedio-faq.qmd#im-encountering-issues-with-embedding-files.-what-should-i-do) of the FAQ.
29+
:::

docs/qembedio-embed-pdf.qmd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ For example, the above shortcode embeds the `my-pdf-file.pdf` in `assets/` as:
2121
| `width` | "100%" | Specifies the width of the embedded PDF object. |
2222
| `download-link` | "true" | Add a link to download the embedded PDF in a new browser window. |
2323

24-
You may also omit specifying a `file` option. We'll automatically use the first parameter as the `file`
24+
You may also omit specifying a `file` option. We'll automatically use the first parameter as the `file`
25+
26+
:::{.callout-tip}
27+
Having trouble with files not appearing in the rendered output? See the [troubleshooting section](qembedio-faq.qmd#im-encountering-issues-with-embedding-files.-what-should-i-do) of the FAQ.
28+
:::

docs/qembedio-embed-revealjs.qmd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ For example, the above shortcode embeds the `my-revealjs-slides.html` in `assets
2222
| `full-screen-link` | "true" | Add a link to view the embedded slide deck in a new full screen browser window. |
2323
| `class` | None | Specifies the classes of the container wrapping the embedded slide deck. |
2424

25-
You may also omit specifying a `file` option. We'll automatically use the first parameter as the `file`
25+
You may also omit specifying a `file` option. We'll automatically use the first parameter as the `file`
26+
27+
:::{.callout-tip}
28+
Having trouble with files not appearing in the rendered output? See the [troubleshooting section](qembedio-faq.qmd#im-encountering-issues-with-embedding-files.-what-should-i-do) of the FAQ.
29+
:::

docs/qembedio-faq.qmd

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,18 @@ For more details, please see [Demo: RevealJS](qembedio-embed-revealjs.qmd).
5252

5353
If you're experiencing difficulties with file embedding, first ensure that your Quarto project has the `embedio` shortcode extension installed by looking in the `_extensions` folder.
5454

55-
Next, make sure that the resource is specified in the `resources` key under the publishing format:
55+
Starting with v0.0.3, the `embedio` shortcodes automatically register embedded files as Quarto resources, so they are copied to the output directory without any extra configuration.
56+
57+
If you are using an older version of the extension (or need to include files that are _not_ referenced by a shortcode), you can manually declare them in the `resources` key under the publishing format:
5658

5759
```yaml
5860
---
5961
title: Example resource inclusion
6062
format:
61-
html:
62-
resources:
63+
html:
64+
resources:
6365
- path/to/file.pdf
66+
- path/to/audio.mp3
6467
---
6568
```
6669

0 commit comments

Comments
 (0)