read text aloud

This commit is contained in:
2026-10-23 09:40:20 +02:00
parent 281f5465a6
commit 92b90c0e13
20 changed files with 471 additions and 18 deletions
+21 -4
View File
@@ -41,8 +41,13 @@ public enum ContextAction: Sendable, Equatable {
case delete(String, isDirectory: Bool)
case copyTerminalSelection(TerminalKind)
case pasteIntoTerminal(TerminalKind)
/// Inserts `/wait-what` with the pane's selection into the agent composer.
case waitWhat(TerminalKind)
/// Inserts `/bouwen <name>` into the agent composer for an ontwikkeling folder.
case bouwen(String)
/// Reads a Markdown file's prose aloud.
case readAloudFile(String)
case readAloudText(String)
/// Destructive items read in amber-deep.
public var isDestructive: Bool {
@@ -81,14 +86,14 @@ public enum ContextActions {
return [
.action(.passOnReference(ref, code: hasCode ? code : nil), label: hasCode ? "Pass on selection" : "Pass on reference"),
.action(.copyReference(ref), label: "Copy reference"),
] + showDiff
] + (hasCode ? readAloudSelection(code ?? "", path: reference.path) : []) + showDiff
case .projectRoot:
return [
.action(.newFile(inDirectory: ""), label: "New file"),
.action(.revealInFinder(""), label: "Show in Finder"),
]
case let .file(path):
return fileHead(path) + [
return fileHead(path) + readAloudFile(path) + [
.separator,
.action(.rename(path, isDirectory: false), label: "Rename file"),
.action(.revealInFinder(path), label: "Show in Finder"),
@@ -109,10 +114,12 @@ public enum ContextActions {
.action(.delete(path, isDirectory: true), label: "Delete folder"),
]
case let .terminal(kind, hasSelection):
let waitWhat: [ContextMenuEntry] = hasSelection && kind == .agent
? [.separator, .action(.waitWhat(kind), label: "Wait what?")] : []
return (hasSelection ? [.action(.copyTerminalSelection(kind), label: "Copy")] : [])
+ [.action(.pasteIntoTerminal(kind), label: "Paste")]
+ [.action(.pasteIntoTerminal(kind), label: "Paste")] + waitWhat
case let .gitRow(path, staged):
return fileHead(path) + [
return fileHead(path) + readAloudFile(path) + [
.separator,
staged ? .action(.unstage(path), label: "Unstage changes") : .action(.stage(path), label: "Stage changes"),
.action(.openDiff(path), label: "Open diff"),
@@ -128,6 +135,16 @@ public enum ContextActions {
return String(parts[1])
}
private static func readAloudFile(_ path: String) -> [ContextMenuEntry] {
FileType.isMarkdown(path) ? [.separator, .action(.readAloudFile(path), label: "Read aloud")] : []
}
/// Selected Markdown is read as prose; a selection with no prose left, such as one code block, is read as it is.
private static func readAloudSelection(_ code: String, path: String) -> [ContextMenuEntry] {
let prose = FileType.isMarkdown(path) ? MarkdownSpeech.plainText(MarkdownParser.parse(code)) : ""
return [.separator, .action(.readAloudText(prose.isEmpty ? code : prose), label: "Read aloud selection")]
}
private static func fileHead(_ path: String) -> [ContextMenuEntry] {
let name = PathUtils.basename(path)
return [
@@ -0,0 +1,62 @@
import Foundation
public enum MarkdownSpeech {
/// The prose of a document for a voice to read: one sentence per line, markup, URLs, code blocks and rules left out.
/// A line without closing punctuation gets a full stop, so the voice pauses between blocks.
public static func plainText(_ blocks: [MarkdownBlock]) -> String {
lines(blocks).joined(separator: "\n")
}
/// The text with each `$` that is not before a digit written as "dollar", so the voice does not say "dollar sign".
/// A `$` before a digit stays: the voice reads `$5` as "five dollars".
public static func spoken(_ text: String) -> String {
let characters = Array(text)
var result = ""
for (index, character) in characters.enumerated() {
let next = index + 1 < characters.count ? characters[index + 1] : nil
guard character == "$", !(next?.isNumber ?? false) else {
result.append(character)
continue
}
if let last = result.last, !last.isWhitespace { result.append(" ") }
result.append("dollar")
if let next, !next.isWhitespace { result.append(" ") }
}
return result
}
private static func lines(_ blocks: [MarkdownBlock]) -> [String] {
blocks.flatMap { block -> [String] in
switch block {
case let .heading(_, content), let .paragraph(content): [sentence(content)]
case let .quote(inner): lines(inner)
case let .list(_, items): items.map(sentence)
case let .table(header, _, rows):
([header] + rows).map { cells in sentence(cells.map(text).filter { !$0.isEmpty }.joined(separator: ", ")) }
case .code, .rule: []
}
}
.filter { !$0.isEmpty }
}
private static func sentence(_ inlines: [MarkdownInline]) -> String {
sentence(text(inlines))
}
private static func sentence(_ text: String) -> String {
let trimmed = text.split(whereSeparator: \.isWhitespace).joined(separator: " ")
guard let last = trimmed.last else { return "" }
return ".!?:;".contains(last) ? trimmed : trimmed + "."
}
private static func text(_ inlines: [MarkdownInline]) -> String {
inlines.map { inline in
switch inline {
case let .text(text), let .code(text): text
case let .strong(inner), let .emphasis(inner), let .strikethrough(inner), let .link(inner, _, _): text(inner)
case let .image(alt, _, _): alt
}
}
.joined()
}
}