Markdown 链接无法正确解析 URL 中的括号

继续讨论 URLs in parenthesis do not turn into links

RFC2396 的 2.3 节 规定:

未保留字符可以被转义而不改变 URI 的语义,但除非 URI 被用于不允许出现未转义字符的上下文,否则不应这样做。

括号就是这样的字符。当粘贴到 Discourse 时,使用这些字符的 URL,例如在某些上下文的高级搜索的查询字符串中,会被误解为 Markdown URL 语法的一部分,从而无法使用。

例如,以下 URL 将检索由我的协会发布的合法存管图书列表:

https://www.depotlegal.be/Depot/form.aspx?SC=KBRVITRINE1#/Search/(query:(AdvancedQuery:(queryGroups:!((queryClauses:!((index:KBR264b_idx,logical:0,operator:0,otherValue:!n,value:‘petites%20singularités’))

虽然如上所示粘贴时可以正常工作,但用作 Markdown 链接时,它将不再起作用:

[anchor](url) → [anchor](https://www.depotlegal.be/Depot/form.aspx?SC=KBRVITRINE1#/Search/(query:(AdvancedQuery:(queryGroups:!((queryClauses:!((index:KBR264b_idx,logical:0,operator:0,otherValue:!n,value:‘petites%20singularités’))

此外,当通过电子邮件收到此类 URL 时,结果是:

Recherche avancée - Depot)))),ForceSearch:!t,Grid:!n,Page:0,PageRange:3,QueryString:!n,ResultSize:-1,ScenarioCode:KBRVITRINE1,SearchContext:1))

其中“Recherche avancée - Depot”是正确解释的链接标题(可点击),其余部分是垃圾——并且不在可点击链接中。(电子邮件设置为以 HTML 格式接收)重建 HTML 并不能修复链接。

这看起来更像是 URL 和 CommonMark 的问题。如果在末尾添加 )))))) 来平衡开括号和闭括号,Markdown 链接就能被正确解析。

链接

[链接](https://www.depotlegal.be/Depot/form.aspx?SC=KBRVITRINE1#/Search/(query:(AdvancedQuery:(queryGroups:!((queryClauses:!((index:KBR264b_idx,logical:0,operator:0,otherValue:!n,value:'petites%20singularit%C3%A9s'))))))))

这与 CommonMark 规范 中的行为一致,而 markdown-it 引擎(Discourse 使用的引擎)遵循该规范。

一个非空字符序列,它不以 < 开头,不包含 ASCII 控制字符空格字符,并且仅在以下情况下包含括号:(a) 它们被反斜杠转义,或 (b) 它们是未转义括号的平衡对的一部分。(实现可能会对括号嵌套施加限制以避免性能问题,但至少应支持三级嵌套。)

也可以在 markdown-it 演示 中进行测试。

3 个赞

该规范似乎相当清晰,用户也可以转义不平衡的括号

[link](https://www.depotlegal.be/Depot/form.aspx?SC=KBRVITRINE1#/Search/\\(query:\\(AdvancedQuery:\\(queryGroups:!\\(\\(queryClauses:!\\(\\(index:KBR264b_idx,logical:0,operator:0,otherValue:!n,value:'petites%20singularit%C3%A9s'\\))\n

变成

[link](Recherche avancée - Depot)\n

由于这符合规范中的描述,我将其移至 Feature 讨论区。

4 个赞

抱歉,大多数时候不会进行括号平衡或转义 :slight_smile:

我认为还有一个关于此的 Bug 主题 Ensure that links containing )) still render and work correctly

1 个赞