### Markdown file link example Source: https://github.com/afollestad/blockinputkit/blob/main/README.md This is the Markdown link that the file-link suggestion inserts. It renders as a chip and leaves the caret after the accepted file chip. ```markdown [README.md](file:///resolved/README.md) ``` -------------------------------- ### Begin, update, and finish provisional text replacement Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Starts a provisional text replacement session, updates it with cumulative text, and commits it. Only one session may be active; interim updates replace the originally snapshotted range and may continue after reconfiguration. Committing registers one 'Text Edit' undo operation; cancelling restores the original block and selection without undo history. ```swift switch editor.beginProvisionalTextReplacement() { case .started(let session): _ = editor.updateProvisionalTextReplacement(session, text: "live cumulative text") _ = editor.finishProvisionalTextReplacement(session, disposition: .commit) case .unavailable(let reason): print("Cannot begin provisional text:", reason) } ``` -------------------------------- ### Custom streaming protocols for Markdown Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Implement BlockInputMarkdownLineReader and BlockInputMarkdownWriter to stream Markdown data. The example shows a database-backed reader and writer, then uses them with BlockInputDocument.readMarkdown and writeMarkdown. ```swift struct DatabaseLineReader: BlockInputMarkdownLineReader { mutating func readMarkdownLine() async throws -> String? { nil } } struct ChunkWriter: BlockInputMarkdownWriter { mutating func writeMarkdown(_ chunk: String) async throws { // Persist this chunk immediately. } } var reader = DatabaseLineReader() let streamed = try await BlockInputDocument.readMarkdown(from: &reader) var writer = ChunkWriter() try await streamed.writeMarkdown(to: &writer) ``` -------------------------------- ### Configure height sizing with default and maximum visible line counts Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Creates a BlockInputConfiguration with height sizing that starts at 2 visible lines and caps at 8. Height is based on rendered content, so wrapping, block chrome, tables, images, and code blocks can affect growth. Extra content scrolls vertically. ```swift let configuration = BlockInputConfiguration( document: document, heightSizing: BlockInputEditorHeightSizing( defaultVisibleLineCount: 2, maximumVisibleLineCount: 8 ) ) ``` -------------------------------- ### Run local demo script Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Run the local demo with ./scripts/run-demo.sh. The default Overview note is a feature tour; the 100K note is a large-document performance sample. The demo includes file mention suggestions, slash commands with .anywhere availability, and a Caret/Overlay completion placement control. ```sh ./scripts/run-demo.sh ``` -------------------------------- ### Basic configuration with BlockInputDocument and BlockInputConfiguration Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Create a document with initial blocks and a configuration object for in-memory editing. Each block owns its own text input, as shown by the quote block. ```swift import BlockInputKit let document = BlockInputDocument(blocks: [ BlockInputBlock(kind: .paragraph, text: "Hello"), BlockInputBlock(kind: .quote, text: "Each block owns its own text input.") ]) let configuration = BlockInputConfiguration(document: document) ``` -------------------------------- ### Configure selectAllBehavior to .document Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Opt in to make Cmd+A select the whole document immediately when content exists. By default, Cmd+A selects focused content first, with special handling for empty text blocks. ```swift BlockInputConfiguration(selectAllBehavior: .document) ``` -------------------------------- ### Run validation scripts (build, test, lint) Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Runs the project's build, test, and lint scripts. Linting uses the repo SwiftLint configuration in `.swiftlint.yml`; keep project exceptions centralized there instead of adding inline suppression comments. ```sh ./scripts/build.sh ./scripts/test.sh ./scripts/lint.sh ``` -------------------------------- ### Configure Layout and Style Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Use this configuration to match the editor's appearance to the host surface. Set editorHorizontalInset, editorVerticalInset, and blockVerticalInsetMultiplier for spacing. Style properties like editorSurface, imageBlock, and fileChip control visual details; nil editor surface colors are transparent. ```swift let configuration = BlockInputConfiguration( document: document, allowsBlockReordering: true, editorHorizontalInset: 20, editorVerticalInset: 8, blockVerticalInsetMultiplier: 0.75, style: BlockInputStyle( editorSurface: BlockInputEditorSurfaceStyle( editorBackgroundColor: nil, scrollBackgroundColor: nil, collectionBackgroundColor: nil, chrome: BlockInputEditorChromeStyle( fillColor: NSColor.labelColor.withAlphaComponent(0.08), strokeColor: NSColor.labelColor.withAlphaComponent(0.18), borderWidth: 1, cornerRadius: 18, clipsContentToShape: true ) ), imageBlock: BlockInputImageBlockStyle(cornerRadius: 8), fileChip: BlockInputInlineChipStyle( fillColor: NSColor.controlAccentColor.withAlphaComponent(0.14), strokeColor: NSColor.controlAccentColor.withAlphaComponent(0.24), foregroundColor: .labelColor, cornerRadius: 5 ), slashCommandChip: BlockInputInlineChipStyle( fillColor: NSColor.controlAccentColor.withAlphaComponent(0.14), strokeColor: NSColor.controlAccentColor.withAlphaComponent(0.24), foregroundColor: .labelColor, cornerRadius: 5 ), rawSlashCommandChip: BlockInputInlineChipStyle( fillColor: NSColor.controlAccentColor.withAlphaComponent(0.14), strokeColor: NSColor.controlAccentColor.withAlphaComponent(0.24), foregroundColor: .labelColor, cornerRadius: 5 ) ) ) ``` -------------------------------- ### Provide modal overlay for link and image modals Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Use this snippet to host link and image modals in a higher overlay surface. The `modalOverlayProvider` receives the modal context and returns a `BlockInputModalOverlay` with a container and frame in that container's coordinate space. ```swift let configuration = BlockInputConfiguration( document: document, modalOverlayProvider: { context in let container = overlayView return BlockInputModalOverlay( container: container, frame: context.modalFrame(in: container) ) } ) ``` -------------------------------- ### Create a file link suggestion for mentions Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Use this snippet to return a file-link suggestion for file mentions in paragraphs or headings. The helper inserts a Markdown file link followed by a space and renders the link as a chip. ```swift BlockInputCompletionSuggestion.fileLink(fileURL: readmeURL) ``` -------------------------------- ### Custom file drop handler with fileDropHandler Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Use this snippet to copy dropped files into project storage and return custom insertion references. The handler receives a context with files, and you can return .insert(references) to insert file chips or image blocks. Return .useDefault for built-in insertion, .handled when the host handled the drop, or .cancel to reject it. ```swift let projectURL = URL(filePath: "/Users/me/Project", directoryHint: .isDirectory) let configuration = BlockInputConfiguration( document: document, fileBaseURL: projectURL, imageBaseURL: projectURL, fileDropHandler: { context in let references = try context.files.map { file in let destination = projectURL .appending(path: "Assets", directoryHint: .isDirectory) .appending(path: file.url.lastPathComponent) try FileManager.default.copyItem(at: file.url, to: destination) return BlockInputFileDropReference( kind: file.defaultKind, source: "Assets/\(destination.lastPathComponent)", label: file.defaultLabel ) } return .insert(references) } ) ``` -------------------------------- ### Configure BlockInputKit with a document store Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Use BlockInputMemoryDocumentStore when the host owns persistence or wants granular mutation handling. The configuration receives callbacks for document mutations and changes, printing the updated markdown. ```swift let store = BlockInputMemoryDocumentStore(document: document) let configuration = BlockInputConfiguration( documentStore: store, onDocumentMutation: { change in print("Applied edit:", change) }, onDocumentChange: { updatedDocument in print(updatedDocument.markdown) } ) ``` -------------------------------- ### Raw file mention chips configuration Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Sets rawFileMentionChips to render literal @-prefixed file path tokens as file chips while the underlying Markdown stays plain text. The @ must sit at a token boundary and be followed by an absolute or home-relative path. ```swift let configuration = BlockInputConfiguration( document: document, rawFileMentionChips: true ) ``` -------------------------------- ### Animate height changes with transition callback Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Configures the editor with height sizing and a transition callback that animates height changes using NSAnimationContext. The callback updates the height constraint and animates layout changes. Use this when the host wants to animate height changes. ```swift editor.configure(BlockInputConfiguration( document: document, heightSizing: BlockInputEditorHeightSizing( defaultVisibleLineCount: 2, maximumVisibleLineCount: 8, animation: .default, onPreferredHeightTransition: { transition in editorHeightConstraint.constant = transition.targetHeight guard let animation = transition.animation else { editor.superview?.layoutSubtreeIfNeeded() return } NSAnimationContext.runAnimationGroup { context in context.duration = animation.duration editor.superview?.layoutSubtreeIfNeeded() } } ) )) ``` -------------------------------- ### BlockInputDocument and BlockInputConfiguration for text links Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Shows how to configure BlockInputKit to preserve image source text and render images as text links using .preserveSourceText and .textLinks. ```swift let document = BlockInputDocument( markdown: markdown, imageParsingMode: .preserveSourceText ) let configuration = BlockInputConfiguration( document: document, imagePresentation: .textLinks ) ``` -------------------------------- ### Register host keyboard shortcuts with BlockInputConfiguration Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Use to intercept keys before editor's built-in behavior. Only dictionary keys are intercepted; unregistered keys keep normal behavior. Handlers run synchronously on the main actor before default editor mutation/navigation. Return .handled, .ignored, or .performDefault(.returnKey) to control behavior. ```swift let configuration = BlockInputConfiguration( keyboardShortcuts: [ .returnKey: { context in sendMessage() return .handled }, .shiftReturn: { _ in .performDefault(.returnKey) }, .optionReturn: { context in runCustomAction(selection: context.selection) return .handled } ] ) ``` -------------------------------- ### Configure height sizing with preferred height change callback Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Configures a BlockInputView with height sizing and a callback that updates a height constraint when the preferred height changes. Use this to keep the editor's height in sync with its content. ```swift let editor = BlockInputView() editor.configure(BlockInputConfiguration( document: document, heightSizing: BlockInputEditorHeightSizing( defaultVisibleLineCount: 2, maximumVisibleLineCount: 8, onPreferredHeightChange: { height in editorHeightConstraint.constant = height } ) )) ``` -------------------------------- ### Configure overlay popup placement with custom frame Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Use this snippet when the host needs to choose the popup parent view and frame. The overlay provider returns a container and frame in that container's coordinate space, with a 12-point vertical offset and the popup's height. ```swift BlockInputCompletionPopupConfiguration(placement: .overlay) { context in let container = context.editorView var frame = context.editorFrame(in: container) frame.origin.y += 12 frame.size.height = context.popupSize.height return BlockInputCompletionPopupOverlay(container: container, frame: frame) } ``` -------------------------------- ### Configure completion popup placement with caret Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Use this snippet to set the completion popup to appear at the caret position. The `completionPopupConfiguration` is set with a placement of `.caret`, which is the default behavior. ```swift let configuration = BlockInputConfiguration( document: document, completionProvider: provider, completionPopupConfiguration: BlockInputCompletionPopupConfiguration(placement: .caret) ) ``` -------------------------------- ### Raw slash command chips with .rawToken insertion Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Configures rawSlashCommandChips and uses .rawToken insertion so the underlying Markdown stays as /command text while still rendering the token as a chip. Raw chips are visual only; editing, selection, copy, accessibility, and Markdown export treat them as normal text. ```swift let configuration = BlockInputConfiguration( document: document, rawSlashCommandChips: true, slashCommandAvailability: .documentStart ) BlockInputCompletionSuggestion.slashCommand( title: "Review GitHub PR", uri: "myapp://commands/review-github-pr", label: "review-github-pr", insertionStyle: .rawToken ) ``` -------------------------------- ### Slash command suggestion with default link insertion Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Returns a slash-command suggestion when context.trigger == .slashCommand. The helper inserts command source followed by a space so the caret lands after the accepted chip; by default that source is a Markdown link. ```swift BlockInputCompletionSuggestion.slashCommand( title: "Insert table", uri: "myapp://commands/table", label: "table" ) ``` -------------------------------- ### Perform semantic commands with BlockInputEditor.performCommand Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Use to trigger editor commands like bold, insert link, or insert table programmatically. Check canPerformCommand before performing optional commands. Commands cover undo, redo, select all, clipboard, inline formatting, links, images, and table actions. ```swift editor.performCommand(.bold) editor.performCommand(.insertLink(BlockInputInsertLinkCommand( text: "Docs", urlString: "https://example.com", presentation: .automatic ))) if editor.canPerformCommand(.insertTable) { editor.performCommand(.insertTable) } ``` -------------------------------- ### Inline image with Markdown Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Demonstrates how remote images sharing a line with other text render inline, matching GitHub behavior. ```markdown Codex flags **![P1](https://img.shields.io/badge/P1-orange) Add the required commit trailer** here. ``` -------------------------------- ### Async Markdown read/write with BlockInputDocument Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Use async Markdown APIs when reading or writing files. Reading from a URL, writing to a URL, parsing Markdown with optional image parsing mode, and obtaining the Markdown snapshot. ```swift let url = URL(filePath: "/tmp/note.md") let document = try await BlockInputDocument.readingMarkdown(from: url) try await document.writeMarkdown(to: url) let parsed = await BlockInputDocument.parsingMarkdown("# Heading") let textImages = await BlockInputDocument.parsingMarkdown( markdown, imageParsingMode: .preserveSourceText ) let markdown = await parsed.markdownSnapshot() ``` -------------------------------- ### Host BlockInputView in AppKit Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Use BlockInputView when the host needs direct editor access. Configure the view with a configuration object after creation. ```swift let editor = BlockInputView() editor.configure(configuration) ``` -------------------------------- ### Add BlockInputKit product to macOS target Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Add the BlockInputKit library product to your macOS target's dependencies in Package.swift. This makes the module available to your target. ```swift .product(name: "BlockInputKit", package: "BlockInputKit") ``` -------------------------------- ### Use BlockInputEditorCommandDispatcher in SwiftUI Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Keep a dispatcher in SwiftUI state and pass it through configuration to trigger commands from buttons. Use commandDispatcher.perform to execute commands like bold. ```swift @State private var commandDispatcher = BlockInputEditorCommandDispatcher() BlockInputEditor(configuration: BlockInputConfiguration( commandDispatcher: commandDispatcher )) Button("Bold") { commandDispatcher.perform(.bold) } ``` -------------------------------- ### Add BlockInputKit as a Swift Package dependency Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Add this package dependency to your Package.swift file to include BlockInputKit in your macOS project. Pin the dependency to an exact version (e.g., 'x.y.z') for reproducibility. ```swift .package(url: "https://github.com/afollestad/BlockInputKit.git", exact: "x.y.z") ``` -------------------------------- ### Inline argument hints with inlineHintProvider Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Uses inlineHintProvider for visual-only slash-command argument hints after the active caret. Hints are not inserted into document text, Markdown export, undo, pasteboard contents, completion ranges, or accessibility value text. ```swift let argumentHints = BlockInputSlashCommandArgumentHints([ "review-github-pr": "[PR URL]" ]) let configuration = BlockInputConfiguration( document: document, slashCommandAvailability: .documentStart, inlineHintProvider: { argumentHints.inlineHint(for: $0) } ) ``` -------------------------------- ### Verify snapshot tests Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Verifies the representative snapshot suite. When no test identifier is provided, `./scripts/snapshots.sh` defaults to `BlockInputKitTests/BlockInputViewSnapshotTests`. ```sh ./scripts/snapshots.sh verify ``` -------------------------------- ### Customize popup style with overlay provider Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Use this snippet to customize the built-in popup chrome (background, border, highlighted row) while using an overlay provider. The `style` parameter accepts a `BlockInputCompletionPopupStyle` with optional colors and corner radius. ```swift BlockInputCompletionPopupConfiguration( placement: .overlay, style: BlockInputCompletionPopupStyle( backgroundColor: popupBackground, borderColor: popupBorder, highlightedRowBackgroundColor: selectedRowBackground, highlightedRowCornerRadius: selectedRowCornerRadius ), overlayProvider: overlayProvider ) ``` -------------------------------- ### Markdown and HTML image syntax Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Shows the Markdown image syntax and HTML image tag that parse as standalone image blocks by default. ```markdown ![Alt Text](https://example.com/image.png) Alt Text ``` -------------------------------- ### Set Placeholder Text Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Use this configuration to display subtle placeholder text in an empty editor. The placeholder is visual only and is not inserted into the document, included in Markdown export, or reported through document-change callbacks. ```swift let configuration = BlockInputConfiguration( document: BlockInputDocument(), placeholder: "Ask anything" ) ``` -------------------------------- ### Configure custom URL opener for file links Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Use this snippet to override the default URL opening behavior. The `urlOpener` closure receives the URL and can route it to a custom handler, such as `routeEditorURL`. ```swift let configuration = BlockInputConfiguration( document: document, urlOpener: { url in routeEditorURL(url) } ) ``` -------------------------------- ### Host BlockInputEditor in SwiftUI Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Use BlockInputEditor in a SwiftUI view to display the editor. Bind the focus state to control editor focus programmatically. ```swift import BlockInputKit import SwiftUI struct EditorScreen: View { let configuration: BlockInputConfiguration @State private var isEditorFocused = false var body: some View { BlockInputEditor( configuration: configuration, isFocused: $isEditorFocused ) } } ``` -------------------------------- ### Verify or record a focused snapshot test Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Verifies or records a focused snapshot test by specifying the test identifier. The default test identifier is `BlockInputKitTests/BlockInputViewSnapshotTests`. ```sh ./scripts/snapshots.sh verify BlockInputKitTests/BlockInputViewSnapshotTests ./scripts/snapshots.sh record BlockInputKitTests/BlockInputViewSnapshotTests ``` -------------------------------- ### Enable Read-Only Mode Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Set isEditable to false to keep content selectable, copyable, scrollable, focusable, and accessible while blocking editor mutations. Existing text is dimmed and disabledCursor communicates the read-only state. Suppresses mutation commands, typing, paste/drop, reordering, checklist toggles, table/image editing, completion acceptance, and edit modals. ```swift let configuration = BlockInputConfiguration( document: document, isEditable: false, disabledCursor: .operationNotAllowed ) ``` -------------------------------- ### Disable drops with allowsDrops Source: https://github.com/afollestad/blockinputkit/blob/main/README.md Use this snippet to disable all editor-owned drop features, such as drop carets, collection drop indicators, file insertion, and block-reorder drag/drop. Programmatic insertion APIs remain available. ```swift let configuration = BlockInputConfiguration( document: document, allowsDrops: false ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.