Skip to content

Commit c69f169

Browse files
authored
Fixes data-fragment-index on code block (#533)
resolves #532
1 parent 8484b01 commit c69f169

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

examples/release-5.2.adoc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ console.log(a+b); <3>
2525
<2> using the `%step` attribute...
2626
<3> to explain code more easily !
2727

28+
== Synchronising step-by-step syntax highlighting
29+
[.fragment, java, highlight="1|2|3", step=0]
30+
----
31+
Optional<String> foo();
32+
Optional<String> bar();
33+
Optional<String> foobar();
34+
----
35+
[.fragment, data-fragment-index=0]
36+
step-by-step syntax highlighting
37+
[.fragment, data-fragment-index=1]
38+
In combination with data-fragment-index
39+
[.fragment, data-fragment-index=2]
40+
Allow synchronized fragment display
41+
2842
[transition=fade,transition-speed=slow]
2943
== Learn More!
3044

lib/asciidoctor-revealjs/highlightjs.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,18 @@ def docinfo location, doc, opts
223223
var highlightSteps = RevealHighlight.deserializeHighlightSteps( block.getAttribute( 'data-line-numbers' ) );
224224
if( highlightSteps.length > 1 ) {
225225
226+
// When rendered with asciidoc, data-fragment index if it exists lies on the listing block element, so we have to lookup
226227
// If the original code block has a fragment-index,
227228
// each clone should follow in an incremental sequence
228-
var fragmentIndex = parseInt( block.getAttribute( 'data-fragment-index' ), 10 );
229+
var preCodeWrapper = block.parentElement;
230+
var divContent = preCodeWrapper.parentElement;
231+
var listingBlock = divContent.parentElement;
232+
var fragmentIndex = parseInt( listingBlock.getAttribute( 'data-fragment-index' ), 10 );
229233
230234
if( typeof fragmentIndex !== 'number' || isNaN( fragmentIndex ) ) {
231235
fragmentIndex = null;
236+
} else {
237+
fragmentIndex += 1;
232238
}
233239
234240
// Generate fragments for all steps except the original block

test/doctest/source-highlightjs-languages.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,18 @@ <h2>Use the Source</h2>
409409
var highlightSteps = RevealHighlight.deserializeHighlightSteps( block.getAttribute( 'data-line-numbers' ) );
410410
if( highlightSteps.length > 1 ) {
411411

412+
// When rendered with asciidoc, data-fragment index if it exists lies on the listing block element, so we have to lookup
412413
// If the original code block has a fragment-index,
413414
// each clone should follow in an incremental sequence
414-
var fragmentIndex = parseInt( block.getAttribute( 'data-fragment-index' ), 10 );
415+
var preCodeWrapper = block.parentElement;
416+
var divContent = preCodeWrapper.parentElement;
417+
var listingBlock = divContent.parentElement;
418+
var fragmentIndex = parseInt( listingBlock.getAttribute( 'data-fragment-index' ), 10 );
415419

416420
if( typeof fragmentIndex !== 'number' || isNaN( fragmentIndex ) ) {
417421
fragmentIndex = null;
422+
} else {
423+
fragmentIndex += 1;
418424
}
419425

420426
// Generate fragments for all steps except the original block

0 commit comments

Comments
 (0)