node-red中的模板节点将链接作为编码的HTML实体返回

问题描述 投票:1回答:1

我正在尝试在节点红色中使用rss解析器并将输入作为格式化消息转发到松弛:

example using reddit news rss feed

节点红色导出:

[{"id":"f6ecdf35.256ab","type":"template","z":"1573541c.6cc74c","name":"Blog post","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{article.author}} has published an article: *{{article.title}}*\n{{article.link}}","output":"str","x":580,"y":640,"wires":[["ffcd3009.7a20c","799b343a.f89a2c"]]},{"id":"4f9293db.e8614c","type":"feedparse","z":"1573541c.6cc74c","name":"reddit news","url":"http://www.reddit.com/r/news/.rss","interval":"60","x":390,"y":640,"wires":[["f6ecdf35.256ab"]]},{"id":"ffcd3009.7a20c","type":"slack","z":"1573541c.6cc74c","name":"","channelURL":"https://hooks.slack.com/services/REMOVED","username":"rss bot","emojiIcon":":robot_face:","channel":"test","x":750,"y":640,"wires":[]},{"id":"799b343a.f89a2c","type":"debug","z":"1573541c.6cc74c","name":"","active":true,"console":"false","complete":"true","x":750,"y":700,"wires":[]}]

模板看起来像:

{{article.author}} has published an article: *{{article.title}}*
{{article.link}}

我期待得到:

/u/casac8 has published an article: *Guy who left manure at Treasury Secretary Mnuchin's house has come forward, cites First Amendment*
https://www.reddit.com/r/news/comments/7m0ag1/guy_who_left_manure_at_treasury_secretary/

但模板返回:

/u/casac8 has published an article: *Guy who left manure at Treasury Secretary Mnuchin's house has come forward, cites First Amendment*
https://www.reddit.com/r/news/comments/7m0ag1/guy_who_left_manure_at_treasury_secretary/

如何让模板节点返回未转义的utf8字符串?

解决方法

我现在的解决方法是将其转换回来的函数节点:

function parseHtmlEntities(str) {
    return str.replace(/&#x([0-9a-f]{1,3});/gi, function(match, numStr) {
        var num = parseInt(numStr, 16);
        return String.fromCharCode(num);
    });
}
msg.payload = parseHtmlEntities(msg.payload);
return msg;
node-red
1个回答
1
投票

如模板节点的侧边栏帮助中所述,为避免转义html实体,您应该使用三重括号:

{{{article.author}}}
© www.soinside.com 2019 - 2024. All rights reserved.