Skip to content

Commit

Permalink
support for named links (closes #55)
Browse files Browse the repository at this point in the history
  • Loading branch information
hakimel committed Aug 21, 2012
1 parent 5bd5ac6 commit 141d694
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 20 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ Reveal.addEventListener( 'fragmenthidden', function( event ) {
} );
```

### Internal links

It's easy to link between slides. The first example below targets the index of another slide whereas the second targets a slide with an ID attribute (```<section id="'"some-slide">```):

```html
<a href="#/2/2">Link</a>
<a href="#/some-slide">Link</a>
```

## PDF Export

Presentations can be exported to PDF via a special print stylesheet. This feature requires that you use [Google Chrome](http://google.com/chrome).
Expand Down
67 changes: 53 additions & 14 deletions js/reveal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* reveal.js 2.0 r19
* reveal.js 2.0 r20
* http://lab.hakim.se/reveal-js
* MIT licensed
*
Expand Down Expand Up @@ -834,14 +834,35 @@ var Reveal = (function(){
* Reads the current URL (hash) and navigates accordingly.
*/
function readURL() {
// Break the hash down to separate components
var bits = window.location.hash.slice(2).split('/');

// Read the index components of the hash
var h = parseInt( bits[0] ) || 0 ;
var v = parseInt( bits[1] ) || 0 ;
var hash = window.location.hash;

// Attempt to parse the hash as either an index or name
var bits = hash.slice( 2 ).split( '/' ),
name = hash.replace( /#|\//gi, '' );

// If the first bit is invalid and there is a name we can
// assume that this is a named link
if( isNaN( parseInt( bits[0] ) ) && name.length ) {
// Find the slide with the specified name
var slide = document.querySelector( '#' + name );

if( slide ) {
// Find the position of the named slide and navigate to it
var indices = Reveal.getIndices( slide );
navigateTo( indices.h, indices.v );
}
// If the slide doesn't exist, navigate to the current slide
else {
navigateTo( indexh, indexv );
}
}
else {
// Read the index components of the hash
var h = parseInt( bits[0] ) || 0,
v = parseInt( bits[1] ) || 0;

navigateTo( h, v );
navigateTo( h, v );
}
}

/**
Expand Down Expand Up @@ -1049,12 +1070,30 @@ var Reveal = (function(){
addEventListeners: addEventListeners,
removeEventListeners: removeEventListeners,

// Returns the indices of the current slide
getIndices: function() {
return {
h: indexh,
v: indexv
};
// Returns the indices of the current, or specified, slide
getIndices: function( slide ) {
// By default, return the current indices
var h = indexh,
v = indexv;

// If a slide is specified, return the indices of that slide
if( slide ) {
var isVertical = !!slide.parentNode.nodeName.match( /section/gi );
var slideh = isVertical ? slide.parentNode : slide;

// Select all horizontal slides
var horizontalSlides = Array.prototype.slice.call( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );

// Now that we know which the horizontal slide is, get its index
h = Math.max( horizontalSlides.indexOf( slideh ), 0 );

// If this is a vertical slide, grab the vertical index
if( isVertical ) {
v = Math.max( Array.prototype.slice.call( slide.parentNode.children ).indexOf( slide ), 0 );
}
}

return { h: h, v: v };
},

// Returns the previous slide element, may be null
Expand Down
14 changes: 8 additions & 6 deletions js/reveal.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 141d694

Please sign in to comment.