Factor out Emoji parsing using html-pipeline-gitlab
Created by: Razer6
This PR is one of the first PRs refactoring the markup handling. As discussed with @randx, I'll do this with small PRs, to keep changes detectable.
What it does?
It removes parsing emoji references, such as :+1:
, from GitLab and calls instead HTML::Pipeline::GitLab::MarkdownPipeline.
This pipeline currently only contains the EmojiFilter.
In future we need to separate the markdown handling, by introducing different pipelines for different context. Eg. for issues/MRs, dashboard...
Created by: TeatroIO
I've prepared a stage. Click to open.
By Administrator on 2014-09-25T12:42:20 (imported from GitLab project)
Created by: dzaporozhets
@Razer6 please add me to owners at http://rubygems.org/gems/html-pipeline-gitlab
By Administrator on 2014-10-01T16:33:55 (imported from GitLab project)
62 65 insert_piece($1) 63 66 end 64 67 68 # Context passed to the markdoqwn pipeline 69 markdown_context = { 70 asset_root: File.join(root_url, Created by: maxlazio
@Razer6 This change caused a problem, emoji doesn't get rendered correctly. This is the html that gets generated:
<img class="emoji" title=":+1:" alt=":+1:" src="https://dev.gitlab.org/assets/emoji/%2B1.png" height="20" width="20">
%2B1.png
doesn't existBy Administrator on 2014-10-03T08:31:02 (imported from GitLab project)
Created by: Razer6
@randx @maxlazio I've tested a bit, but did not come to a solution. The problem is, I can't access assets from the asset-pipeline from another gem. However, if we move the filters from the gem to GitLab's codebase it would work. For filters in future, I think it would be better anyway because these filters are tightly closed to GitLab itself.
Would that be a solution you would accept?
By Administrator on 2014-10-05T10:29:23 (imported from GitLab project)
Created by: Razer6
Does anyone know, we can use for e.g.
image_path
(a function fromActionView::Helpers::AssetUrlHelper
) in module scope, but not in class scope?We can use this helper in
Gitlab::Markdown.gfm
but I can't use it in a method fromGitlab::Markdown::EmojiFilter
.That's one mystery to me. Seems some rails magic is behind, but I didn't get it yet
😰 By Administrator on 2014-10-05T16:10:21 (imported from GitLab project)
Created by: mr-vinn
@Razer6 One option might be to use
emoji/something.png
as thesrc
attribute in the pipeline filter instead of trying to get the full asset path (I also had to remove the call to::CGI#escape
to make this work):diff --git a/lib/html/pipeline/gitlab/gitlab_emoji_filter.rb b/lib/html/pipeline/gitlab/gitlab_emoji_filter.rb index 384db29..6131e02 100644 --- a/lib/html/pipeline/gitlab/gitlab_emoji_filter.rb +++ b/lib/html/pipeline/gitlab/gitlab_emoji_filter.rb @@ -57,7 +57,7 @@ module HTML private def emoji_url(name) - File.join(asset_root, 'emoji', emoji_filename(name)) + File.join('emoji', emoji_filename(name)) end # Build a regexp that matches all valid :emoji: names. @@ -74,7 +74,7 @@ module HTML end def emoji_filename(name) - "#{::CGI.escape(name)}.png" + "#{name}.png" end end end
Then you can replace the
src
attributes inGitlab::Markdown#gfm
before calling#to_html
on the output:diff --git a/lib/gitlab/markdown.rb b/lib/gitlab/markdown.rb index 17512a5..db9c0d5 100644 --- a/lib/gitlab/markdown.rb +++ b/lib/gitlab/markdown.rb @@ -78,6 +78,10 @@ module Gitlab result = HTML::Pipeline::Gitlab::MarkdownPipeline.call(text, markdown_context) + result[:output].css('img').each do |img| + img.attribute('src').content = image_path(img.attribute('src')) + end + text = result[:output].to_html(save_with: 0) allowed_attributes = ActionView::Base.sanitized_allowed_attributes
This approach feels a little awkward, but at least you don't have to worry about the asset path in the emoji filter.
By Administrator on 2014-10-08T03:07:05 (imported from GitLab project)
Created by: maxlazio
This has been resolved in the version 0.1.3 of
html-pipeline-gitlab
gem. Gitlab is using this version since this commitBy Administrator on 2014-10-08T12:18:57 (imported from GitLab project)