0

我想在 Rails 应用程序中显示用户的活动提要。我正在使用 feedjira。

2.2.4 :006 > xml_feed = Feedjira::Feed.fetch_raw "https://github.com/prasadsurase.atom"
2.2.4 :006 > github_feed = Feedjira::Feed.parse xml_feed
2.2.4 :006 > github_feed.entries.first.content
=> "<!-- pull_request -->\n<svg aria-label=\"Pull request\" class=\"octicon octicon-git-pull-request dashboard-event-icon\" height=\"32\" role=\"img\" version=\"1.1\" viewBox=\"0 0 12 16\" width=\"24\"><path d=\"M11 11.28V5c-.03-.78-.34-1.47-.94-2.06C9.46 2.35 8.78 2.03 8 2H7V0L4 3l3 3V4h1c.27.02.48.11.69.31.21.2.3.42.31.69v6.28A1.993 1.993 0 0 0 10 15a1.993 1.993 0 0 0 1-3.72zm-1 2.92c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zM4 3c0-1.11-.89-2-2-2a1.993 1.993 0 0 0-1 3.72v6.56A1.993 1.993 0 0 0 2 15a1.993 1.993 0 0 0 1-3.72V4.72c.59-.34 1-.98 1-1.72zm-.8 10c0 .66-.55 1.2-1.2 1.2-.65 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2zM2 4.2C1.34 4.2.8 3.65.8 3c0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2z\"></path></svg>\n\n<div class=\"time\">\n  <relative-time datetime=\"2016-09-23T07:30:24Z\">Sep 23, 2016</relative-time>\n</div>\n\n<div class=\"title\">\n  <a href=\"/prasadsurase\" data-ga-click=\"News feed, event click, Event click type:PullRequestEvent target:actor\">prasadsurase</a> opened pull request <a href=\"/joshsoftware/code-curiosity/pull/136\" data-ga-click=\"News feed, event click, Event click type:PullRequestEvent target:pull\">joshsoftware/code-curiosity#136</a>\n</div>\n\n<div class=\"details\">\n  <a href=\"/prasadsurase\"><img alt=\"@prasadsurase\" class=\"gravatar\" height=\"30\" src=\"https://avatars3.githubusercontent.com/u/562052?v=3&amp;s=60\" width=\"30\" /></a>\n  <div class=\"message\">\n    <blockquote>Fixed controller method scopes. Removed unwanted routes.</blockquote>\n      <div class=\"pull-info\">\n        <svg aria-hidden=\"true\" class=\"octicon octicon-git-commit\" height=\"16\" version=\"1.1\" viewBox=\"0 0 14 16\" width=\"14\"><path d=\"M10.86 7c-.45-1.72-2-3-3.86-3-1.86 0-3.41 1.28-3.86 3H0v2h3.14c.45 1.72 2 3 3.86 3 1.86 0 3.41-1.28 3.86-3H14V7h-3.14zM7 10.2c-1.22 0-2.2-.98-2.2-2.2 0-1.22.98-2.2 2.2-2.2 1.22 0 2.2.98 2.2 2.2 0 1.22-.98 2.2-2.2 2.2z\"></path></svg>\n        <em>1</em> commit with\n        <em>34</em> additions and\n        <em>31</em> deletions\n      </div>\n  </div>\n</div>\n"

在将 haml 中 github_feed.entries 中的每个条目的内容呈现raw链接包含https://github.com/,而仅包含路径。这会导致问题,例如 UI 上呈现的链接包含应用程序域而不是 github。我们如何解决这个问题?

4

1 回答 1

2

由于 github atom feeds 为内容提供了相对 url,您可能希望将它们替换为绝对引用。

这可以通过

gsub( %r{<a href=\"/},'<a href="https://github.com/')

从而替换您的:

2.2.4 :006 > github_feed.entries.first.content

2.2.4 :006 > github_feed.entries.first.content.gsub( %r{<a href=\"/},'<a href="https://github.com/')
=> "<!-- pull_request_review_comment -->\n<svg aria-label=\"Review pull request comment\" class=\"octicon octicon-comment-discussion dashboard-event-icon\" height=\"32\" role=\"img\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"32\"><path d=\"M15 1H6c-.55 0-1 .45-1 1v2H1c-.55 0-1 .45-1 1v6c0 .55.45 1 1 1h1v3l3-3h4c.55 0 1-.45 1-1V9h1l3 3V9h1c.55 0 1-.45 1-1V2c0-.55-.45-1-1-1zM9 11H4.5L3 12.5V11H1V5h4v3c0 .55.45 1 1 1h3v2zm6-3h-2v1.5L11.5 8H6V2h9v6z\"></path></svg>\n\n<div class=\"time\">\n  <relative-time datetime=\"2016-09-28T03:21:31Z\">Sep 28, 2016</relative-time>\n</div>\n\n<div class=\"title\">\n  <a href=\"https://github.com/prasadsurase\" data-ga-click=\"News feed, event click, Event click type:PullRequestReviewCommentEvent target:actor\">prasadsurase</a> commented on pull request <a href=\"https://github.com/joshsoftware/code-curiosity/pull/137#discussion_r80837476\" data-ga-click=\"News feed, event click, Event click type:PullRequestReviewCommentEvent target:pull-comment\">joshsoftware/code-curiosity#137</a>\n</div>\n\n<div class=\"details\">\n  <a href=\"https://github.com/prasadsurase\"><img alt=\"@prasadsurase\" class=\"gravatar\" height=\"30\" src=\"https://avatars3.githubusercontent.com/u/562052?v=3&amp;s=60\" width=\"30\" /></a>\n  <div class=\"message markdown-body\">\n    <blockquote>\n      <p><a href=\"https://github.com/BandanaPandey\" class=\"user-mention\">@BandanaPandey</a> Same here.</p>\n    </blockquote>\n  </div>\n</div>\n"
于 2016-09-28T04:52:08.970 回答