From 0338f280d3942094782da57ea3b5b72bdb833e9d Mon Sep 17 00:00:00 2001
From: Hakim El Hattab <hakim.elhattab@gmail.com>
Date: Thu, 10 Sep 2015 08:28:52 +0200
Subject: [PATCH] add getSlideNotes API method and tests

---
 README.md      |  3 +++
 js/reveal.js   | 46 ++++++++++++++++++++++++++++++----------------
 test/test.html |  3 ++-
 test/test.js   | 10 +++++++++-
 4 files changed, 44 insertions(+), 18 deletions(-)

diff --git a/README.md b/README.md
index 8d25fa7..9011605 100644
--- a/README.md
+++ b/README.md
@@ -372,6 +372,9 @@ Reveal.getIndices(); // { h: 0, v: 0 } }
 Reveal.getProgress(); // 0-1
 Reveal.getTotalSlides();
 
+// Returns the speaker notes for the current slide
+Reveal.getSlideNotes();
+
 // State checks
 Reveal.isFirstSlide();
 Reveal.isLastSlide();
diff --git a/js/reveal.js b/js/reveal.js
index 83c180b..2b8fec7 100644
--- a/js/reveal.js
+++ b/js/reveal.js
@@ -2475,22 +2475,7 @@
 
 		if( config.showNotes && dom.speakerNotes && currentSlide && !isPrintingPDF() ) {
 
-			var notes = '';
-
-			// Notes can be specified via the data-notes attribute...
-			if( currentSlide.hasAttribute( 'data-notes' ) ) {
-				notes = currentSlide.getAttribute( 'data-notes' );
-			}
-
-			// ... or using an <aside class="notes"> element
-			if( !notes ) {
-				var notesElement = currentSlide.querySelector( 'aside.notes' );
-				if( notesElement ) {
-					notes = notesElement.innerHTML;
-				}
-			}
-
-			dom.speakerNotes.innerHTML = notes;
+			dom.speakerNotes.innerHTML = getSlideNotes() || '';
 
 		}
 
@@ -3335,6 +3320,32 @@
 
 	}
 
+	/**
+	 * Retrieves the speaker notes from a slide. Notes can be
+	 * defined in two ways:
+	 * 1. As a data-notes attribute on the slide <section>
+	 * 2. As an <aside class="notes"> inside of the slide
+	 */
+	function getSlideNotes( slide ) {
+
+		// Default to the current slide
+		slide = slide || currentSlide;
+
+		// Notes can be specified via the data-notes attribute...
+		if( slide.hasAttribute( 'data-notes' ) ) {
+			return slide.getAttribute( 'data-notes' );
+		}
+
+		// ... or using an <aside class="notes"> element
+		var notesElement = slide.querySelector( 'aside.notes' );
+		if( notesElement ) {
+			return notesElement.innerHTML;
+		}
+
+		return null;
+
+	}
+
 	/**
 	 * Retrieves the current state of the presentation as
 	 * an object. This state can then be restored at any
@@ -4486,6 +4497,9 @@
 		// Returns the slide background element at the specified index
 		getSlideBackground: getSlideBackground,
 
+		// Returns the speaker notes string for a slide, or null
+		getSlideNotes: getSlideNotes,
+
 		// Returns the previous slide element, may be null
 		getPreviousSlide: function() {
 			return previousSlide;
diff --git a/test/test.html b/test/test.html
index 34cf832..d08e4f0 100644
--- a/test/test.html
+++ b/test/test.html
@@ -24,10 +24,11 @@
 					<img data-src="fake-url.png">
 					<video data-src="fake-url.mp4"></video>
 					<audio data-src="fake-url.mp3"></audio>
+					<aside class="notes">speaker notes 1</aside>
 				</section>
 
 				<section>
-					<section data-background="examples/assets/image2.png">
+					<section data-background="examples/assets/image2.png" data-notes="speaker notes 2">
 						<h1>2.1</h1>
 					</section>
 					<section>
diff --git a/test/test.js b/test/test.js
index 79ff81e..a96b70b 100644
--- a/test/test.js
+++ b/test/test.js
@@ -89,7 +89,7 @@ Reveal.addEventListener( 'ready', function() {
 
 	test( 'Reveal.isLastSlide after vertical slide', function() {
 		var lastSlideIndex = document.querySelectorAll( '.reveal .slides>section' ).length - 1;
-		
+
 		Reveal.slide( 1, 1 );
 		Reveal.slide( lastSlideIndex );
 		strictEqual( Reveal.isLastSlide(), true, 'true after Reveal.slide( 1, 1 ) and then Reveal.slide( '+ lastSlideIndex +', 0 )' );
@@ -139,6 +139,14 @@ Reveal.addEventListener( 'ready', function() {
 		strictEqual( Reveal.getSlideBackground( 1, 100 ), undefined, 'undefined when out of vertical bounds' );
 	});
 
+	test( 'Reveal.getSlideNotes', function() {
+		Reveal.slide( 0, 0 );
+		ok( Reveal.getSlideNotes() === 'speaker notes 1', 'works with <aside class="notes">' );
+
+		Reveal.slide( 1, 0 );
+		ok( Reveal.getSlideNotes() === 'speaker notes 2', 'works with <section data-notes="">' );
+	});
+
 	test( 'Reveal.getPreviousSlide/getCurrentSlide', function() {
 		Reveal.slide( 0, 0 );
 		Reveal.slide( 1, 0 );
-- 
GitLab