Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[draft] make embedded video in html cards work in email #1398

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions packages/kg-default-nodes/lib/nodes/html/html-renderer.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
import {addCreateDocumentOption} from '../../utils/add-create-document-option';
import {renderEmptyContainer} from '../../utils/render-empty-container';

import * as cheerio from 'cheerio';

function rewriteVideoEmbeds(html, postUrl) {
try {
// quick check that there's a video tag in the html
const videoTagRegex = /<video.*?>/g;
if (!videoTagRegex.test(html)) {
return html;
}
const $ = cheerio.load(html);
// check the html for one or more video tags
const videoTags = $('video');
if (videoTags.length === 0 || !postUrl) {
return html;
}
// loop over video tags, looking for any with the poster attribute
videoTags.each((index, videoTag) => {
const $videoTag = $(videoTag);
const poster = $videoTag.attr('poster');
// if a poster attribute is found, replace the video with a linked image
if (poster) {
const title = $videoTag.attr('title') || 'Click to play video';
$videoTag.replaceWith(`<a href="${postUrl}"><img src="${poster}" title="${title}" alt="${title}" /></a>`);
}
});
return $.html();
} catch (err) {
return html;
}
}

export function renderHtmlNode(node, options = {}) {
addCreateDocumentOption(options);
const document = options.createDocument();

const html = node.html;
let html = node.html;

if (!html) {
return renderEmptyContainer(document);
}

if (options.target === 'email') {
html = rewriteVideoEmbeds(html, options.postUrl);
}
const wrappedHtml = `\n<!--kg-card-begin: html-->\n${html}\n<!--kg-card-end: html-->\n`;

const textarea = document.createElement('textarea');
Expand Down
1 change: 1 addition & 0 deletions packages/kg-default-nodes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@lexical/utils": "0.13.1",
"@tryghost/kg-clean-basic-html": "4.1.4",
"@tryghost/kg-markdown-html-renderer": "7.0.7",
"cheerio": "^1.0.0",
"html-minifier": "^4.0.0",
"jsdom": "^24.1.0",
"lexical": "0.13.1",
Expand Down
Loading