MarkdownSharp 正在转换包含下划线字符的 URL
我在我的一个项目中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MarkdownSharp 有一个正是出于这个原因而创建的配置选项:
有关一些背景信息,请参阅 中的第 1 点。 https://blog.stackoverflow.com/2008/06/ Three-markdown-gotcha/。
MarkdownSharp has a configuration option that was created for this very reason:
For some background info, see point 1. in https://blog.stackoverflow.com/2008/06/three-markdown-gotcha/.
您是否尝试过使用反斜杠转义要保留的
_
字符?请参阅: http://daringfireball.net/projects/markdown/syntax#backslash 文章:
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:
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.