Skip to content

Commit bb4585d

Browse files
committed
add email-version hook to override detected Connect version
1 parent d46afbf commit bb4585d

5 files changed

Lines changed: 89 additions & 5 deletions

File tree

src/resources/filters/modules/connectversion.lua

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ Connect version sniffing utilities for email extension
44
Functions to detect and compare Posit Connect versions from environment variables
55
]]
66

7+
-- Get email format version override from metadata
8+
-- Can be set in YAML as:
9+
-- format:
10+
-- email:
11+
-- email-version: 2
12+
-- Returns: version string (e.g., "2") if override is set, nil otherwise
13+
function get_email_format_override(meta)
14+
if meta and meta["email-version"] then
15+
return pandoc.utils.stringify(meta["email-version"])
16+
end
17+
18+
return nil
19+
end
20+
721
-- Parse Connect version from SPARK_CONNECT_USER_AGENT
822
-- Format: posit-connect/2024.09.0
923
-- posit-connect/2024.09.0-dev+26-dirty-g51b853f70e
@@ -70,5 +84,6 @@ end
7084

7185
-- Export functions for module usage
7286
return {
73-
is_connect_version_at_least = is_connect_version_at_least
87+
is_connect_version_at_least = is_connect_version_at_least,
88+
get_email_format_override = get_email_format_override
7489
}

src/resources/filters/quarto-post/email.lua

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,22 @@ function process_document(doc)
347347
return doc
348348
end
349349

350-
-- Detect format upfront: Check Connect version and look for document-level metadata
351-
-- This must be determined before processing to avoid confusion about which format to use
352-
if connectversion.is_connect_version_at_least(constants.kConnectEmailMetadataChangeVersion) then
353-
connect_supports_v2 = true
350+
-- Check for explicit email format override first
351+
local format_override = connectversion.get_email_format_override(doc.meta)
352+
if format_override then
353+
-- If override is set, interpret the value as a string
354+
if format_override == "2" then
355+
connect_supports_v2 = true
356+
io.stderr:write("WARNING: Email format v2 is being forced via 'format.email.version: " .. format_override .. "' in YAML. This overrides the Connect version detection.\n")
357+
else
358+
connect_supports_v2 = false
359+
io.stderr:write("WARNING: Email format v1 is being forced via 'format.email.version: " .. format_override .. "' in YAML. This overrides the Connect version detection.\n")
360+
end
361+
else
362+
-- Fall back to version sniffing if no explicit override
363+
if connectversion.is_connect_version_at_least(constants.kConnectEmailMetadataChangeVersion) then
364+
connect_supports_v2 = true
365+
end
354366
end
355367

356368
-- Scan for document-level metadata at the TOP LEVEL of the document
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
- name: email-version
2+
tags:
3+
formats: [email]
4+
schema:
5+
enum: [1, 2]
6+
description:
7+
short: "Email format version"
8+
long: |
9+
Specifies which email format version to use.
10+
11+
- `1`: Legacy email format with document-level metadata (compatible with older Connect versions)
12+
- `2`: New email format with multiple individual emails and v2 markers (requires Posit Connect 2026.03 or later)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: Email Test Document
3+
author: Charles Teague
4+
format:
5+
email:
6+
email-version: 2
7+
---
8+
9+
The report content. Anything that is here is not part of the email message.
10+
11+
::: {.email}
12+
13+
::: {.subject}
14+
The subject line.
15+
:::
16+
17+
::: {.email-text}
18+
An optional text-only version of the email message..
19+
:::
20+
21+
The HTML email content. Here you can add code cells, produce images, and write accompanying text.
22+
This content is not seen when viewing the rendered document on Connect (it's only
23+
seen when sending an email from the Connect document page). Emails from Connect
24+
can be sent manually, and they can also be scheduled.
25+
26+
:::
27+
28+
Any additional report content not part of the email message.

tests/smoke/render/render-email.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,23 @@ testRender(docs("email/email-multi-v2.qmd"), "email", false, [
183183
}
184184
});
185185

186+
// Test v2 format override with old Connect version
187+
// Uses email-format: v2 in YAML to force v2 despite old Connect version
188+
testRender(docs("email/email-force-v2.qmd"), "email", false, [fileExists(previewFileV2_1), validJsonWithMultipleEmails(jsonFile, 1, {
189+
"0": {
190+
"email_id": 1,
191+
"subject": "The subject line.",
192+
"attachments": [],
193+
"suppress_scheduled": false,
194+
"send_report_as_attachment": false
195+
}
196+
})], {
197+
...cleanupCtx,
198+
env: {
199+
"SPARK_CONNECT_USER_AGENT": "posit-connect/2024.09.0"
200+
}
201+
});
202+
186203
// Test mixed metadata - some emails have metadata, others don't
187204
testRender(docs("email/email-mixed-metadata-v2.qmd"), "email", false, [
188205
fileExists(previewFileV2_1),

0 commit comments

Comments
 (0)