Skip to content

Commit 56a759a

Browse files
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 <[email protected]>
1 parent d37c533 commit 56a759a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

guides/rails_guides/markdown/renderer.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
module RailsGuides
99
class Markdown
1010
class Renderer < Redcarpet::Render::HTML # :nodoc:
11+
APPLICATION_FILEPATH_REGEXP = /(app|config|db|lib|test)\//
12+
ERB_FILEPATH_REGEXP = /^<%# #{APPLICATION_FILEPATH_REGEXP}.* %>/o
13+
RUBY_FILEPATH_REGEXP = /^# #{APPLICATION_FILEPATH_REGEXP}/o
14+
1115
cattr_accessor :edge, :version
1216

1317
def block_code(code, language)
@@ -78,6 +82,7 @@ def lexer_language(code_type)
7882
end
7983

8084
def clipboard_content(code, language)
85+
# Remove prompt and results of commands.
8186
prompt_regexp =
8287
case language
8388
when "bash"
@@ -90,6 +95,19 @@ def clipboard_content(code, language)
9095
code = code.lines.grep(prompt_regexp).join.gsub(prompt_regexp, "")
9196
end
9297

98+
# Remove comments that reference an application file.
99+
filepath_regexp =
100+
case language
101+
when "erb", "html+erb"
102+
ERB_FILEPATH_REGEXP
103+
when "ruby", "yaml", "yml"
104+
RUBY_FILEPATH_REGEXP
105+
end
106+
107+
if filepath_regexp
108+
code = code.lines.grep_v(filepath_regexp).join
109+
end
110+
93111
ERB::Util.html_escape(code)
94112
end
95113

0 commit comments

Comments
 (0)