我在使用Facebook Instant Articles SDK Transformer从Image标签中提取属性文本时遇到问题
我无法弄清楚rules.json需要从alt属性中提取文本并从中制作标题。
//MARKUP
<img src="https://upload.wikimedia.org/wikipedia/commons/8/84/Example.svg" alt="Foto By: Bla Bla"/>
//RULES.JSON
{
"class": "ImageRule",
"selector" : "img",
"properties" :
{
"image.url" :
{
"type" : "string",
"selector" : "img",
"attribute": "src"
},
"image.caption" :
{
"type" : "string",
"selector" : "img",
"attribute" : "alt"
}
}
}
Expected results are Facebook Instant Article compliant markup like:
<figure>
<img src="https://upload.wikimedia.org/wikipedia/commons/8/84/Example.svg"/>
<figcaption>Foto By: Bla Bla</figcaption>
</figure>
我得到的是Uncaught Error:在第305行的/Facebook/InstantArticles/Transformer/Transformer.php中调用字符串上的成员函数hasChildNodes()。
不知何故图像被处理,标题被处理,我得到正确的值,但然后它递归地再次进入转换函数传递提取的“alt”字符串,它失败,因为它期望HTML节点输入不是字符串。
关于此事的Facebook文件非常模糊,所以如果有人有处理Facebook即时文章的经验,请加入。
可以在这里找到shitty docs: https://developers.facebook.com/docs/instant-articles/sdk/transformer/ https://developers.facebook.com/docs/instant-articles/sdk/transformer-rules
这里是SDK的主要提交者。
您可以检查我们在SimpleTransformerTest.php中的设置,它完全涵盖您的需求。您还可以使用任何测试来使用Transformer。
你做错了是image.caption
的选择器应该是一种element
。
对于您的Rules.json,它应该如下所示:
{
"class": "CaptionRule",
"selector" : "//img[@alt]",
"properties" : {
"caption.default": {
"type": "string",
"selector": "img",
"attribute": "alt"
}
}
},
{
"class": "ImageRule",
"selector" : "figure",
"properties" :
{
"image.url" :
{
"type" : "string",
"selector" : "img",
"attribute": "src"
},
"image.caption" :
{
"type" : "element",
"selector" : "img"
}
}
}
检查我是否使用不同的策略,而不是直接到ImageRule上的<img>
元素,我选择了<figure>
标签,这样我们就可以保持变压器完好无损。请注意,rules.json是自下而上应用的。
如果这符合您的需求,请告诉我。