MarkdownSharp 正在转换包含下划线字符的 URL

发布于 2024-12-18 20:11:52 字数 902 浏览 1 评论 0原文

我在我的一个项目中使用 MarkdownSharp,并注意到如果我的任何 URL 中某处包含成对的下划线字符,它将被视为斜体,因此将 _ 替换为

我在谷歌上查看过,但找不到任何对此问题行为的参考,并且通过阅读 MarkdownSharp 代码中的一些注释,它表明代码的编写方式是为了防止这不会发生。请参阅以下 Markdown 代码片段:

此处调用其他子程序的顺序至关重要。链接和图像替换需要在 EscapeSpecialChars() 之前进行,以便对 a 和 img 标记中的任何 * 或 _ 进行编码。

    public string Transform(string text)
    {
        if (String.IsNullOrEmpty(text)) return "";

        Setup();

        text = Normalize(text);

        text = HashHTMLBlocks(text);
        text = StripLinkDefinitions(text);
        text = RunBlockGamut(text);
        text = Unescape(text);

        Cleanup();

        return text + "\n";
    }

此行为是否有已知的解决方法?

** 更新:我刚刚测试了在 StackOverflow 上输入一个 url,我相信它使用了 MarkdownSharp 的一个版本(并且根据我的项目启用了 AutoHyperlink),并且它处理 url 中的单个下划线实例,只要一对下划线出现在网址中,它就中断了。

I am using MarkdownSharp in one of my projects and have noticed that if any of my Url's contain pairs of underscore characters somewhere within it, it's treated as italic and therefore replaces the _ with <em>.

I've had a look on google but can't find any reference to this problem behaviour, and from reading some of the comments in the MarkdownSharp code, it suggests that the code is written the way it is to prevent this from happening. See below snippet from the markdown code:

The order in which other subs are called here is essential. Link and image substitutions need to happen before EscapeSpecialChars(), so that any *'s or _'s in the a and img tags get encoded.

    public string Transform(string text)
    {
        if (String.IsNullOrEmpty(text)) return "";

        Setup();

        text = Normalize(text);

        text = HashHTMLBlocks(text);
        text = StripLinkDefinitions(text);
        text = RunBlockGamut(text);
        text = Unescape(text);

        Cleanup();

        return text + "\n";
    }

Is there a known workaround for this behaviour?

** UPDATE: I have just tested entering a url on StackOverflow which I believe uses a version of MarkdownSharp (and AutoHyperlink is enabled as per my project) and whilst it handles a single underscore instance within the url, as soon as a pair of underscores appear in the url, it breaks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贱贱哒 2024-12-25 20:11:52

MarkdownSharp 有一个正是出于这个原因而创建的配置选项:

/// <summary>
/// when true, bold and italic require non-word characters on either side  
/// WARNING: this is a significant deviation from the markdown spec
/// </summary>
public bool StrictBoldItalic { get; set; }

有关一些背景信息,请参阅 中的第 1 点。 https://blog.stackoverflow.com/2008/06/ Three-markdown-gotcha/

MarkdownSharp has a configuration option that was created for this very reason:

/// <summary>
/// when true, bold and italic require non-word characters on either side  
/// WARNING: this is a significant deviation from the markdown spec
/// </summary>
public bool StrictBoldItalic { get; set; }

For some background info, see point 1. in https://blog.stackoverflow.com/2008/06/three-markdown-gotcha/.

烟凡古楼 2024-12-25 20:11:52

您是否尝试过使用反斜杠转义要保留的 _ 字符?

请参阅: http://daringfireball.net/projects/markdown/syntax#backslash 文章:

例如,如果您想用星号(而不是 HTML 标记)包围单词,则可以在星号前使用反斜杠,如下所示:


\*literal asterisks\*

Markdown 中的星号或多或少与下划线相同。


编辑:由于我发表的评论,答案被接受。 OP 决定禁用自动超链接并使用 HTML 标记或 Markdown 链接语法手动插入链接。

听起来这个功能没有经过充分考虑。它带有一个警告,称它将使系统执行不符合标准降价的操作,在我看来,这似乎是一个警告,表明他们没有解决这些功能的所有问题。您可能需要更改库来修复此类错误...

Have you tried escaping the _ characters that you want to preserve by using a backslash?

See: http://daringfireball.net/projects/markdown/syntax#backslash

From the article:

For example, if you wanted to surround a word with literal asterisks (instead of an HTML <em> tag), you can use backslashes before the asterisks, like this:

\*literal asterisks\*

Asterisks in Markdown are more or less the same as underscores.


Edit: The answer was accepted due to this comment I made. The OP decided to disable auto hyperlinks and manually insert the links with either HTML markup or Markdown link syntax.

Sounds like that feature wasn't thought through well enough. It comes with a warning saying it will make the system do things that aren't compliant with standard markdown, which seems to me like a warning that they didn't solve every problem with those features. You might need to change the library to fix bugs like that...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文