Commit ad494f660e

ad494f660efb99b2adbf3db9dbe0314db3762426

parent: 94a73856e1

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-21 19:43 UTC

Read bracket link paths that run onto the next line as Emacs does

Layout: unified · split

Sources/OrgCore/Export/ASCIIExport.swift +1 −1
@@ -1244,7 +1244,7 @@ final class ASCIIRenderer {
12441244 if node.firstChild(.linkDescription)?.descendants().contains(where: { $0.kind == .link }) == true {
12451245 throw Unsupported(reason: "Links in link descriptions")
12461246 }
1247 let pathToken = node.tokens.first { $0.kind == .linkPath }?.text
1247 let pathToken = node.linkPath
12481248 var raw = pathToken ?? node.text
12491249 if pathToken == nil, raw.hasPrefix("<"), raw.hasSuffix(">") { raw = String(raw.dropFirst().dropLast()) }
12501250 var description: String? = node.firstChild(.linkDescription) == nil ? nil : try transcode(children)
Sources/OrgCore/Export/ASCIIText.swift +1 −1
@@ -461,7 +461,7 @@ struct ASCIIText {
461461
462462 func link(_ node: SyntaxNode) -> String {
463463 let description = node.firstChild(.linkDescription).map { inline($0) }
464 guard let path = node.tokens.first(where: { $0.kind == .linkPath })?.text else {
464 guard let path = node.linkPath else {
465465 var target = node.text
466466 if target.hasPrefix("<"), target.hasSuffix(">") { target = String(target.dropFirst().dropLast()) }
467467 // A radio link is its text.
Sources/OrgCore/Export/ExportOptions.swift +1 −1
@@ -205,7 +205,7 @@ public enum ExportPreprocess {
205205 let rebased = rebasingLinks(description.text, from: directory, to: includer, plain: true)
206206 if rebased != description.text { edits.append((description.range, rebased)) }
207207 }
208 if let path = node.tokens.first(where: { $0.kind == .linkPath }), let rebased = rebasedPath(path.text, from: directory, to: includer, bracket: true) {
208 if let path = node.tokens.first(where: { $0.kind == .linkPath }), let text = node.linkPath, let rebased = rebasedPath(text, from: directory, to: includer, bracket: true) {
209209 edits.append((path.range, rebased))
210210 }
211211 } else if whole.hasPrefix("<"), whole.hasSuffix(">") {
Sources/OrgCore/Export/HTMLExport.swift +1 −1
@@ -856,7 +856,7 @@ final class HTMLRenderer {
856856 static let imageExtensions: Set<String> = ["png", "jpg", "jpeg", "gif", "svg", "webp", "bmp", "tif", "tiff", "avif"]
857857
858858 private func link(_ node: SyntaxNode) -> String {
859 let path = node.tokens.first { $0.kind == .linkPath }?.text
859 let path = node.linkPath
860860 let description = node.firstChild(.linkDescription)
861861 guard let path else {
862862 // A radio link goes to its target.
Sources/OrgCore/Export/LaTeXExport.swift +1 −1
@@ -1525,7 +1525,7 @@ final class LaTeXRenderer {
15251525 // MARK: Links
15261526
15271527 private func linkTarget(_ node: SyntaxNode) -> (type: String, path: String) {
1528 var raw = node.tokens.first { $0.kind == .linkPath }?.text ?? node.text
1528 var raw = node.linkPath ?? node.text
15291529 if node.tokens.first(where: { $0.kind == .linkPath }) == nil, raw.hasPrefix("<"), raw.hasSuffix(">") {
15301530 raw = String(raw.dropFirst().dropLast())
15311531 }
Sources/OrgCore/Export/MarkdownExport.swift +1 −1
@@ -358,7 +358,7 @@ final class MarkdownRenderer {
358358 }
359359
360360 private func link(_ node: SyntaxNode) -> String {
361 guard let path = node.tokens.first(where: { $0.kind == .linkPath })?.text else {
361 guard let path = node.linkPath else {
362362 // A radio link is its text.
363363 if !node.text.hasPrefix("<"), !node.text.contains(":") { return node.text }
364364 var target = node.text
Sources/OrgCore/Export/ODTExport.swift +1 −1
@@ -1026,7 +1026,7 @@ final class ODTRenderer {
10261026
10271027 /// A bracket link's path, or a plain or angle `file:` link's.
10281028 static func linkPath(_ node: SyntaxNode) -> String? {
1029 if let token = node.tokens.first(where: { $0.kind == .linkPath }) { return token.text }
1029 if let path = node.linkPath { return path }
10301030 var text = node.text
10311031 if text.hasPrefix("<"), text.hasSuffix(">") { text = String(text.dropFirst().dropLast()) }
10321032 return text.hasPrefix("file:") ? text : nil
Sources/OrgCore/Links/Links.swift +2 −2
@@ -136,8 +136,8 @@ public enum Links {
136136 guard let node = found else { return nil }
137137 let whole = node.text
138138 var link: OrgLink
139 if whole.hasPrefix("[["), let path = node.tokens.first(where: { $0.kind == .linkPath }) {
140 link = parse(bracket: path.text, abbreviations: abbreviations(in: tree.settings.setupKeywords + text))
139 if whole.hasPrefix("[["), let path = node.linkPath {
140 link = parse(bracket: path, abbreviations: abbreviations(in: tree.settings.setupKeywords + text))
141141 } else if whole.hasPrefix("<") && whole.hasSuffix(">") {
142142 link = parse(plain: String(whole.dropFirst().dropLast()))
143143 } else if tree.settings.radioTargets.contains(where: { words($0) == words(whole) }) {
Sources/OrgCore/Parser/Inline.swift +1 −1
@@ -304,7 +304,7 @@ struct InlineScanner {
304304 guard i + 1 < limit, chars[i + 1] == "[" else { return nil }
305305 var j = i + 2
306306 while j < limit, chars[j] != "]" {
307 if chars[j] == "[" || isNewline(chars[j]) { return nil }
307 if chars[j] == "[" { return nil }
308308 if chars[j] == "\\", j + 1 < limit { j += 1 }
309309 j += 1
310310 }
Sources/OrgCore/Semantic/DocumentModel.swift +1 −1
@@ -202,7 +202,7 @@ public struct DocumentModel: Sendable {
202202 }
203203
204204 private func linkTarget(_ node: SyntaxNode) -> String {
205 if let path = node.tokens.first(where: { $0.kind == .linkPath }) { return path.text }
205 if let path = node.linkPath { return path }
206206 let text = node.text
207207 if text.hasPrefix("<"), text.hasSuffix(">") { return String(text.dropFirst().dropLast()) }
208208 return text
Sources/OrgCore/Syntax/SyntaxNode.swift +6
@@ -103,6 +103,12 @@ public final class SyntaxNode: Sendable {
103103 public func descendants() -> [SyntaxNode] {
104104 [self] + children.flatMap { $0.descendants() }
105105 }
106
107 /// A bracket link's path as `org-element-link-parser` reads it: each run of blanks
108 /// holding a newline made one space.
109 var linkPath: String? {
110 tokens.first { $0.kind == .linkPath }.map { $0.text.replacing(/[ \t]*\n[ \t]*/, with: " ") }
111 }
106112}
107113
108114public struct SyntaxToken: Sendable, Equatable {
Tests/OrgCoreTests/InlineTests.swift +6
@@ -46,6 +46,12 @@ struct InlineTests {
4646 #expect(link.children.map(\.kind) == [.linkDescription])
4747 }
4848
49 /// `org-element-link-parser`: a path may run onto the next line, read with its line break as a space.
50 @Test func linkPathsSpanLines() {
51 let links = OrgParser.parse("[[elisp:(+ 1\n 2)]] and [[a \n\tb][c]]\n\n[[x\n\ny]]\n").root.descendants().filter { $0.kind == .link }
52 #expect(links.map(\.linkPath) == ["elisp:(+ 1 2)", "a b"])
53 }
54
4955 @Test func noLinksInsideLinkDescriptions() {
5056 let link = OrgParser.parse("[[a][see https://b.c]]\n").root.descendants().first { $0.kind == .link }!
5157 #expect(link.descendants().filter { $0.kind == .link }.count == 1)
Tests/OrgCoreTests/LaTeXExportTests.swift +1
@@ -107,6 +107,7 @@ struct LaTeXExportTests {
107107 "Line one\\\\\nline two \\\\\n\\\\\nafter\n",
108108 "Links: [[https://orgmode.org][Org & co]] <https://a.b/c_d> https://x.y/%20 [[mailto:me@x.org]] [[file:doc.pdf][the doc]] [[./rel/file.txt]] [[file:/abs/path.org]]\n",
109109 "Run [[elisp:(+ 1 2)][add]] and [[elisp:(message \"50% #x\")]], [[shell:ls ~/x_y][list]] [[shell:echo $HOME]], [[help:car][car]] [[shortdoc:string]].\n",
110 "Run [[elisp:(+ 1\n 2)]] and see [[https://a.b/c\n d]] ok, [[shell:a \n\tb][two\n lines]] and [[elisp:a\tb\n c]].\n",
110111 "* Target section\nA <<anchor>> here and [[anchor]] and [[anchor][desc]].\n* Other\nSee [[*Target section]] and [[Target section][named]] and [[#cid]].\n* Third\n:PROPERTIES:\n:CUSTOM_ID: cid\n:END:\nx\n",
111112 "#+OPTIONS: num:nil\n* Head\nSee [[*Head]].\n",
112113 "Footnotes[fn:1] twice[fn:1] and[fn:2][fn:3] and inline[fn::anon /x/] and[fn:named:inline def].\n\n[fn:1] First.\n\n[fn:2] Second\n with two lines.\n\n[fn:3] Third.\n",