diff options
author | Toon Claes <toon@gitlab.com> | 2017-04-06 16:47:52 +0200 |
---|---|---|
committer | Toon Claes <toon@gitlab.com> | 2017-04-27 13:22:17 +0200 |
commit | 05e0f504530a162d4bcb886adf504c12cffd5934 (patch) | |
tree | aeece92fb84421062c919e0c63173718db3577ba | |
parent | ef4d4446e957ecbb086859d96185a27af168d11c (diff) | |
download | gitlab-ce-05e0f504530a162d4bcb886adf504c12cffd5934.tar.gz |
Cache the rendered README, but post-process on show
Because the post-processing of the rendered README is dependent on the
context (i.e. the current user), do the post-processing when the
README is being displayed.
-rw-r--r-- | app/helpers/markup_helper.rb | 40 | ||||
-rw-r--r-- | app/helpers/tree_helper.rb | 4 | ||||
-rw-r--r-- | app/models/repository.rb | 15 | ||||
-rw-r--r-- | app/views/projects/_readme.html.haml | 3 | ||||
-rw-r--r-- | app/views/projects/tree/_readme.html.haml | 2 | ||||
-rw-r--r-- | changelogs/unreleased/26585-remove-readme-view-caching.yml | 4 | ||||
-rw-r--r-- | lib/gitlab/asciidoc.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/other_markup.rb | 2 | ||||
-rw-r--r-- | spec/helpers/application_helper_spec.rb | 27 | ||||
-rw-r--r-- | spec/helpers/markup_helper_spec.rb | 28 | ||||
-rw-r--r-- | spec/models/repository_spec.rb | 4 |
11 files changed, 74 insertions, 57 deletions
diff --git a/app/helpers/markup_helper.rb b/app/helpers/markup_helper.rb index f82791b88fa..c39b630aec6 100644 --- a/app/helpers/markup_helper.rb +++ b/app/helpers/markup_helper.rb @@ -60,12 +60,17 @@ module MarkupHelper end def markdown(text, context = {}) + html = markdown_render(text, context) + + markup_postprocess(html, context) + end + + def markdown_render(text, context = {}) return "" unless text.present? context[:project] ||= @project - html = Banzai.render(text, context) - banzai_postprocess(html, context) + Banzai.render(text, context) end def markdown_field(object, field) @@ -76,7 +81,7 @@ module MarkupHelper banzai_postprocess(html, object.banzai_render_context(field)) end - def asciidoc(text) + def asciidoc_render(text) Gitlab::Asciidoc.render( text, project: @project, @@ -90,7 +95,7 @@ module MarkupHelper ) end - def other_markup(file_name, text) + def other_markup_render(file_name, text) Gitlab::OtherMarkup.render( file_name, text, @@ -105,6 +110,14 @@ module MarkupHelper ) end + def markup_postprocess(html, context = {}) + return "" unless html.present? + + context[:project] ||= @project + + banzai_postprocess(html, context) + end + # Return the first line of +text+, up to +max_chars+, after parsing the line # as Markdown. HTML tags in the parsed output are not counted toward the # +max_chars+ limit. If the length limit falls within a tag's contents, then @@ -116,27 +129,34 @@ module MarkupHelper end def render_wiki_content(wiki_page) + context = { pipeline: :wiki, project_wiki: @project_wiki, page_slug: wiki_page.slug } case wiki_page.format when :markdown - markdown(wiki_page.content, pipeline: :wiki, project_wiki: @project_wiki, page_slug: wiki_page.slug) + html = markdown_render(wiki_page.content, context) when :asciidoc - asciidoc(wiki_page.content) + html = asciidoc_render(wiki_page.content) else - wiki_page.formatted_content.html_safe + return wiki_page.formatted_content.html_safe end + markup_postprocess(html, context) end def render_markup(file_name, file_content) + html = markup_render(file_name, file_content) + markup_postprocess(html) + end + + def markup_render(file_name, file_content) if gitlab_markdown?(file_name) - Hamlit::RailsHelpers.preserve(markdown(file_content)) + Hamlit::RailsHelpers.preserve(markdown_render(file_content)) elsif asciidoc?(file_name) - asciidoc(file_content) + asciidoc_render(file_content) elsif plain?(file_name) content_tag :pre, class: 'plain-readme' do file_content end else - other_markup(file_name, file_content) + other_markup_render(file_name, file_content) end rescue RuntimeError simple_format(file_content) diff --git a/app/helpers/tree_helper.rb b/app/helpers/tree_helper.rb index f1dab60524e..f7b5a5f4dfc 100644 --- a/app/helpers/tree_helper.rb +++ b/app/helpers/tree_helper.rb @@ -12,10 +12,6 @@ module TreeHelper tree.html_safe end - def render_readme(readme) - render_markup(readme.name, readme.data) - end - # Return an image icon depending on the file type and mode # # type - String type of the tree item; either 'folder' or 'file' diff --git a/app/models/repository.rb b/app/models/repository.rb index 7bb874d7744..328e817cc3f 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -1,6 +1,7 @@ require 'securerandom' class Repository + include MarkupHelper include Gitlab::ShellAdapter include RepositoryMirroring @@ -17,9 +18,9 @@ class Repository # same name. The cache key used by those methods must also match method's # name. # - # For example, for entry `:readme` there's a method called `readme` which - # stores its data in the `readme` cache key. - CACHED_METHODS = %i(size commit_count readme contribution_guide + # For example, for entry `:commit_count` there's a method called `commit_count` which + # stores its data in the `commit_count` cache key. + CACHED_METHODS = %i(size commit_count rendered_readme contribution_guide changelog license_blob license_key gitignore koding_yml gitlab_ci_yml branch_names tag_names branch_count tag_count avatar exists? empty? root_ref).freeze @@ -28,7 +29,7 @@ class Repository # changed. This Hash maps file types (as returned by Gitlab::FileDetector) to # the corresponding methods to call for refreshing caches. METHOD_CACHES_FOR_FILE_TYPES = { - readme: :readme, + readme: :rendered_readme, changelog: :changelog, license: %i(license_blob license_key), contributing: :contribution_guide, @@ -527,7 +528,11 @@ class Repository head.readme end end - cache_method :readme + + def rendered_readme + markup_render(readme.name, readme.data) if readme + end + cache_method :rendered_readme def contribution_guide file_on_head(:contributing) diff --git a/app/views/projects/_readme.html.haml b/app/views/projects/_readme.html.haml index b6fb08b68e9..b4615e084e0 100644 --- a/app/views/projects/_readme.html.haml +++ b/app/views/projects/_readme.html.haml @@ -4,8 +4,7 @@ - if can?(current_user, :push_code, @project) = link_to icon('pencil'), namespace_project_edit_blob_path(@project.namespace, @project, tree_join(@repository.root_ref, readme.name)), class: 'light edit-project-readme' .file-content.wiki - = cache(readme_cache_key) do - = render_readme(readme) + = markup_postprocess(@repository.rendered_readme) - else .row-content-block.second-block.center %h3.page-title diff --git a/app/views/projects/tree/_readme.html.haml b/app/views/projects/tree/_readme.html.haml index bdcc160a067..30df6fd50d0 100644 --- a/app/views/projects/tree/_readme.html.haml +++ b/app/views/projects/tree/_readme.html.haml @@ -5,4 +5,4 @@ %strong = readme.name .file-content.wiki - = render_readme(readme) + = render_markup(readme.name, readme.data) diff --git a/changelogs/unreleased/26585-remove-readme-view-caching.yml b/changelogs/unreleased/26585-remove-readme-view-caching.yml new file mode 100644 index 00000000000..6aefae982bf --- /dev/null +++ b/changelogs/unreleased/26585-remove-readme-view-caching.yml @@ -0,0 +1,4 @@ +--- +title: 'Remove view fragment caching for project READMEs' +merge_request: 8838 +author: diff --git a/lib/gitlab/asciidoc.rb b/lib/gitlab/asciidoc.rb index d575367d81a..b157b59f9dd 100644 --- a/lib/gitlab/asciidoc.rb +++ b/lib/gitlab/asciidoc.rb @@ -34,8 +34,6 @@ module Gitlab html = ::Asciidoctor.convert(input, asciidoc_opts) - html = Banzai.post_process(html, context) - filter = Banzai::Filter::SanitizationFilter.new(html) html = filter.call.to_s diff --git a/lib/gitlab/other_markup.rb b/lib/gitlab/other_markup.rb index e67acf28c94..214dba0fa11 100644 --- a/lib/gitlab/other_markup.rb +++ b/lib/gitlab/other_markup.rb @@ -15,8 +15,6 @@ module Gitlab html = GitHub::Markup.render(file_name, input). force_encoding(input.encoding) - html = Banzai.post_process(html, context) - filter = Banzai::Filter::SanitizationFilter.new(html) html = filter.call.to_s diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 5c07ea8a872..01bdf01ad22 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -239,33 +239,6 @@ describe ApplicationHelper do end end - describe 'render_markup' do - let(:content) { 'Noël' } - let(:user) { create(:user) } - before do - allow(helper).to receive(:current_user).and_return(user) - end - - it 'preserves encoding' do - expect(content.encoding.name).to eq('UTF-8') - expect(helper.render_markup('foo.rst', content).encoding.name).to eq('UTF-8') - end - - it "delegates to #markdown when file name corresponds to Markdown" do - expect(helper).to receive(:gitlab_markdown?).with('foo.md').and_return(true) - expect(helper).to receive(:markdown).and_return('NOEL') - - expect(helper.render_markup('foo.md', content)).to eq('NOEL') - end - - it "delegates to #asciidoc when file name corresponds to AsciiDoc" do - expect(helper).to receive(:asciidoc?).with('foo.adoc').and_return(true) - expect(helper).to receive(:asciidoc).and_return('NOEL') - - expect(helper.render_markup('foo.adoc', content)).to eq('NOEL') - end - end - describe '#active_when' do it { expect(helper.active_when(true)).to eq('active') } it { expect(helper.active_when(false)).to eq(nil) } diff --git a/spec/helpers/markup_helper_spec.rb b/spec/helpers/markup_helper_spec.rb index 617cfed5bee..59ae6570ae7 100644 --- a/spec/helpers/markup_helper_spec.rb +++ b/spec/helpers/markup_helper_spec.rb @@ -126,15 +126,16 @@ describe MarkupHelper do it "uses Wiki pipeline for markdown files" do allow(@wiki).to receive(:format).and_return(:markdown) - expect(helper).to receive(:markdown).with('wiki content', pipeline: :wiki, project_wiki: @wiki, page_slug: "nested/page") + expect(helper).to receive(:markdown_render).with('wiki content', pipeline: :wiki, project_wiki: @wiki, page_slug: "nested/page") helper.render_wiki_content(@wiki) end it "uses Asciidoctor for asciidoc files" do + allow_any_instance_of(ApplicationSetting).to receive(:current).and_return(::ApplicationSetting.create_from_defaults) allow(@wiki).to receive(:format).and_return(:asciidoc) - expect(helper).to receive(:asciidoc).with('wiki content') + expect(helper).to receive(:asciidoc_render).with('wiki content') helper.render_wiki_content(@wiki) end @@ -149,6 +150,29 @@ describe MarkupHelper do end end + describe 'render_markup' do + let(:content) { 'Noël' } + + it 'preserves encoding' do + expect(content.encoding.name).to eq('UTF-8') + expect(helper.render_markup('foo.rst', content).encoding.name).to eq('UTF-8') + end + + it "delegates to #markdown_render when file name corresponds to Markdown" do + expect(helper).to receive(:gitlab_markdown?).with('foo.md').and_return(true) + expect(helper).to receive(:markdown_render).and_return('NOEL') + + expect(helper.render_markup('foo.md', content)).to eq('NOEL') + end + + it "delegates to #asciidoc_render when file name corresponds to AsciiDoc" do + expect(helper).to receive(:asciidoc?).with('foo.adoc').and_return(true) + expect(helper).to receive(:asciidoc_render).and_return('NOEL') + + expect(helper.render_markup('foo.adoc', content)).to eq('NOEL') + end + end + describe '#first_line_in_markdown' do it 'truncates Markdown properly' do text = "@#{user.username}, can you look at this?\nHello world\n" diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index 74d5ebc6db0..98d0641443e 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -1803,9 +1803,9 @@ describe Repository, models: true do describe '#refresh_method_caches' do it 'refreshes the caches of the given types' do expect(repository).to receive(:expire_method_caches). - with(%i(readme license_blob license_key)) + with(%i(rendered_readme license_blob license_key)) - expect(repository).to receive(:readme) + expect(repository).to receive(:rendered_readme) expect(repository).to receive(:license_blob) expect(repository).to receive(:license_key) |