2020-04-15 10:23:51 +02:00
|
|
|
import hljs from './highlight-core.js'
|
|
|
|
import './highlight-line-numbers.js'
|
2019-03-04 14:32:38 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
/*!
|
|
|
|
* reveal.js plugin that adds syntax highlight support.
|
2019-03-12 13:17:50 +01:00
|
|
|
*/
|
2016-11-22 23:42:21 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Function to perform a better "data-trim" on code snippets
|
|
|
|
// Will slice an indentation amount on each line of the snippet (amount based on the line having the lowest indentation length)
|
|
|
|
function betterTrim(snippetEl) {
|
|
|
|
// Helper functions
|
|
|
|
function trimLeft(val) {
|
|
|
|
// Adapted from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim#Polyfill
|
|
|
|
return val.replace(/^[\s\uFEFF\xA0]+/g, '');
|
|
|
|
}
|
|
|
|
function trimLineBreaks(input) {
|
|
|
|
var lines = input.split('\n');
|
|
|
|
|
|
|
|
// Trim line-breaks from the beginning
|
|
|
|
for (var i = 0; i < lines.length; i++) {
|
|
|
|
if (lines[i].trim() === '') {
|
|
|
|
lines.splice(i--, 1);
|
|
|
|
} else break;
|
|
|
|
}
|
2016-11-22 23:42:21 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Trim line-breaks from the end
|
|
|
|
for (var i = lines.length-1; i >= 0; i--) {
|
|
|
|
if (lines[i].trim() === '') {
|
|
|
|
lines.splice(i, 1);
|
|
|
|
} else break;
|
2016-11-22 23:42:21 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
return lines.join('\n');
|
2016-11-22 23:42:21 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Main function for betterTrim()
|
|
|
|
return (function(snippetEl) {
|
|
|
|
var content = trimLineBreaks(snippetEl.innerHTML);
|
|
|
|
var lines = content.split('\n');
|
|
|
|
// Calculate the minimum amount to remove on each line start of the snippet (can be 0)
|
|
|
|
var pad = lines.reduce(function(acc, line) {
|
|
|
|
if (line.length > 0 && trimLeft(line).length > 0 && acc > line.length - trimLeft(line).length) {
|
|
|
|
return line.length - trimLeft(line).length;
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, Number.POSITIVE_INFINITY);
|
|
|
|
// Slice each line with this amount
|
|
|
|
return lines.map(function(line, index) {
|
|
|
|
return line.slice(pad);
|
|
|
|
})
|
|
|
|
.join('\n');
|
|
|
|
})(snippetEl);
|
|
|
|
}
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
var RevealHighlight = {
|
2020-04-08 13:05:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
id: 'highlight',
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
HIGHLIGHT_STEP_DELIMITER: '|',
|
|
|
|
HIGHLIGHT_LINE_DELIMITER: ',',
|
|
|
|
HIGHLIGHT_LINE_RANGE_DELIMITER: '-',
|
2013-04-28 20:12:31 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
init: function( deck ) {
|
2019-03-12 13:17:50 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Read the plugin config options and provide fallbacks
|
|
|
|
var config = deck.getConfig().highlight || {};
|
|
|
|
config.highlightOnLoad = typeof config.highlightOnLoad === 'boolean' ? config.highlightOnLoad : true;
|
|
|
|
config.escapeHTML = typeof config.escapeHTML === 'boolean' ? config.escapeHTML : true;
|
2019-03-04 14:32:38 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
[].slice.call( document.querySelectorAll( '.reveal pre code' ) ).forEach( function( block ) {
|
2013-04-28 20:12:31 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Trim whitespace if the "data-trim" attribute is present
|
|
|
|
if( block.hasAttribute( 'data-trim' ) && typeof block.innerHTML.trim === 'function' ) {
|
|
|
|
block.innerHTML = betterTrim( block );
|
|
|
|
}
|
2019-03-04 14:32:38 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Escape HTML tags unless the "data-noescape" attrbute is present
|
|
|
|
if( config.escapeHTML && !block.hasAttribute( 'data-noescape' )) {
|
|
|
|
block.innerHTML = block.innerHTML.replace( /</g,"<").replace(/>/g, '>' );
|
|
|
|
}
|
2013-05-01 21:44:53 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Re-highlight when focus is lost (for contenteditable code)
|
|
|
|
block.addEventListener( 'focusout', function( event ) {
|
|
|
|
hljs.highlightBlock( event.currentTarget );
|
|
|
|
}, false );
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
if( config.highlightOnLoad ) {
|
|
|
|
RevealHighlight.highlightBlock( block );
|
|
|
|
}
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
} );
|
|
|
|
|
|
|
|
// If we're printing to PDF, scroll the code highlights of
|
|
|
|
// all blocks in the deck into view at once
|
|
|
|
deck.on( 'pdf-ready', function() {
|
|
|
|
[].slice.call( document.querySelectorAll( '.reveal pre code[data-line-numbers].current-fragment' ) ).forEach( function( block ) {
|
|
|
|
RevealHighlight.scrollHighlightedLineIntoView( block, {}, true );
|
2019-03-08 15:07:59 +01:00
|
|
|
} );
|
2020-04-15 10:23:51 +02:00
|
|
|
} );
|
2019-03-04 14:32:38 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
},
|
2019-03-08 15:07:59 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
/**
|
|
|
|
* Highlights a code block. If the <code> node has the
|
|
|
|
* 'data-line-numbers' attribute we also generate slide
|
|
|
|
* numbers.
|
|
|
|
*
|
|
|
|
* If the block contains multiple line highlight steps,
|
|
|
|
* we clone the block and create a fragment for each step.
|
|
|
|
*/
|
|
|
|
highlightBlock: function( block ) {
|
2019-03-08 15:07:59 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
hljs.highlightBlock( block );
|
2019-03-08 15:07:59 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Don't generate line numbers for empty code blocks
|
|
|
|
if( block.innerHTML.trim().length === 0 ) return;
|
2019-05-27 07:30:08 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
if( block.hasAttribute( 'data-line-numbers' ) ) {
|
|
|
|
hljs.lineNumbersBlock( block, { singleLine: true } );
|
2019-03-08 15:07:59 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
var scrollState = { currentBlock: block };
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// If there is at least one highlight step, generate
|
|
|
|
// fragments
|
|
|
|
var highlightSteps = RevealHighlight.deserializeHighlightSteps( block.getAttribute( 'data-line-numbers' ) );
|
|
|
|
if( highlightSteps.length > 1 ) {
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// If the original code block has a fragment-index,
|
|
|
|
// each clone should follow in an incremental sequence
|
|
|
|
var fragmentIndex = parseInt( block.getAttribute( 'data-fragment-index' ), 10 );
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
if( typeof fragmentIndex !== 'number' || isNaN( fragmentIndex ) ) {
|
|
|
|
fragmentIndex = null;
|
|
|
|
}
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Generate fragments for all steps except the original block
|
|
|
|
highlightSteps.slice(1).forEach( function( highlight ) {
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
var fragmentBlock = block.cloneNode( true );
|
|
|
|
fragmentBlock.setAttribute( 'data-line-numbers', RevealHighlight.serializeHighlightSteps( [ highlight ] ) );
|
|
|
|
fragmentBlock.classList.add( 'fragment' );
|
|
|
|
block.parentNode.appendChild( fragmentBlock );
|
|
|
|
RevealHighlight.highlightLines( fragmentBlock );
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
if( typeof fragmentIndex === 'number' ) {
|
|
|
|
fragmentBlock.setAttribute( 'data-fragment-index', fragmentIndex );
|
|
|
|
fragmentIndex += 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fragmentBlock.removeAttribute( 'data-fragment-index' );
|
|
|
|
}
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Scroll highlights into view as we step through them
|
|
|
|
fragmentBlock.addEventListener( 'visible', RevealHighlight.scrollHighlightedLineIntoView.bind( RevealHighlight, fragmentBlock, scrollState ) );
|
|
|
|
fragmentBlock.addEventListener( 'hidden', RevealHighlight.scrollHighlightedLineIntoView.bind( RevealHighlight, fragmentBlock.previousSibling, scrollState ) );
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
} );
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
block.removeAttribute( 'data-fragment-index' )
|
|
|
|
block.setAttribute( 'data-line-numbers', RevealHighlight.serializeHighlightSteps( [ highlightSteps[0] ] ) );
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
}
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Scroll the first highlight into view when the slide
|
|
|
|
// becomes visible. Note supported in IE11 since it lacks
|
|
|
|
// support for Element.closest.
|
|
|
|
var slide = typeof block.closest === 'function' ? block.closest( 'section:not(.stack)' ) : null;
|
|
|
|
if( slide ) {
|
|
|
|
var scrollFirstHighlightIntoView = function() {
|
|
|
|
RevealHighlight.scrollHighlightedLineIntoView( block, scrollState, true );
|
|
|
|
slide.removeEventListener( 'visible', scrollFirstHighlightIntoView );
|
2020-03-12 17:08:20 +01:00
|
|
|
}
|
2020-04-15 10:23:51 +02:00
|
|
|
slide.addEventListener( 'visible', scrollFirstHighlightIntoView );
|
|
|
|
}
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
RevealHighlight.highlightLines( block );
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
}
|
2019-03-08 15:07:59 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
},
|
2019-03-08 15:07:59 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
/**
|
|
|
|
* Animates scrolling to the first highlighted line
|
|
|
|
* in the given code block.
|
|
|
|
*/
|
|
|
|
scrollHighlightedLineIntoView: function( block, scrollState, skipAnimation ) {
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
cancelAnimationFrame( scrollState.animationFrameID );
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Match the scroll position of the currently visible
|
|
|
|
// code block
|
|
|
|
if( scrollState.currentBlock ) {
|
|
|
|
block.scrollTop = scrollState.currentBlock.scrollTop;
|
|
|
|
}
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Remember the current code block so that we can match
|
|
|
|
// its scroll position when showing/hiding fragments
|
|
|
|
scrollState.currentBlock = block;
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
var highlightBounds = this.getHighlightedLineBounds( block )
|
|
|
|
var viewportHeight = block.offsetHeight;
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Subtract padding from the viewport height
|
|
|
|
var blockStyles = getComputedStyle( block );
|
|
|
|
viewportHeight -= parseInt( blockStyles.paddingTop ) + parseInt( blockStyles.paddingBottom );
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Scroll position which centers all highlights
|
|
|
|
var startTop = block.scrollTop;
|
|
|
|
var targetTop = highlightBounds.top + ( Math.min( highlightBounds.bottom - highlightBounds.top, viewportHeight ) - viewportHeight ) / 2;
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Account for offsets in position applied to the
|
|
|
|
// <table> that holds our lines of code
|
|
|
|
var lineTable = block.querySelector( '.hljs-ln' );
|
|
|
|
if( lineTable ) targetTop += lineTable.offsetTop - parseInt( blockStyles.paddingTop );
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Make sure the scroll target is within bounds
|
|
|
|
targetTop = Math.max( Math.min( targetTop, block.scrollHeight - viewportHeight ), 0 );
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
if( skipAnimation === true || startTop === targetTop ) {
|
|
|
|
block.scrollTop = targetTop;
|
|
|
|
}
|
|
|
|
else {
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Don't attempt to scroll if there is no overflow
|
|
|
|
if( block.scrollHeight <= viewportHeight ) return;
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
var time = 0;
|
|
|
|
var animate = function() {
|
|
|
|
time = Math.min( time + 0.02, 1 );
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Update our eased scroll position
|
|
|
|
block.scrollTop = startTop + ( targetTop - startTop ) * RevealHighlight.easeInOutQuart( time );
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Keep animating unless we've reached the end
|
|
|
|
if( time < 1 ) {
|
|
|
|
scrollState.animationFrameID = requestAnimationFrame( animate );
|
|
|
|
}
|
|
|
|
};
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
animate();
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
}
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
},
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
/**
|
|
|
|
* The easing function used when scrolling.
|
|
|
|
*/
|
|
|
|
easeInOutQuart: function( t ) {
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// easeInOutQuart
|
|
|
|
return t<.5 ? 8*t*t*t*t : 1-8*(--t)*t*t*t;
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
},
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
getHighlightedLineBounds: function( block ) {
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
var highlightedLines = block.querySelectorAll( '.highlight-line' );
|
|
|
|
if( highlightedLines.length === 0 ) {
|
|
|
|
return { top: 0, bottom: 0 };
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var firstHighlight = highlightedLines[0];
|
|
|
|
var lastHighlight = highlightedLines[ highlightedLines.length -1 ];
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
return {
|
|
|
|
top: firstHighlight.offsetTop,
|
|
|
|
bottom: lastHighlight.offsetTop + lastHighlight.offsetHeight
|
2020-03-12 17:08:20 +01:00
|
|
|
}
|
2020-04-15 10:23:51 +02:00
|
|
|
}
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
},
|
2020-03-12 17:08:20 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
/**
|
|
|
|
* Visually emphasize specific lines within a code block.
|
|
|
|
* This only works on blocks with line numbering turned on.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} block a <code> block
|
|
|
|
* @param {String} [linesToHighlight] The lines that should be
|
|
|
|
* highlighted in this format:
|
|
|
|
* "1" = highlights line 1
|
|
|
|
* "2,5" = highlights lines 2 & 5
|
|
|
|
* "2,5-7" = highlights lines 2, 5, 6 & 7
|
|
|
|
*/
|
|
|
|
highlightLines: function( block, linesToHighlight ) {
|
2019-03-08 15:07:59 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
var highlightSteps = RevealHighlight.deserializeHighlightSteps( linesToHighlight || block.getAttribute( 'data-line-numbers' ) );
|
2019-03-08 15:07:59 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
if( highlightSteps.length ) {
|
2019-03-08 15:07:59 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
highlightSteps[0].forEach( function( highlight ) {
|
2019-03-08 15:07:59 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
var elementsToHighlight = [];
|
2019-03-14 13:05:01 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Highlight a range
|
|
|
|
if( typeof highlight.end === 'number' ) {
|
|
|
|
elementsToHighlight = [].slice.call( block.querySelectorAll( 'table tr:nth-child(n+'+highlight.start+'):nth-child(-n+'+highlight.end+')' ) );
|
|
|
|
}
|
|
|
|
// Highlight a single line
|
|
|
|
else if( typeof highlight.start === 'number' ) {
|
|
|
|
elementsToHighlight = [].slice.call( block.querySelectorAll( 'table tr:nth-child('+highlight.start+')' ) );
|
|
|
|
}
|
2019-04-05 19:49:17 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
if( elementsToHighlight.length ) {
|
|
|
|
elementsToHighlight.forEach( function( lineElement ) {
|
|
|
|
lineElement.classList.add( 'highlight-line' );
|
|
|
|
} );
|
2019-03-08 15:07:59 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
block.classList.add( 'has-highlights' );
|
|
|
|
}
|
2019-03-11 16:12:25 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
} );
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
}
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
},
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
/**
|
|
|
|
* Parses and formats a user-defined string of line
|
|
|
|
* numbers to highlight.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* RevealHighlight.deserializeHighlightSteps( '1,2|3,5-10' )
|
|
|
|
* // [
|
|
|
|
* // [ { start: 1 }, { start: 2 } ],
|
|
|
|
* // [ { start: 3 }, { start: 5, end: 10 } ]
|
|
|
|
* // ]
|
|
|
|
*/
|
|
|
|
deserializeHighlightSteps: function( highlightSteps ) {
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Remove whitespace
|
|
|
|
highlightSteps = highlightSteps.replace( /\s/g, '' );
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Divide up our line number groups
|
|
|
|
highlightSteps = highlightSteps.split( RevealHighlight.HIGHLIGHT_STEP_DELIMITER );
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
return highlightSteps.map( function( highlights ) {
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
return highlights.split( RevealHighlight.HIGHLIGHT_LINE_DELIMITER ).map( function( highlight ) {
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Parse valid line numbers
|
|
|
|
if( /^[\d-]+$/.test( highlight ) ) {
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
highlight = highlight.split( RevealHighlight.HIGHLIGHT_LINE_RANGE_DELIMITER );
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
var lineStart = parseInt( highlight[0], 10 ),
|
|
|
|
lineEnd = parseInt( highlight[1], 10 );
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
if( isNaN( lineEnd ) ) {
|
|
|
|
return {
|
|
|
|
start: lineStart
|
|
|
|
};
|
2019-04-05 07:59:28 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-04-15 10:23:51 +02:00
|
|
|
return {
|
|
|
|
start: lineStart,
|
|
|
|
end: lineEnd
|
|
|
|
};
|
|
|
|
}
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
}
|
|
|
|
// If no line numbers are provided, no code will be highlighted
|
|
|
|
else {
|
2019-03-11 16:12:25 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
return {};
|
2019-03-08 15:07:59 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
}
|
2019-03-08 15:07:59 +01:00
|
|
|
|
2019-04-05 07:59:28 +02:00
|
|
|
} );
|
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
} );
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
},
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
/**
|
|
|
|
* Serializes parsed line number data into a string so
|
|
|
|
* that we can store it in the DOM.
|
|
|
|
*/
|
|
|
|
serializeHighlightSteps: function( highlightSteps ) {
|
2019-04-05 19:49:17 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
return highlightSteps.map( function( highlights ) {
|
2019-04-05 19:49:17 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
return highlights.map( function( highlight ) {
|
2019-04-05 19:49:17 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
// Line range
|
|
|
|
if( typeof highlight.end === 'number' ) {
|
|
|
|
return highlight.start + RevealHighlight.HIGHLIGHT_LINE_RANGE_DELIMITER + highlight.end;
|
|
|
|
}
|
|
|
|
// Single line
|
|
|
|
else if( typeof highlight.start === 'number' ) {
|
|
|
|
return highlight.start;
|
|
|
|
}
|
|
|
|
// All lines
|
|
|
|
else {
|
|
|
|
return '';
|
|
|
|
}
|
2019-04-05 19:49:17 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
} ).join( RevealHighlight.HIGHLIGHT_LINE_DELIMITER );
|
2019-03-08 15:07:59 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
} ).join( RevealHighlight.HIGHLIGHT_STEP_DELIMITER );
|
2019-04-05 07:59:28 +02:00
|
|
|
|
2013-04-28 20:12:31 +02:00
|
|
|
}
|
2012-10-28 23:09:54 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
}
|
2019-03-12 13:17:50 +01:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
export default RevealHighlight;
|