[clang-tools-extra] [clangd] Improve Markup Rendering (PR #140498)
Maksim Ivanov via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 11 19:45:16 PDT 2025
================
@@ -151,16 +151,91 @@ bool needsLeadingEscape(char C, llvm::StringRef Before, llvm::StringRef After,
}
}
+/// \brief Tests whether \p C should be backslash-escaped in markdown.
+///
+/// The MarkupContent LSP specification defines that `markdown` content needs to
+/// follow GFM (GitHub Flavored Markdown) rules. And we can assume that markdown
+/// is rendered on the client side. This means we do not need to escape any
+/// markdown constructs.
+/// The only exception is when the client does not support HTML rendering in
+/// markdown. In that case, we need to escape HTML tags and HTML entities.
+///
+/// **FIXME:** handle the case when the client does support HTML rendering in
+/// markdown. For this, the LSP server needs to check the
+/// [supportsHtml
+/// capability](https://github.com/microsoft/language-server-protocol/issues/1344)
+/// of the client.
+///
+/// \param C The character to check.
+/// \param After The string that follows \p C .
+// This is used to determine if \p C is part of a tag or an entity reference.
+/// \returns true if \p C should be escaped, false otherwise.
+bool needsLeadingEscapeMarkdown(char C, llvm::StringRef After) {
+ switch (C) {
+ case '<': // HTML tag (or autolink, which we choose not to escape)
+ return looksLikeTag(After);
+ case '&': { // HTML entity reference
+ auto End = After.find(';');
+ if (End == llvm::StringRef::npos)
+ return false;
+ llvm::StringRef Content = After.substr(0, End);
+ if (Content.consume_front("#")) {
+ if (Content.consume_front("x") || Content.consume_front("X"))
+ return llvm::all_of(Content, llvm::isHexDigit);
+ return llvm::all_of(Content, llvm::isDigit);
+ }
+ return llvm::all_of(Content, llvm::isAlpha);
+ }
+ default:
+ return false;
+ }
+}
+
+bool needsLeadingEscape(char C, llvm::StringRef Before, llvm::StringRef After,
+ bool StartsLine, bool EscapeMarkdown) {
+ if (EscapeMarkdown)
+ return needsLeadingEscapePlaintext(C, Before, After, StartsLine);
+ return needsLeadingEscapeMarkdown(C, After);
+}
+
/// Escape a markdown text block. Ensures the punctuation will not introduce
----------------
emaxx-google wrote:
nit: Update the comment (e.g., the second sentence is only applicable when `EscapeMarkdown` is `true` if I understand correctly).
https://github.com/llvm/llvm-project/pull/140498
More information about the cfe-commits
mailing list