Because it’s meant to be a block of code, not something that formats using MarkDown rules. The point of code blocks is that you can use special characters and have them rendered as plaintext, instead of actually working as special characters. For instance, here’s the classic Reddit shrug:
¯_(ツ)_/¯
Notice that the left arm is missing? That’s because the backslash and underscores are both special characters. The backslash cancels out the underscore, making the backslash disappear. Now here’s the same shrug formatted as code:
¯\_(ツ)_/¯
Notice that the left arm is visible, (or at least, it should be), because none of the characters were treated as special characters. If the left arm is invisible on your client, that is actually an error in parsing the MarkDown formatting.
Tangentially, if I wanted to format that shrug without the missing arms, it would actually take three backslashes, like this:
¯\\\_(ツ)_/¯
The first backslash cancels out the second, making the second visible. Then the third cancels out the underscore, making the underscores visible, if I only used two, the backslash would be visible, but the underscores would disappear and make the head italicized. But by using three, we get this:
¯\_(ツ)_/¯
Notice how I was able to actually show how I used all three backslashes, using the code block?
compare the rendering of the test output (which i also wrapped in backticks to tell the markdown rendering to render it as code) in my other comment on lemmy vs on piefed.
Essentially, it is not possible to reliably identify the URLs in code (in a language that is not known to either PyFedi or its markdown parser), because they sometimes are adjacent to otherwise-URL-valid characters which actually terminate the URL in whatever syntax is being used inside the code block. So, even though it is sometimes harmless to auto-linkify inside a code block, it is also often wrong and therefore should not be (and generally is not) done.
But also, code formatting should be available as a way to disable auto-linkification when a post or comment author wants to (as I did in my original comment).
hmm, i see my first edit to this comment got federated to piefed but subsequent edits did not; the version after subsequent edits can be seen here on the lemmy instance i’m posting from
I had a glance at the source and don’t see a very quick fix, but I think the solution involves getting rid of the auto-linkification here and instead have that be done by the markdown library. This issue indicates that the markdown library PyFedi is using is capable of doing that.
Here is a test I wrote which I think should pass which does not currently :)
diff --git a/tests/test_markdown_to_html.py b/tests/test_markdown_to_html.pyindex 329b19be..108276c5 100644--- a/tests/test_markdown_to_html.py+++ b/tests/test_markdown_to_html.py@@ -37,6 +37,12 @@ class TestMarkdownToHtml(unittest.TestCase):
result = markdown_to_html(markdown)
self.assertTrue("<pre><code>code block" in result)
+ def test_code_block_link(self):+ """Test code blocks formatting containing a link"""+ markdown = "```\ncode block with link: https://example.com/ \n```"+ result = markdown_to_html(markdown)+ self.assertEqual("<pre><code>code block with link: https://example.com/ ", result)
it currently produces this failure:
====================================================== FAILURES =======================================================
_______________________________________ TestMarkdownToHtml.test_code_block_link _______________________________________
self = <tests.test_markdown_to_html.TestMarkdownToHtmltestMethod=test_code_block_link> def test_code_block_link(self):
"""Test code blocks formatting containing a link"""
markdown = "```\ncode block with link: https://example.com/ \n```"
result = markdown_to_html(markdown)
> self.assertEqual("<pre><code>code block with link: https://example.com/ ", result)
E AssertionError: '<pre[24 chars]ink: https://example.com/ ' != '<pre[24 chars]ink: <a href="https://example.com/" rel="nofoll[61 chars]e>\n'
E - <pre><code>code block with link: https://example.com/
E + <pre><code>code block with link: <a href="https://example.com/" rel="nofollow ugc" target="_blank">https://example.com/</a>
E + </code></pre>
E +
tests/test_markdown_to_html.py:44: AssertionError
oops, i read my test closer and realized it will never pass as-is due to being cribbed from another test that was using in instead of assertEqual (and the expected string not containing the closing tags) but I trust it conveys what i mean to
URL perfectly clickable from PieFed
PieFed W
URLs inside code fragments should not be clickable. This is a Markdown parsing error.
PieFed L
Why shouldn’t they be ?
Code blocks are for displaying raw text, ignoring all formatting.
Because it’s meant to be a block of code, not something that formats using MarkDown rules. The point of code blocks is that you can use special characters and have them rendered as plaintext, instead of actually working as special characters. For instance, here’s the classic Reddit shrug:
¯_(ツ)_/¯
Notice that the left arm is missing? That’s because the backslash and underscores are both special characters. The backslash cancels out the underscore, making the backslash disappear. Now here’s the same shrug formatted as code:
Notice that the left arm is visible, (or at least, it should be), because none of the characters were treated as special characters. If the left arm is invisible on your client, that is actually an error in parsing the MarkDown formatting.
Tangentially, if I wanted to format that shrug without the missing arms, it would actually take three backslashes, like this:
The first backslash cancels out the second, making the second visible. Then the third cancels out the underscore, making the underscores visible, if I only used two, the backslash would be visible, but the underscores would disappear and make the head italicized. But by using three, we get this:
¯\_(ツ)_/¯
Notice how I was able to actually show how I used all three backslashes, using the code block?
compare the rendering of the test output (which i also wrapped in backticks to tell the markdown rendering to render it as code) in my other comment on lemmy vs on piefed.
Essentially, it is not possible to reliably identify the URLs in code (in a language that is not known to either PyFedi or its markdown parser), because they sometimes are adjacent to otherwise-URL-valid characters which actually terminate the URL in whatever syntax is being used inside the code block. So, even though it is sometimes harmless to auto-linkify inside a code block, it is also often wrong and therefore should not be (and generally is not) done.
But also, code formatting should be available as a way to disable auto-linkification when a post or comment author wants to (as I did in my original comment).
Oh ok, I see
Thank you for explaining !
hmm, i see my first edit to this comment got federated to piefed but subsequent edits did not; the version after subsequent edits can be seen here on the lemmy instance i’m posting from
that is not a W… i surrounded the URL with backticks, which means it should be treated as code and not made into a link.
cc: PyFedi developer @rimu@piefed.social
I had a glance at the source and don’t see a very quick fix, but I think the solution involves getting rid of the auto-linkification here and instead have that be done by the markdown library. This issue indicates that the markdown library PyFedi is using is capable of doing that.
Here is a test I wrote which I think should pass which does not currently :)
diff --git a/tests/test_markdown_to_html.py b/tests/test_markdown_to_html.py index 329b19be..108276c5 100644 --- a/tests/test_markdown_to_html.py +++ b/tests/test_markdown_to_html.py @@ -37,6 +37,12 @@ class TestMarkdownToHtml(unittest.TestCase): result = markdown_to_html(markdown) self.assertTrue("<pre><code>code block" in result) + def test_code_block_link(self): + """Test code blocks formatting containing a link""" + markdown = "```\ncode block with link: https://example.com/ \n```" + result = markdown_to_html(markdown) + self.assertEqual("<pre><code>code block with link: https://example.com/ ", result)
it currently produces this failure:
====================================================== FAILURES ======================================================= _______________________________________ TestMarkdownToHtml.test_code_block_link _______________________________________ self = <tests.test_markdown_to_html.TestMarkdownToHtml testMethod=test_code_block_link> def test_code_block_link(self): """Test code blocks formatting containing a link""" markdown = "```\ncode block with link: https://example.com/ \n```" result = markdown_to_html(markdown) > self.assertEqual("<pre><code>code block with link: https://example.com/ ", result) E AssertionError: '<pre[24 chars]ink: https://example.com/ ' != '<pre[24 chars]ink: <a href="https://example.com/" rel="nofoll[61 chars]e>\n' E - <pre><code>code block with link: https://example.com/ E + <pre><code>code block with link: <a href="https://example.com/" rel="nofollow ugc" target="_blank">https://example.com/</a> E + </code></pre> E + tests/test_markdown_to_html.py:44: AssertionError
HTH, and thanks for writing free software!
That does help very much, thank you!
I’ve committed a fix.
PieFed / @cypherpunks@lemmy.ml W
oops, i read my test closer and realized it will never pass as-is due to being cribbed from another test that was using
in
instead ofassertEqual
(and the expected string not containing the closing tags) but I trust it conveys what i mean to