Skip to content

Commit a337f6e

Browse files
Lee Seongjooclaude
andcommitted
Web paste cleanup via the clipboard's HTML flavor (the general fix)
Math renderers (KaTeX, MathJax) emit a hidden screen-reader MathML copy of every formula; the plain-text copy includes both that and the visual, duplicating tokens ("k k는"). RichPaste reads the clipboard HTML, strips the assistive MathML (katex-mathml / mjx-assistive-mml), and converts the remaining visual HTML to text via NSAttributedString — so duplication is gone at the source, generally, for any KaTeX/MathJax page. Verified on a real MyST/KaTeX copy: clean output. A "웹에서 붙여넣기 (정리)" button in the Generate tab uses it (falls back to plain text + heuristic cleanup when there is no HTML flavor). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 19df7e4 commit a337f6e

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎Sources/Core/RichPaste.swift‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import AppKit
2+
3+
/// Clean text from a web copy by reading the clipboard's HTML flavor instead of
4+
/// the lossy plain text. Math renderers (KaTeX, MathJax) emit a visually-hidden
5+
/// screen-reader MathML copy of every formula, which the plain-text copy
6+
/// duplicates ("k k는"). We drop those assistive-MathML nodes, then convert the
7+
/// remaining (visual) HTML to text — so duplication is gone at the source.
8+
enum RichPaste {
9+
/// Best-effort clean text from the pasteboard. Falls back to plain text (with
10+
/// heuristic cleanup) when there is no HTML flavor. nil if the clipboard is empty.
11+
@MainActor
12+
static func cleanText() -> String? {
13+
let pb = NSPasteboard.general
14+
if let html = htmlString(pb) {
15+
let stripped = stripAssistiveMathML(html)
16+
if let data = stripped.data(using: .utf8) {
17+
let opts: [NSAttributedString.DocumentReadingOptionKey: Any] = [
18+
.documentType: NSAttributedString.DocumentType.html,
19+
.characterEncoding: String.Encoding.utf8.rawValue,
20+
]
21+
if let attr = try? NSAttributedString(data: data, options: opts, documentAttributes: nil) {
22+
return TextSplitter.cleanInput(attr.string)
23+
}
24+
}
25+
}
26+
if let s = pb.string(forType: .string) { return TextSplitter.cleanInput(s) }
27+
return nil
28+
}
29+
30+
private static func htmlString(_ pb: NSPasteboard) -> String? {
31+
if let s = pb.string(forType: .html) { return s }
32+
if let d = pb.data(forType: .html) { return String(data: d, encoding: .utf8) }
33+
return nil
34+
}
35+
36+
/// Remove the hidden screen-reader MathML that renderers add (the duplicate).
37+
private static func stripAssistiveMathML(_ html: String) -> String {
38+
var h = html
39+
let patterns = [
40+
#"(?s)<span class="katex-mathml"[^>]*>.*?</span>"#, // KaTeX
41+
#"(?s)<mjx-assistive-mml[^>]*>.*?</mjx-assistive-mml>"#, // MathJax 3
42+
]
43+
for p in patterns {
44+
h = h.replacingOccurrences(of: p, with: "", options: [.regularExpression])
45+
}
46+
return h
47+
}
48+
}

‎Sources/UI/GenerateView.swift‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,17 @@ struct GenerateView: View {
1717

1818
var body: some View {
1919
VStack(alignment: .leading, spacing: 8) {
20-
Text("원본 텍스트").font(.headline)
20+
HStack {
21+
Text("원본 텍스트").font(.headline)
22+
Spacer()
23+
Button {
24+
if let t = RichPaste.cleanText() { app.inputText = t }
25+
} label: {
26+
Label("웹에서 붙여넣기 (정리)", systemImage: "doc.on.clipboard")
27+
}
28+
.controlSize(.small)
29+
.help("클립보드의 웹 서식(HTML)을 읽어 수식·마크다운 잔재를 정리해 붙여넣습니다")
30+
}
2131
TextEditor(text: $app.inputText)
2232
.font(.body).frame(minHeight: 110)
2333
.overlay(RoundedRectangle(cornerRadius: 6).stroke(.quaternary))

0 commit comments

Comments
 (0)