Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not escape underscored username in code or codeblocks
Skip escaping underscore when content is in code or codeblock. It works by going through all lines of a markdown content. Find underscored username. Escape if we are not in the codeblock. The (?<!) is negative lookbehind which rules out the case when a underscored username is present in code, e.g., `@_dev_` will not be escaped.
- Loading branch information
1 parent
41d812b
commit 432885f
Showing
3 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# To go through a markdown document. Mainly used to decide if we are in | ||
# code blocks to decide if we should escape underscored usernames. | ||
class MarkdownTraverser | ||
def initialize(markdown) | ||
@lines = markdown.dup.lines | ||
init_position | ||
end | ||
|
||
def each | ||
lines.each do |line| | ||
update_position(line.include?(CODEBLOCK_MARKER)) | ||
yield(line) | ||
end | ||
end | ||
|
||
def in_codeblock? | ||
prev == true && current == false | ||
end | ||
|
||
private | ||
|
||
CODEBLOCK_MARKER = "```".freeze | ||
|
||
attr_accessor :lines, :prev, :current | ||
|
||
def init_position | ||
self.prev = false | ||
self.current = lines.first.include?(CODEBLOCK_MARKER) | ||
end | ||
|
||
def update_position(current) | ||
self.current = current | ||
self.prev = current ? !prev : prev | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters