Remove example filepaths from code blocks in guides

Some code examples use commented out paths for the path where the code
should be written to. These paths can be ignored in the clipboard when
using the "copy" button.

Co-authored-by: Carlos Antonio da Silva <carlosantoniodasilva@gmail.com>
This commit is contained in:
Petrik 2024-04-09 15:58:22 +02:00
parent d37c533139
commit 56a759a22b
1 changed files with 18 additions and 0 deletions

View File

@ -8,6 +8,10 @@ Rouge::Lexers::Shell::BUILTINS << "|bin/rails|brew|bundle|gem|git|node|rails|rak
module RailsGuides module RailsGuides
class Markdown class Markdown
class Renderer < Redcarpet::Render::HTML # :nodoc: class Renderer < Redcarpet::Render::HTML # :nodoc:
APPLICATION_FILEPATH_REGEXP = /(app|config|db|lib|test)\//
ERB_FILEPATH_REGEXP = /^<%# #{APPLICATION_FILEPATH_REGEXP}.* %>/o
RUBY_FILEPATH_REGEXP = /^# #{APPLICATION_FILEPATH_REGEXP}/o
cattr_accessor :edge, :version cattr_accessor :edge, :version
def block_code(code, language) def block_code(code, language)
@ -78,6 +82,7 @@ module RailsGuides
end end
def clipboard_content(code, language) def clipboard_content(code, language)
# Remove prompt and results of commands.
prompt_regexp = prompt_regexp =
case language case language
when "bash" when "bash"
@ -90,6 +95,19 @@ module RailsGuides
code = code.lines.grep(prompt_regexp).join.gsub(prompt_regexp, "") code = code.lines.grep(prompt_regexp).join.gsub(prompt_regexp, "")
end end
# Remove comments that reference an application file.
filepath_regexp =
case language
when "erb", "html+erb"
ERB_FILEPATH_REGEXP
when "ruby", "yaml", "yml"
RUBY_FILEPATH_REGEXP
end
if filepath_regexp
code = code.lines.grep_v(filepath_regexp).join
end
ERB::Util.html_escape(code) ERB::Util.html_escape(code)
end end