amp-html - 是否可以从 amp-list 中的 JSON 呈现 AMP 组件?
问题描述
我正在创建一个 ASP.NET MVC 应用程序来转换来自内容管理系统的内容并将其呈现为符合 AMP 标准的页面。CMS 有一个称为列表的概念,它将特定模板的内容项组合在一起,每个内容项都可以具有不同类型的自定义字段(纯文本、布尔值、HTML 等)。
amp-list 组件允许您使用三重花括号而不是双花括号来呈现原始 HTML,例如{{{htmlProperty}}}
. 这通常有效,但我遇到了剥离图像的问题,即使我在服务器端修改它们以使用正确的 amp-img 语法。
我有以下 amp-list 组件:
<amp-list layout="fixed-height" height="500" src="https://jsonblob.com/api/jsonBlob/56eb207e-e2c3-11e8-a8cb-676415a24d50" single-item>
<template type="amp-mustache">
{{#contentItems}}
<section>
<header>
{{title}}
</header>
<div>
{{{content}}}
</div>
</section>
{{/contentItems}}
</template>
</amp-list>
我有以下示例 JSON:
{
"totalRecords": 2,
"pageSize": 0,
"pageNumber": 1,
"items": {
"contentItems": [
{
"row": 1,
"title": "What is your favorite sport?",
"content": "<p><strong>Hockey</strong></p>"
},
{
"row": 2,
"title": "What is your favorite country?",
"content": "<p>Canada</p><amp-img src=\"http://www.flagblvd.com/wp-content/uploads/2015/09/canadian-flag-medium.png\" alt=\"\" layout=\"fill\"></amp-img>"
}
]
}
}
请注意,该title
属性是纯文本,content
而是 HTML。加载页面时,第<strong>
一个内容项中标记的粗体起作用,但如果您在浏览器的开发人员控制台中进行检查,您会注意到输出中省略了 amp-img。请参阅此jsfiddle进行演示。
有没有办法让它正常工作?我在官方文档中没有看到任何关于此的内容。我什至只是在没有相应文档的示例中看到使用三重大括号来呈现原始 HTML 时才发现它。
解决方案
"triple-mustache" 的输出被清理为只允许以下标签:a
, b
, br
, caption
, colgroup
, code
, del
, div
, em
, i
, ins
, li
, mark
, ol
, p
, q
, s
, small
, span
, , strong
, sub
, sup
, table
, tbody
, time
, td
, th
, thead
, tfoot
, tr
, .u
ul
推荐阅读
- regex-group - 如何创建可选的命名组?
- rust - 用serde反序列化成不同的特征实现者?
- android - 如何在干净的架构中实现购物车?
- eslint - 无法关闭 eslint no-unused-expressions
- python - Matplotlib - 为什么图形会被保存两次?
- localization - 嵌入式脚本语言的编译时本地化
- javascript - 当我声明一个函数的属性时,我在哪里存储一个值?
- ios - 在 iOS 中流式传输谷歌照片视频不起作用(在 Safari 中也不起作用)
- css - 在两行文本后添加省略号和工具提示 - React
- python - 如何使用 pymunk 的碰撞处理程序调用函数?