Skip to content

适用于macOS12及以上的markdown图片链接自动转换为瀑布流图片的应用程序

约 454 字大约 2 分钟

Code

2025-02-20

下载构建完成的App(未签名,未在其它设备验证适用性)

import SwiftUI

struct ContentView: View {
    @State private var inputImages: String = ""
    @State private var output: String = ""
    @State private var isProcessing: Bool = false
    @State private var showAlert: Bool = false

    var body: some View {
        VStack {
            Text("Input Markdown Picture URL Code")
                .font(.headline)
                .padding(.top)
            TextEditor(text: $inputImages)
                .font(.custom("Monaco", size: 14)) // 设置为 Monaco 字体
                .padding()
                .background(Color.clear) // 确保背景透明
                .frame(minWidth: 300, minHeight: 100)

            HStack {
                Button(action: {
                    copyToClipboard(output)
                    showAlert = true // 显示复制提示
                }) {
                    Label("复制", systemImage: "doc.on.doc") // 使用系统符号
                }
                .buttonStyle(.borderedProminent) // 使用原生按钮样式
                .disabled(output.isEmpty) // 如果输出为空,禁用按钮
            }
            .padding(.top)

            Text("Processed Markdown code")
                .font(.headline)
                .padding(.top)

            TextEditor(text: $output)
                .font(.custom("Monaco", size: 14)) // 设置为 Monaco 字体
                .padding()
                .background(Color.clear) // 确保背景透明
                .disabled(true) // 禁用编辑输出文本框

            Spacer()
        }
        .padding()
        .overlay(
            Group {
                if isProcessing {
                    ProgressView("处理中...")
                        .progressViewStyle(CircularProgressViewStyle())
                }
            }
        )
        .alert(isPresented: $showAlert) {
            Alert(title: Text("已复制"), message: Text("处理后的文本已复制到剪贴板。"), dismissButton: .default(Text("确定")))
        }
        .onChange(of: inputImages) { newValue in
            // 使用异步处理来减少卡顿
            isProcessing = true
            DispatchQueue.global(qos: .userInitiated).async {
                let processedOutput = generateMarkdown(from: newValue)
                DispatchQueue.main.async {
                    output = processedOutput
                    isProcessing = false
                }
            }
        }
    }

    func generateMarkdown(from input: String) -> String {
        // 清理输入字符串,确保换行符一致
        let cleanedInput = input.replacingOccurrences(of: "\r\n", with: "\n") // 将 \r\n 替换为 \n
        let images = cleanedInput.split(separator: "\n").map(String.init) // 使用换行符分割输入字符串

        var output = "::: card-masonry\n"
        
        for image in images {
            output += image + "\n\n" // 每行后面添加两个换行符
        }
        
        output += ":::"

        // 打印调试信息
        print("生成的输出: \(output.debugDescription)")
        
        return output
    }

    func copyToClipboard(_ text: String) {
        let pasteboard = NSPasteboard.general
        pasteboard.clearContents()
        pasteboard.setString(text, forType: .string)
    }
}