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 {
1244 if node.firstChild(.linkDescription)?.descendants().contains(where: { $0.kind == .link }) == true { 1244 if node.firstChild(.linkDescription)?.descendants().contains(where: { $0.kind == .link }) == true {
1245 throw Unsupported(reason: "Links in link descriptions") 1245 throw Unsupported(reason: "Links in link descriptions")
1246 } 1246 }
1247 let pathToken = node.tokens.first { $0.kind == .linkPath }?.text 1247 let pathToken = node.linkPath
1248 var raw = pathToken ?? node.text 1248 var raw = pathToken ?? node.text
1249 if pathToken == nil, raw.hasPrefix("<"), raw.hasSuffix(">") { raw = String(raw.dropFirst().dropLast()) } 1249 if pathToken == nil, raw.hasPrefix("<"), raw.hasSuffix(">") { raw = String(raw.dropFirst().dropLast()) }
1250 var description: String? = node.firstChild(.linkDescription) == nil ? nil : try transcode(children) 1250 var description: String? = node.firstChild(.linkDescription) == nil ? nil : try transcode(children)
Sources/OrgCore/Export/ASCIIText.swift +1 −1
@@ -461,7 +461,7 @@ struct ASCIIText {
461 461
462 func link(_ node: SyntaxNode) -> String { 462 func link(_ node: SyntaxNode) -> String {
463 let description = node.firstChild(.linkDescription).map { inline($0) } 463 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 {
465 var target = node.text 465 var target = node.text
466 if target.hasPrefix("<"), target.hasSuffix(">") { target = String(target.dropFirst().dropLast()) } 466 if target.hasPrefix("<"), target.hasSuffix(">") { target = String(target.dropFirst().dropLast()) }
467 // A radio link is its text. 467 // A radio link is its text.
Sources/OrgCore/Export/ExportOptions.swift +1 −1
@@ -205,7 +205,7 @@ public enum ExportPreprocess {
205 let rebased = rebasingLinks(description.text, from: directory, to: includer, plain: true) 205 let rebased = rebasingLinks(description.text, from: directory, to: includer, plain: true)
206 if rebased != description.text { edits.append((description.range, rebased)) } 206 if rebased != description.text { edits.append((description.range, rebased)) }
207 } 207 }
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) {
209 edits.append((path.range, rebased)) 209 edits.append((path.range, rebased))
210 } 210 }
211 } else if whole.hasPrefix("<"), whole.hasSuffix(">") { 211 } else if whole.hasPrefix("<"), whole.hasSuffix(">") {
Sources/OrgCore/Export/HTMLExport.swift +1 −1
@@ -856,7 +856,7 @@ final class HTMLRenderer {
856 static let imageExtensions: Set<String> = ["png", "jpg", "jpeg", "gif", "svg", "webp", "bmp", "tif", "tiff", "avif"] 856 static let imageExtensions: Set<String> = ["png", "jpg", "jpeg", "gif", "svg", "webp", "bmp", "tif", "tiff", "avif"]
857 857
858 private func link(_ node: SyntaxNode) -> String { 858 private func link(_ node: SyntaxNode) -> String {
859 let path = node.tokens.first { $0.kind == .linkPath }?.text 859 let path = node.linkPath
860 let description = node.firstChild(.linkDescription) 860 let description = node.firstChild(.linkDescription)
861 guard let path else { 861 guard let path else {
862 // A radio link goes to its target. 862 // A radio link goes to its target.
Sources/OrgCore/Export/LaTeXExport.swift +1 −1
@@ -1525,7 +1525,7 @@ final class LaTeXRenderer {
1525 // MARK: Links 1525 // MARK: Links
1526 1526
1527 private func linkTarget(_ node: SyntaxNode) -> (type: String, path: String) { 1527 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
1529 if node.tokens.first(where: { $0.kind == .linkPath }) == nil, raw.hasPrefix("<"), raw.hasSuffix(">") { 1529 if node.tokens.first(where: { $0.kind == .linkPath }) == nil, raw.hasPrefix("<"), raw.hasSuffix(">") {
1530 raw = String(raw.dropFirst().dropLast()) 1530 raw = String(raw.dropFirst().dropLast())
1531 } 1531 }
Sources/OrgCore/Export/MarkdownExport.swift +1 −1
@@ -358,7 +358,7 @@ final class MarkdownRenderer {
358 } 358 }
359 359
360 private func link(_ node: SyntaxNode) -> String { 360 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 {
362 // A radio link is its text. 362 // A radio link is its text.
363 if !node.text.hasPrefix("<"), !node.text.contains(":") { return node.text } 363 if !node.text.hasPrefix("<"), !node.text.contains(":") { return node.text }
364 var target = node.text 364 var target = node.text
Sources/OrgCore/Export/ODTExport.swift +1 −1
@@ -1026,7 +1026,7 @@ final class ODTRenderer {
1026 1026
1027 /// A bracket link's path, or a plain or angle `file:` link's. 1027 /// A bracket link's path, or a plain or angle `file:` link's.
1028 static func linkPath(_ node: SyntaxNode) -> String? { 1028 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 }
1030 var text = node.text 1030 var text = node.text
1031 if text.hasPrefix("<"), text.hasSuffix(">") { text = String(text.dropFirst().dropLast()) } 1031 if text.hasPrefix("<"), text.hasSuffix(">") { text = String(text.dropFirst().dropLast()) }
1032 return text.hasPrefix("file:") ? text : nil 1032 return text.hasPrefix("file:") ? text : nil
Sources/OrgCore/Links/Links.swift +2 −2
@@ -136,8 +136,8 @@ public enum Links {
136 guard let node = found else { return nil } 136 guard let node = found else { return nil }
137 let whole = node.text 137 let whole = node.text
138 var link: OrgLink 138 var link: OrgLink
139 if whole.hasPrefix("[["), let path = node.tokens.first(where: { $0.kind == .linkPath }) { 139 if whole.hasPrefix("[["), let path = node.linkPath {
140 link = parse(bracket: path.text, abbreviations: abbreviations(in: tree.settings.setupKeywords + text)) 140 link = parse(bracket: path, abbreviations: abbreviations(in: tree.settings.setupKeywords + text))
141 } else if whole.hasPrefix("<") && whole.hasSuffix(">") { 141 } else if whole.hasPrefix("<") && whole.hasSuffix(">") {
142 link = parse(plain: String(whole.dropFirst().dropLast())) 142 link = parse(plain: String(whole.dropFirst().dropLast()))
143 } else if tree.settings.radioTargets.contains(where: { words($0) == words(whole) }) { 143 } else if tree.settings.radioTargets.contains(where: { words($0) == words(whole) }) {
Sources/OrgCore/Parser/Inline.swift +1 −1
@@ -304,7 +304,7 @@ struct InlineScanner {
304 guard i + 1 < limit, chars[i + 1] == "[" else { return nil } 304 guard i + 1 < limit, chars[i + 1] == "[" else { return nil }
305 var j = i + 2 305 var j = i + 2
306 while j < limit, chars[j] != "]" { 306 while j < limit, chars[j] != "]" {
307 if chars[j] == "[" || isNewline(chars[j]) { return nil } 307 if chars[j] == "[" { return nil }
308 if chars[j] == "\\", j + 1 < limit { j += 1 } 308 if chars[j] == "\\", j + 1 < limit { j += 1 }
309 j += 1 309 j += 1
310 } 310 }
Sources/OrgCore/Semantic/DocumentModel.swift +1 −1
@@ -202,7 +202,7 @@ public struct DocumentModel: Sendable {
202 } 202 }
203 203
204 private func linkTarget(_ node: SyntaxNode) -> String { 204 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 }
206 let text = node.text 206 let text = node.text
207 if text.hasPrefix("<"), text.hasSuffix(">") { return String(text.dropFirst().dropLast()) } 207 if text.hasPrefix("<"), text.hasSuffix(">") { return String(text.dropFirst().dropLast()) }
208 return text 208 return text
Sources/OrgCore/Syntax/SyntaxNode.swift +6
@@ -103,6 +103,12 @@ public final class SyntaxNode: Sendable {
103 public func descendants() -> [SyntaxNode] { 103 public func descendants() -> [SyntaxNode] {
104 [self] + children.flatMap { $0.descendants() } 104 [self] + children.flatMap { $0.descendants() }
105 } 105 }
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 }
106} 112}
107 113
108public struct SyntaxToken: Sendable, Equatable { 114public struct SyntaxToken: Sendable, Equatable {
Tests/OrgCoreTests/InlineTests.swift +6
@@ -46,6 +46,12 @@ struct InlineTests {
46 #expect(link.children.map(\.kind) == [.linkDescription]) 46 #expect(link.children.map(\.kind) == [.linkDescription])
47 } 47 }
48 48
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
49 @Test func noLinksInsideLinkDescriptions() { 55 @Test func noLinksInsideLinkDescriptions() {
50 let link = OrgParser.parse("[[a][see https://b.c]]\n").root.descendants().first { $0.kind == .link }! 56 let link = OrgParser.parse("[[a][see https://b.c]]\n").root.descendants().first { $0.kind == .link }!
51 #expect(link.descendants().filter { $0.kind == .link }.count == 1) 57 #expect(link.descendants().filter { $0.kind == .link }.count == 1)
Tests/OrgCoreTests/LaTeXExportTests.swift +1
@@ -107,6 +107,7 @@ struct LaTeXExportTests {
107 "Line one\\\\\nline two \\\\\n\\\\\nafter\n", 107 "Line one\\\\\nline two \\\\\n\\\\\nafter\n",
108 "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", 108 "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",
109 "Run [[elisp:(+ 1 2)][add]] and [[elisp:(message \"50% #x\")]], [[shell:ls ~/x_y][list]] [[shell:echo $HOME]], [[help:car][car]] [[shortdoc:string]].\n", 109 "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",
110 "* 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", 111 "* 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",
111 "#+OPTIONS: num:nil\n* Head\nSee [[*Head]].\n", 112 "#+OPTIONS: num:nil\n* Head\nSee [[*Head]].\n",
112 "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", 113 "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",