Skip to content

Commit 8758e87

Browse files
committed
Add sync-group-id support
Closes gh-21
1 parent 4f9f28f commit 8758e87

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

lib/include-code-extension.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function createExtensionGroup (context) {
4747
return accum.concat({ name, lang, lines })
4848
}, [])
4949
if (!includes.length) return log(doc, 'warn', `no code includes found for ${target}`)
50-
const tabsSource = generateTabsSource(attrs.title, includes, tabsEnabled)
50+
const tabsSource = generateTabsSource(attrs.title, attrs['sync-group-id'], includes, tabsEnabled)
5151
const reader = PreprocessorReader.$new(doc, tabsSource, cursor)
5252
Object.defineProperty(reader, 'lineno', { get: () => cursor.lineno })
5353
return this.parseContent(parent, reader)
@@ -56,16 +56,17 @@ function createExtensionGroup (context) {
5656
}
5757
}
5858

59-
function generateTabsSource (title, includes, tabsEnabled) {
59+
function generateTabsSource (title, syncGroupId, includes, tabsEnabled) {
6060
const source = []
6161
if (includes.length === 1) {
6262
if (title) source.push('.' + title)
6363
source.push(`[,${includes[0].lang}]`, '----', ...includes[0].lines, '----')
6464
} else if (tabsEnabled) {
6565
if (title) source.push('.' + title)
6666
const lastIdx = includes.length - 1
67+
const tabAttrs = syncGroupId ? `,sync-group-id=${syncGroupId}` : ''
6768
includes.forEach(({ name, lang, lines }, idx) => {
68-
idx ? source.push('') : source.push('[tabs]', '======')
69+
idx ? source.push('') : source.push(`[tabs${tabAttrs}]`, '======')
6970
source.push(`${name}::`, '+', `[,${lang}]`, '----', ...lines, '----')
7071
if (idx === lastIdx) source.push('======')
7172
})

test/include-code-extension-test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,66 @@ describe('include-code-extension', () => {
281281
expect(actual.getBlocks()[0].getAttributes()).to.include(expectedAttrs)
282282
})
283283

284+
it('should support sync-group-id', () => {
285+
addExample(
286+
'kotlin/hello.kt',
287+
heredoc`
288+
fun main(args : Array<String>) {
289+
println("Hello, World!")
290+
}
291+
`
292+
)
293+
addExample(
294+
'java/hello.java',
295+
heredoc`
296+
public class Hello {
297+
public static void main (String[] args) {
298+
System.out.println("Hello, World!");
299+
}
300+
}
301+
`
302+
)
303+
const input = heredoc`
304+
= Test
305+
:tabs-sync-option:
306+
307+
include-code::hello[sync-group-id=thisisauniqueid]
308+
`
309+
const actual = run(input, { registerAsciidoctorTabs: true })
310+
expect(actual.convert()).to.contain(
311+
'class="openblock tabs is-sync data-sync-group-id=thisisauniqueid is-loading"'
312+
)
313+
})
314+
315+
it('should support sync-group-id not defined', () => {
316+
addExample(
317+
'kotlin/hello.kt',
318+
heredoc`
319+
fun main(args : Array<String>) {
320+
println("Hello, World!")
321+
}
322+
`
323+
)
324+
addExample(
325+
'java/hello.java',
326+
heredoc`
327+
public class Hello {
328+
public static void main (String[] args) {
329+
System.out.println("Hello, World!");
330+
}
331+
}
332+
`
333+
)
334+
const input = heredoc`
335+
= Test
336+
:tabs-sync-option:
337+
338+
include-code::hello[]
339+
`
340+
const actual = run(input, { registerAsciidoctorTabs: true })
341+
expect(actual.convert()).to.contain('class="openblock tabs is-sync is-loading"')
342+
})
343+
284344
it('should report line number of block macro when include tag not found', () => {
285345
const inputSource = heredoc`
286346
fun main(args : Array<String>) {

0 commit comments

Comments
 (0)