Skip to content

Commit

Permalink
Fixes data-fragment-index on code block (#533)
Browse files Browse the repository at this point in the history
resolves #532
  • Loading branch information
jtama authored May 4, 2024
1 parent 8484b01 commit c69f169
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
14 changes: 14 additions & 0 deletions examples/release-5.2.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ console.log(a+b); <3>
<2> using the `%step` attribute...
<3> to explain code more easily !

== Synchronising step-by-step syntax highlighting
[.fragment, java, highlight="1|2|3", step=0]
----
Optional<String> foo();
Optional<String> bar();
Optional<String> foobar();
----
[.fragment, data-fragment-index=0]
step-by-step syntax highlighting
[.fragment, data-fragment-index=1]
In combination with data-fragment-index
[.fragment, data-fragment-index=2]
Allow synchronized fragment display

[transition=fade,transition-speed=slow]
== Learn More!

Expand Down
8 changes: 7 additions & 1 deletion lib/asciidoctor-revealjs/highlightjs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,18 @@ def docinfo location, doc, opts
var highlightSteps = RevealHighlight.deserializeHighlightSteps( block.getAttribute( 'data-line-numbers' ) );
if( highlightSteps.length > 1 ) {
// When rendered with asciidoc, data-fragment index if it exists lies on the listing block element, so we have to lookup
// 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 );
var preCodeWrapper = block.parentElement;
var divContent = preCodeWrapper.parentElement;
var listingBlock = divContent.parentElement;
var fragmentIndex = parseInt( listingBlock.getAttribute( 'data-fragment-index' ), 10 );
if( typeof fragmentIndex !== 'number' || isNaN( fragmentIndex ) ) {
fragmentIndex = null;
} else {
fragmentIndex += 1;
}
// Generate fragments for all steps except the original block
Expand Down
8 changes: 7 additions & 1 deletion test/doctest/source-highlightjs-languages.html
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,18 @@ <h2>Use the Source</h2>
var highlightSteps = RevealHighlight.deserializeHighlightSteps( block.getAttribute( 'data-line-numbers' ) );
if( highlightSteps.length > 1 ) {

// When rendered with asciidoc, data-fragment index if it exists lies on the listing block element, so we have to lookup
// 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 );
var preCodeWrapper = block.parentElement;
var divContent = preCodeWrapper.parentElement;
var listingBlock = divContent.parentElement;
var fragmentIndex = parseInt( listingBlock.getAttribute( 'data-fragment-index' ), 10 );

if( typeof fragmentIndex !== 'number' || isNaN( fragmentIndex ) ) {
fragmentIndex = null;
} else {
fragmentIndex += 1;
}

// Generate fragments for all steps except the original block
Expand Down

0 comments on commit c69f169

Please sign in to comment.