Rails atom_feed:如何在提要内容中添加 HTML 标签,例如 <h1>?
Rails atom_feed: How to add HTML tags like <h1> to the feed content?
我想使用标题来构建我的 Atom 提要的内容:
atom_feed do |feed|
feed.title "#{Page.model_name.human(count: :other)} - #{t('app.name')}"
feed.updated(@pages[0].created_at) if @pages.length > 0
@pages.each do |page|
feed.entry(page) do |entry|
entry.title(page.title)
entry.content(type: 'html') do |html|
html.h1 t('.position_in_hierarchy')
html.p page.ancestors.reverse.map(&:title).join ': '
html.h1 Page.human_attribute_name :lead
html.div markdown page.lead
html.h1 Page.human_attribute_name :content
html.div markdown page.content
end
end
end
end
对于我的每个页面,这会生成以下 XML:
<entry>
<id>tag:localhost,2005:PageDecorator/1</id>
<published>2017-02-24T18:11:26+01:00</published>
<updated>2017-04-05T09:58:42+02:00</updated>
<link rel="alternate" type="text/html" href="http://localhost:3001/en/pages/1"/>
<title>Preparation</title>
<content type="html">
<h1>Position in page hierarchy</h1>
<p>Preparation</p>
<h1>Lead</h1>
<div><p>This the lead.</p></div>
<h1>Content</h1>
<div><p>This is some content!</p>
<h2 id="with-a-heading">With a heading</h2>
<p><a href="http://www.example.com">And a link!</a></p></div>
<h1>Notice about Atom feed</h1>
<p>Your feed reader has downloaded version 14 of the current page, which was current at April 05, 2017 09:58. Meanwhile there could be an updated version of the page available online. Visit the original page to see the most current version!</p>
</content>
</entry>
如您所见,html.h1
调用呈现为普通 <h1>
。但有趣的是,我的 feed reader Vienna 没有显示它们!更有趣的是,我自定义的markdown
方法渲染的markdown中的html.div
内容被html转义了……而这东西是Vienna渲染的!
markdown
方法本身转义HTML:
def markdown(string)
PandocRuby.convert(string, to: :html).html_safe
end
所以我现在非常不确定如何才能使我的其他 HTML 标记在维也纳也能正常工作。
正如 Kris 在他的评论中指出的那样,需要对内容进行转义:https://validator.w3.org/feed/docs/atom.html#text
最简单的方法就是像这样渲染局部:
entry.content(render(page), type: 'html')
这将呈现 _page.atom.slim
(或 .erb
或其他)并自动转义 HTML。
我想使用标题来构建我的 Atom 提要的内容:
atom_feed do |feed|
feed.title "#{Page.model_name.human(count: :other)} - #{t('app.name')}"
feed.updated(@pages[0].created_at) if @pages.length > 0
@pages.each do |page|
feed.entry(page) do |entry|
entry.title(page.title)
entry.content(type: 'html') do |html|
html.h1 t('.position_in_hierarchy')
html.p page.ancestors.reverse.map(&:title).join ': '
html.h1 Page.human_attribute_name :lead
html.div markdown page.lead
html.h1 Page.human_attribute_name :content
html.div markdown page.content
end
end
end
end
对于我的每个页面,这会生成以下 XML:
<entry>
<id>tag:localhost,2005:PageDecorator/1</id>
<published>2017-02-24T18:11:26+01:00</published>
<updated>2017-04-05T09:58:42+02:00</updated>
<link rel="alternate" type="text/html" href="http://localhost:3001/en/pages/1"/>
<title>Preparation</title>
<content type="html">
<h1>Position in page hierarchy</h1>
<p>Preparation</p>
<h1>Lead</h1>
<div><p>This the lead.</p></div>
<h1>Content</h1>
<div><p>This is some content!</p>
<h2 id="with-a-heading">With a heading</h2>
<p><a href="http://www.example.com">And a link!</a></p></div>
<h1>Notice about Atom feed</h1>
<p>Your feed reader has downloaded version 14 of the current page, which was current at April 05, 2017 09:58. Meanwhile there could be an updated version of the page available online. Visit the original page to see the most current version!</p>
</content>
</entry>
如您所见,html.h1
调用呈现为普通 <h1>
。但有趣的是,我的 feed reader Vienna 没有显示它们!更有趣的是,我自定义的markdown
方法渲染的markdown中的html.div
内容被html转义了……而这东西是Vienna渲染的!
markdown
方法本身转义HTML:
def markdown(string)
PandocRuby.convert(string, to: :html).html_safe
end
所以我现在非常不确定如何才能使我的其他 HTML 标记在维也纳也能正常工作。
正如 Kris 在他的评论中指出的那样,需要对内容进行转义:https://validator.w3.org/feed/docs/atom.html#text
最简单的方法就是像这样渲染局部:
entry.content(render(page), type: 'html')
这将呈现 _page.atom.slim
(或 .erb
或其他)并自动转义 HTML。