node.js - 后端 (NodeJS) - Adaptivecard JSON 到 HTML 解析器
问题描述
我想要达到什么目的?
- 一个后端 NodeJS 服务,它将接收自适应卡片 JSON 请求并给出解析后的 HTML 响应。
为什么我要写这个服务?
- 需要向用户发送电子邮件,当我们已经拥有大量可重复使用的自适应卡片 JSON 模板时,我不想为电子邮件编写单独的 HTML 模板。
我为后端服务尝试了什么?
- 创建了一个nodejs服务
- 安装的节点包:npm install Adaptivecards
- 编写了一个示例程序,将 JSON 自适应卡片解析为 HTML。
我遇到的问题:
我遇到的第一个问题:render()函数报告没有document、window和HTMLElement。
为了解决上述问题,我安装了jsdom并创建了一个 DOM 对象并分配给global:
var jsdom = require("jsdom"); var JSDOM = jsdom.JSDOM; var html = `<!DOCTYPE html> <html> <body id="cardAdaptive"></body> </html> `; const DOM = new JSDOM(html); global.window = DOM.window; global.document = DOM.window.document; global.HTMLElement = DOM.window.HTMLElement;
主要问题:程序运行良好,没有任何问题,但解析的 HTML 不完整。请参考下面附上的图片。
解析器程序(parser.js):
var jsdom = require("jsdom");
var JSDOM = jsdom.JSDOM;
var html = `<!DOCTYPE html>
<html>
<body id="cardAdaptive"></body>
</html>
`;
const DOM = new JSDOM(html);
global.window = DOM.window;
global.document = DOM.window.document;
global.HTMLElement = DOM.window.HTMLElement;
var AdaptiveCards = require("adaptivecards");
var card = {
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "Image",
"url": "http://adaptivecards.io/content/adaptive-card-50.png"
},
{
"type": "TextBlock",
"text": "Hello **Adaptive Cards!**"
}
],
"actions": [
{
"type": "Action.OpenUrl",
"title": "Learn more",
"url": "http://adaptivecards.io"
},
{
"type": "Action.OpenUrl",
"title": "GitHub",
"url": "http://github.com/Microsoft/AdaptiveCards"
}
]
};
// Create an AdaptiveCard instance
var adaptiveCard = new AdaptiveCards.AdaptiveCard();
// Set its hostConfig property unless you want to use the default Host Config
// Host Config defines the style and behavior of a card
adaptiveCard.hostConfig = new AdaptiveCards.HostConfig({
fontFamily: "Segoe UI, Helvetica Neue, sans-serif"
// More host config options
});
// Parse the card payload
adaptiveCard.parse(card);
// Render the card to an HTML element:
var renderedCard = adaptiveCard.render();
// And finally insert it somewhere in your page:
DOM.window.document.getElementById("cardAdaptive").appendChild(renderedCard);
// Print HTML string
console.log(DOM.serialize());
程序输出(HTML):
<!DOCTYPE html>
<html>
<head></head>
<body id="cardAdaptive">
<div class="ac-container ac-adaptiveCard"
style="display: flex; flex-direction: column; justify-content: flex-start; box-sizing: border-box; flex: 0 0 auto; padding: 15px 15px 15px 15px; margin: 0px 0px 0px 0px;"
tabindex="0">
<div
style="display: flex; align-items: flex-start; justify-content: flex-start; box-sizing: border-box; flex: 0 0 auto;">
<img style="max-height: 100%; min-width: 0; max-width: 100%;" class="ac-image"
src="http://adaptivecards.io/content/adaptive-card-50.png"></div>
<div class="ac-horizontal-separator"
style="height: 8px; overflow: hidden; margin-right: 0px; margin-left: 0px; flex: 0 0 auto;"></div>
<div class="ac-textBlock"
style="overflow: hidden; font-family: Segoe UI, Helvetica Neue, sans-serif; font-size: 14px; color: rgb(51, 51, 51); font-weight: 400; text-align: left; line-height: 18.62px; white-space: nowrap; text-overflow: ellipsis; box-sizing: border-box; flex: 0 0 auto;">
</div>
<div class="ac-horizontal-separator" style="height: 8px; overflow: hidden;"></div>
<div>
<div style="overflow: hidden;">
<div class="ac-actionSet" style="display: flex; flex-direction: row; justify-content: flex-start;">
<button aria-label="Learn more" type="button"
style="display: flex; align-items: center; justify-content: center; flex: 0 1 auto;" role="link"
class="ac-pushButton style-default">
<div style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"></div>
</button>
<div style="flex: 0 0 auto; width: 20px;"></div><button aria-label="GitHub" type="button"
style="display: flex; align-items: center; justify-content: center; flex: 0 1 auto;" role="link"
class="ac-pushButton style-default">
<div style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"></div>
</button>
</div>
</div>
<div></div>
</div>
</div>
</body>
</html>
解析后的 HTML 如下所示:
- 缺少文本字段和内容。
但预期是这样的:
更新
我尝试设置此stackoverflow 答案中提到的 hostConfig 以及此 github中提到的所有示例主机配置。他们都没有工作。看起来有文本的字段总是丢失。现在我不知道如何使它工作。
我对这个程序的规格:
- Ubuntu 18.04.4 LTS
- 节点:v12.18.1
- “自适应卡”:“^1.2.6”,
- "jsdom": "^16.2.2",
参考链接:
解决方案
推荐阅读
- javascript - Javascript添加系统有问题(简单但我很慢)
- javascript - 使用 reactJS 显示动态数组列表
- r - 如何通过 R 中的循环将数据框列中的特定值替换为其他特定值
- python - 没有得到预期的输出 - python 类
- javascript - 如何在浏览器控制台中访问 Vue-CLI 中的 Vue 数据
- javascript - TypeError:未定义不是对象(评估'object ['body']')
- c# - 将 ListBox 中的所有内容保存到 .txt 文件
- symfony - symfony5 - composer udpate 后不工作(缓存问题)
- asp.net-mvc-5 - 强制 IIS 不模拟用户
- rust - 更改静态字符串值