I think I have found the solution to a GitHub issue, but it involves a bit of advanced RegEx in Ruby. (I rarely worked with Ruby and I wouldn’t call myself a professional in RegEx). The problem is that when you have let’s say 5 (anything above 3) opening backticks then the parser encounters 3 backticks, it will automatically close the code block. Basically the code is as follows:
def escape_liquid_tags_in_codeblock(content)
# Escape codeblocks, code spans, and inline code
content.gsub(/[[:space:]]*~{3}.*?~{3}|[[:space:]]*`{3}.*?`{3}|`{2}.+?`{2}|`{1}.+?`{1}/m) do |codeblock|
codeblock.gsub!("{% endraw %}", "{----% endraw %----}")
codeblock.gsub!("{% raw %}", "{----% raw %----}")
if codeblock.match?(/[[:space:]]*`{3}/)
"n{% raw %}n#{codeblock}n{% endraw %}n"
else
"{% raw %}#{codeblock}{% endraw %}"
end
end
end
First of all, a block of code in markdown can be at least 3 backticks (`) or tildes (~), not necessarily exactly 3. I believe I know the solution for this problem, but I just need to know if it is possible to write something like this?
[[:space:]]*`{N}.*?`{N}
where N can be any number, but of course, the number of beginning and closing ticks have to be equal.
I also believe that the if statement should be {3,}
not {3}
. If anyone can tell me how to have a regex of N beginning backticks and N closing backticks that would be great. I would also be open to any suggestions on how to go about the issue.
I tried ChatGPTing it, but ChatGPT didn’t give me the right RegEx if it is even possible.
1
I agree with your “at least” statement:
- Change
{3}
to{3,}
to match at least 3 characters.
Next, capture groups can apply here too:
- Change …
{3,}.*?{3,}
… to: …(`{3,}).*?1
This will capture the backtick itself and the repeat, and then match that capture on the closing end.
We can then simplify the match to a character range to eliminate the big top-level “or”:
- Replace
(`{3,})
with([~`]{3,})
Then, depending on your preference, we can replace [[:space:]]
with s
. This reduces the regex to:
/s*([~`]{3,})(.*)?1/m
I added a capture for the content because that’s usually desired, and your m
flag allows matches to span multiple lines. Here is a link to this in an online Ruby Regex Evaluator:
https://rubular.com/r/lOCj3z2a23CApH
All that’s left then is to add comments.
2