Stop markdown caching of non-markdown Snippet content
Summary
When viewing a Snippet, Banzai::Renderer#render_field
gets called for the Snippet title
when rendering the header, which in turn will call CacheMarkdownField#refresh_markdown_cache
if the :title
cache is not up to date / does not exist.
The problem with this, is that the refresh method will then attempt to cache all fields on the Snippet, including content
and treating them as if they're markdown, which is not always the case.
This means that we are needlessly processing and caching non-markdown content but also causing errors when passing large non-markdown fields to CommonMarker for parsing (see: #204894 (closed)), and the content_html
field is not even being used when rendering non-markdown Snippets.
Steps to reproduce
- Create a new snippet with a non-markdown filename, e.g.
my_snippet.rb
- View the snippet's
content_html
in DB or rails-console to confirm it was cached, e.g.<p data-sourcepos="1:1-1:13" dir="auto">test ruby file</p>
What is the current bug behavior?
Snippet.content_html
is populated for non-markdown Snippets
What is the expected correct behavior?
Snippet.content_html
should not be populated for non-markdown Snippets
Output of checks
This bug happens on GitLab.com
Possible fixes
Banzai::Renderer#render_field
could change to check if the object
should be cached due to it's content, rather than just whether or not it responds to cached_markdown_fields
and cache is up to date.
We could add a helper method e.g. is_markdown_file?
that checks for object.file_name
and it's file extension, to determine whether or not it is actually intended to be markdown.
We should probably also ensure that if the file extension changes from markdown to something else, we clear the cached fields to avoid future confusion.