Tests/OrgCoreTests/RevealExportTests.swift
239 lines · 10614 bytes
1import Foundation
2import Testing
3@testable import OrgCore
4
5/// reveal.js export against the structure org-re-reveal writes; Emacs's org-re-reveal isn't
6/// installed for an oracle test.
7struct RevealExportTests {
8 static let library = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
9 .appendingPathComponent("Sources/OrgMathJax/Reveal")
10
11 static let deck = """
12 #+TITLE: Talk
13 #+AUTHOR: Ann
14 #+DATE: 2026-11-26
15 #+OPTIONS: toc:nil num:nil
16
17 * First
18 Intro text.
19 #+BEGIN_NOTES
20 Say hello.
21 #+END_NOTES
22 ** Down
23 #+ATTR_REVEAL: :frag (appear)
24 - one
25 - two
26 * Second
27 #+ATTR_REVEAL: :frag fade-in :frag_idx 2
28 A fragment.
29 *** Deep
30 Still on Second's group.
31
32 """
33
34 /// The slides alone, between the slides div and its end, without the title slide.
35 static func slides(_ html: String) -> String {
36 let start = html.range(of: "<div class=\"slides\">\n")!.upperBound
37 let end = html.range(of: "</div>\n</div>\n", options: .backwards)!.lowerBound
38 return String(html[start..<end])
39 }
40
41 @Test func sectionsFollowHeadlineLevels() {
42 let html = RevealExport.document(Self.deck, fallbackTitle: "talk", library: nil)
43 #expect(Self.slides(html) == """
44 <section id="sec-title-slide">
45 <h1 class="title">Talk</h1><h2 class="author">Ann</h2><h2 class="date">2026-11-26</h2>
46 </section>
47 <section>
48 <section id="slide-first">
49 <h2 id="first">First</h2>
50 <p>Intro text.</p>
51 <aside class="notes">
52 <p>Say hello.</p>
53 </aside>
54 </section>
55 <section id="slide-down">
56 <h3 id="down">Down</h3>
57 <ul>
58 <li class="fragment appear">one</li>
59 <li class="fragment appear">two</li>
60 </ul>
61 </section>
62 </section>
63 <section>
64 <section id="slide-second">
65 <h2 id="second">Second</h2>
66 <p class="fragment fade-in" data-fragment-index="2">A fragment.</p>
67 </section>
68 <section id="slide-deep">
69 <h4 id="deep">Deep</h4>
70 <p>Still on Second's group.</p>
71 </section>
72 </section>
73
74 """)
75 }
76
77 @Test func inlinesRevealWithoutRoot() throws {
78 let html = RevealExport.document(Self.deck, fallbackTitle: "talk", library: Self.library)
79 let theme = try String(contentsOf: Self.library.appendingPathComponent("theme/black.css"), encoding: .utf8)
80 let script = try String(contentsOf: Self.library.appendingPathComponent("reveal.js"), encoding: .utf8)
81 #expect(html.contains("<style id=\"theme\">\n\(theme)\n</style>"))
82 #expect(html.contains("<script>\n\(script)\n</script>"))
83 #expect(!html.contains("<script src="))
84 #expect(!html.contains("<link rel=\"stylesheet\""))
85 #expect(html.contains("plugins: [ RevealNotes, RevealSearch, RevealZoom ]"))
86 #expect(html.contains("e.RevealNotes="))
87 }
88
89 @Test func linksToRevealRoot() {
90 let text = """
91 #+REVEAL_ROOT: https://cdn.example/reveal.js
92 #+REVEAL_THEME: moon
93 #+REVEAL_TRANS: fade
94 #+REVEAL_PLUGINS: (notes highlight)
95 #+REVEAL_INIT_OPTIONS: width:1200, height:800,
96 #+REVEAL_INIT_OPTIONS: slideNumber:false
97 #+OPTIONS: toc:nil reveal_control:nil
98 * A
99
100 """
101 let html = RevealExport.document(text, fallbackTitle: "deck", library: Self.library)
102 #expect(html.contains("<link rel=\"stylesheet\" href=\"https://cdn.example/reveal.js/dist/reveal.css\"/>"))
103 #expect(html.contains("<link rel=\"stylesheet\" href=\"https://cdn.example/reveal.js/dist/theme/moon.css\" id=\"theme\"/>"))
104 #expect(html.contains("<script src=\"https://cdn.example/reveal.js/dist/reveal.js\"></script>\n<script src=\"https://cdn.example/reveal.js/dist/plugin/notes.js\"></script>\n<script src=\"https://cdn.example/reveal.js/dist/plugin/highlight.js\"></script>"))
105 #expect(html.contains("controls: false,"))
106 #expect(html.contains("transition: 'fade',\ntransitionSpeed: 'default',\nwidth:1200, height:800,\nslideNumber:false,\nplugins: [ RevealNotes, RevealHighlight ]"))
107 #expect(!html.contains("<style id=\"theme\">"))
108 }
109
110 @Test func inlineDeckLeavesOutPluginsNotBundled() {
111 let html = RevealExport.document("#+REVEAL_PLUGINS: (markdown zoom highlight)\n* A\n", fallbackTitle: "deck", library: Self.library)
112 #expect(html.contains("plugins: [ RevealZoom ]"))
113 #expect(!html.contains("RevealMarkdown"))
114 }
115
116 @Test func tableOfContentsAndFootnotesAreSlides() {
117 let text = """
118 #+OPTIONS: num:nil reveal_title_slide:nil
119 * One
120 See [[*Two][two]] and a note.[fn:1]
121 * Two
122
123 [fn:1] The note.
124
125 """
126 let slides = Self.slides(RevealExport.document(text, fallbackTitle: "deck", library: nil))
127 #expect(slides.hasPrefix("<section id=\"table-of-contents-section\">\n<div id=\"table-of-contents\" role=\"doc-toc\">"))
128 #expect(slides.contains("<li><a href=\"#/slide-one\">One</a>"))
129 #expect(slides.contains("<a href=\"#/slide-two\">two</a>"))
130 #expect(slides.hasSuffix("</section>\n<section>\n<section class=\"footnotes\" aria-label=\"Footnotes\">\n<hr>\n<ol>\n<li id=\"fn-1\"><p>The note.</p>\n <a class=\"footnote-back\" href=\"#fnr-1\" aria-label=\"Back to reference 1\">↩</a></li>\n</ol>\n</section>\n</section>\n"))
131 #expect(!slides.contains("sec-title-slide"))
132 }
133
134 @Test func hlevelTwoMakesSecondLevelHorizontal() {
135 let text = "#+REVEAL_HLEVEL: 2\n#+OPTIONS: toc:nil num:nil reveal_title_slide:nil\n* A\n** B\n*** C\n"
136 #expect(Self.slides(RevealExport.document(text, fallbackTitle: "deck", library: nil)) == """
137 <section>
138 <section id="slide-a">
139 <h2 id="a">A</h2>
140 </section>
141 </section>
142 <section>
143 <section id="slide-b">
144 <h3 id="b">B</h3>
145 </section>
146 <section id="slide-c">
147 <h4 id="c">C</h4>
148 </section>
149 </section>
150
151 """)
152 }
153
154 @Test func headingsBelowHAreNotSlides() {
155 let text = "#+OPTIONS: toc:nil num:nil reveal_title_slide:nil H:1\n* A\n** B\nText.\n* C :noexport:\n"
156 #expect(Self.slides(RevealExport.document(text, fallbackTitle: "deck", library: nil)) == """
157 <section>
158 <section id="slide-a">
159 <h2 id="a">A</h2>
160 <h3 id="b">B</h3>
161 <p>Text.</p>
162 </section>
163 </section>
164
165 """)
166 }
167
168 @Test func textBeforeTheFirstHeadingIsASlide() {
169 let text = "#+OPTIONS: toc:nil num:nil reveal_title_slide:nil\nOpening words.\n* A\n"
170 #expect(Self.slides(RevealExport.document(text, fallbackTitle: "deck", library: nil)).hasPrefix("<section>\n<p>Opening words.</p>\n</section>\n<section>\n<section id=\"slide-a\">"))
171 }
172
173 @Test func fragmentsSplitsAndRawHTML() {
174 let text = """
175 #+REVEAL_DEFAULT_FRAG_STYLE: grow
176 #+OPTIONS: toc:nil num:nil reveal_title_slide:nil
177 * A
178 :PROPERTIES:
179 :REVEAL_BACKGROUND: #123456
180 :REVEAL_DATA_TRANSITION: zoom
181 :END:
182 #+ATTR_REVEAL: :frag t
183 First.
184 #+REVEAL: split
185 #+ATTR_REVEAL: :frag (none fade-up) :frag_idx (- 3)
186 1. one
187 2. two
188 3. three
189 #+REVEAL_HTML: <div class="raw">raw</div>
190 @@reveal:<b>snippet</b>@@
191
192 """
193 #expect(Self.slides(RevealExport.document(text, fallbackTitle: "deck", library: nil)) == """
194 <section>
195 <section id="slide-a" data-transition="zoom" data-background="#123456">
196 <h2 id="a">A</h2>
197 <p class="fragment grow">First.</p>
198 </section>
199 <section data-transition="zoom" data-background="#123456">
200 <ol>
201 <li>one</li>
202 <li class="fragment fade-up" data-fragment-index="3">two</li>
203 <li class="fragment fade-up" data-fragment-index="3">three</li>
204 </ol>
205 <div class="raw">raw</div>
206 <p><b>snippet</b></p>
207 </section>
208 </section>
209
210 """)
211 }
212
213 @Test func titleSlideTemplate() {
214 let text = "#+TITLE: T\n#+SUBTITLE: S\n#+AUTHOR: A\n#+REVEAL_TITLE_SLIDE: <h1>%t</h1><p>%s by %a, 100%%</p>\n#+REVEAL_TITLE_SLIDE_BACKGROUND: bg.png\n"
215 let html = RevealExport.document(text, fallbackTitle: "deck", library: nil)
216 #expect(html.contains("<section id=\"sec-title-slide\" data-background=\"bg.png\">\n<h1>T</h1><p>S by A, 100%</p>\n</section>"))
217 }
218
219 @Test func inlineDeckEmbedsLocalImages() throws {
220 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("reveal-\(UUID().uuidString)")
221 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
222 defer { try? FileManager.default.removeItem(at: folder) }
223 try Data([0x89, 0x50, 0x4E, 0x47]).write(to: folder.appendingPathComponent("a.png"))
224 let path = folder.appendingPathComponent("deck.org").path
225 let text = "* A\n :PROPERTIES:\n :REVEAL_BACKGROUND: ./a.png\n :END:\n[[file:a.png]]\n[[file:missing.png]]\n"
226 let html = RevealExport.document(text, fallbackTitle: "deck", path: path, library: Self.library)
227 #expect(html.contains("data-background=\"data:image/png;base64,iVBORw==\""))
228 #expect(html.contains("<img src=\"data:image/png;base64,iVBORw==\" alt=\"a.png\">"))
229 #expect(html.contains("<img src=\"missing.png\""))
230 let linked = RevealExport.document("#+REVEAL_ROOT: ./r\n" + text, fallbackTitle: "deck", path: path, library: Self.library)
231 #expect(linked.contains("<img src=\"a.png\""))
232 }
233
234 @Test func codeColorsFollowTheThemesBackground() {
235 let css = ".org-keyword { color: #111111; }\n@media (prefers-color-scheme: dark) { .org-keyword { color: #eeeeee; } }\n"
236 #expect(RevealExport.codeCSS(css, dark: false) == ".org-keyword { color: #111111; }")
237 #expect(RevealExport.codeCSS(css, dark: true) == ".org-keyword { color: #eeeeee; }")
238 }
239}