为什么Github内容HTML与代码块呈现所有行缩进除了第一个?

时间:2015-10-06 06:42:32

标签: ruby-on-rails markdown

我正在使用Github API来获取文档的内容并将其显示在我的页面上。这是我正在使用的代码:

@contents = Octokit.contents "user/repo", path: "/readme.md", accept: 'application/vnd.github.v3.html'

然后我在调用.html_safe后显示视图中的内容。

问题是一切正常,但属于代码块的行都是缩进的,除了第一行。

看看它是如何出现的:

enter image description here

当然是错的。请注意,从Github Web界面查看时,原始内容正确显示。

原始内容是:

https://gist.github.com/pmatsinopoulos/87f81d7125124190e0c8

有没有人知道如何解决这个问题?

请注意,如果这与问题有关,我正在使用Ruby on Rails v4。

1 个答案:

答案 0 :(得分:1)

You're likely displaying that content in <pre> tag and you're using HAML. You see, <pre> tag is space-preserving and HAML is space-sensitive.

I had a similar problem a while ago. IIRC, haml indentation level is somehow added to all subsequent lines of content being rendered.

My memory is blurry on the details, but this is what appears to have fixed it for me, HAML's find_and_preserve helper:

= find_and_preserve(item['content']).html_safe