Files
paseo/packages/react-native-enriched-markdown/ios/utils/ENRMCodeBlockContent.mm
T
1ff119dc2a feat(app): copy a quote's body with one tap (#10608)
Agents often write drafts as quotes meant to be pasted elsewhere. A plain
quote now has a copy control on web, desktop, Android and iOS. It copies
the quote's body without the `>` markers and with its nested Markdown. A
nested quote, a code block or a Mermaid diagram inside it keeps its own
control. Admonitions get no control.

Web reads the rendered quote at press time, so a streaming quote copies
what it shows, and writes the same two clipboard flavors as selection
copy. A rendered Mermaid diagram copies as the fence it shows, and an
admonition inside a copied quote copies as its `> [!TYPE]` source. Code
blocks and quotes share one copy button with a Copied state and an error
toast when the clipboard refuses the write. On compact layouts the
control is always visible in a band above the quote body. On wide
layouts it shows on hover or focus and the quote keeps a right gutter.

Native code blocks and quotes share one header copy control with the
Lucide Copy glyph. A quote reserves the code block's header band above
its body, so the control never covers text at any font size. Native copy
emits the quote body as written, and a copied fence grows longer than
any fence inside its code.

Supersedes #9429 and #9319. Refs discussion #9303.

Co-authored-by: Pedro Vieira <pedro.vieira@dremio.com>
Co-authored-by: Phieu Dinh <phieu@fiu.one>
2026-11-25 13:36:01 +07:00

102 lines
3.4 KiB
Plaintext

#import "ENRMCodeBlockContent.h"
#include "CodeBlockLanguages.hpp"
#import "ENRMUIKit.h"
#import "MarkdownASTNode.h"
#import "ParagraphStyleUtils.h"
#import "StyleConfig.h"
static void ENRMAppendNodeContent(MarkdownASTNode *node, NSMutableString *output)
{
if (node.content.length > 0) {
[output appendString:node.content];
}
for (MarkdownASTNode *child in node.children) {
ENRMAppendNodeContent(child, output);
}
}
NSString *ENRMCodeBlockExtractCode(MarkdownASTNode *node)
{
NSMutableString *code = [NSMutableString string];
ENRMAppendNodeContent(node, code);
NSUInteger end = code.length;
while (end > 0 && [code characterAtIndex:end - 1] == '\n') {
end--;
}
return [code substringToIndex:end];
}
NSString *ENRMCodeBlockLanguage(MarkdownASTNode *node)
{
NSString *language = node.attributes[@"language"];
return language.length > 0 ? language : nil;
}
NSString *ENRMCodeBlockFenceChar(MarkdownASTNode *node)
{
NSString *fenceChar = node.attributes[@"fenceChar"];
return fenceChar.length > 0 ? fenceChar : @"`";
}
NSString *ENRMCodeBlockFencedMarkdown(NSString *code, NSString *language, NSString *fenceChar)
{
// A fence must be longer than any run of its character in the code, or an inner
// fence (a Markdown example) closes the block early.
unichar fenceUnit = [fenceChar characterAtIndex:0];
NSUInteger longestRun = 0;
NSUInteger run = 0;
for (NSUInteger index = 0; index < code.length; index++) {
run = [code characterAtIndex:index] == fenceUnit ? run + 1 : 0;
longestRun = MAX(longestRun, run);
}
NSUInteger fenceLength = MAX((NSUInteger)3, longestRun + 1);
NSString *fence = [@"" stringByPaddingToLength:fenceLength withString:fenceChar startingAtIndex:0];
return [NSString stringWithFormat:@"%@%@\n%@\n%@", fence, language ?: @"", code, fence];
}
NSString *ENRMCodeBlockDisplayLanguageName(NSString *language)
{
if (language.length == 0) {
return @"";
}
std::string display = Markdown::displayNameForLanguage(language.UTF8String ?: "");
return [NSString stringWithUTF8String:display.c_str()] ?: language;
}
void ENRMApplyCodeBlockTextAttributes(NSMutableAttributedString *string, NSRange range, StyleConfig *config)
{
if (range.length == 0) {
return;
}
UIFont *font = [config codeBlockFont];
RCTUIColor *color = [config codeBlockColor];
if (color) {
[string addAttributes:@{NSFontAttributeName : font, NSForegroundColorAttributeName : color} range:range];
} else {
[string addAttribute:NSFontAttributeName value:font range:range];
}
CGFloat lineHeight = [config codeBlockLineHeight];
if (lineHeight > 0) {
applyLineHeight(string, range, lineHeight);
}
NSMutableParagraphStyle *paragraphStyle = [getOrCreateParagraphStyle(string, range.location) mutableCopy];
paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
paragraphStyle.alignment = NSTextAlignmentLeft;
// Re-clamp: unlike paragraphs, code blocks reserve their height analytically as
// lineCount * lineHeight (ENRMCodeBlockCodeHeight), so a line must not grow past the
// configured lineHeight or a tall run (emoji, wide glyph) would overflow and clip the block.
if (lineHeight > 0) {
paragraphStyle.maximumLineHeight = lineHeight;
}
[string addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
if (lineHeight > 0) {
applyBaselineOffset(string, range);
}
ENRMPinLineMetricsToStyledFonts(string, range);
}