Files
ghostgram/submodules/TelegramUI/Sources/PastedMarkdownConversion.swift
ichmagmaus111andClaude Opus 5.5 0622057a4f Ghostgram: open-source release
Ghostgram for iOS, rebuilt from scratch on Telegram iOS 13.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 17:57:09 +01:00

31 lines
1.7 KiB
Swift

import Foundation
import TelegramCore
import AccountContext
import TextFormat
import BrowserUI
/// Parses pasted plain text as CommonMark markdown (the same parser used on the rich-message send path)
/// and returns a `ChatInputContent` when the result carries formatting or structure that ordinary
/// plain-text paste would not already produce. Returns nil when:
/// - the text is unparseable / empty / iOS < 15 (`inputRichTextAttributeFromText` returns nil), or
/// - the parsed content is nothing but unformatted body paragraphs (`pastedMarkdownContentIsRicherThanPlain`).
/// Hosts convert the returned content to a `Document` (native editor) or an attributed string (legacy
/// field) with modules they already depend on.
///
/// Self-referential links are stripped BEFORE the richness gate: the markdown parser applies the GFM
/// autolink extension, so pasting a bare `https://…` (or `www.…`, or an email) yields a link run whose
/// label is the URL itself. That entity alone made the paste look "richer than plain", and the URL then
/// landed in the composer as a text link (`textUrl`) instead of as plain text the server auto-detects.
/// A genuine text link — `[label](url)`, a label that differs from its target — is untouched and still
/// classifies as rich.
func chatInputContentFromPastedMarkdown(context: AccountContext, plainText: String) -> ChatInputContent? {
guard let attribute = inputRichTextAttributeFromText(context: context, text: plainText) else {
return nil
}
let content = chatInputContentStrippingSelfReferentialLinks(chatInputContent(fromInstantPage: attribute.instantPage))
guard pastedMarkdownContentIsRicherThanPlain(content) else {
return nil
}
return content
}