diff --git a/lib/include-code-extension.js b/lib/include-code-extension.js index 5a4a97f..a32c85e 100644 --- a/lib/include-code-extension.js +++ b/lib/include-code-extension.js @@ -47,7 +47,7 @@ function createExtensionGroup (context) { return accum.concat({ name, lang, lines }) }, []) if (!includes.length) return log(doc, 'warn', `no code includes found for ${target}`) - const tabsSource = generateTabsSource(attrs.title, includes, tabsEnabled) + const tabsSource = generateTabsSource(attrs.title, attrs['sync-group-id'], includes, tabsEnabled) const reader = PreprocessorReader.$new(doc, tabsSource, cursor) Object.defineProperty(reader, 'lineno', { get: () => cursor.lineno }) return this.parseContent(parent, reader) @@ -56,7 +56,7 @@ function createExtensionGroup (context) { } } -function generateTabsSource (title, includes, tabsEnabled) { +function generateTabsSource (title, syncGroupId, includes, tabsEnabled) { const source = [] if (includes.length === 1) { if (title) source.push('.' + title) @@ -64,8 +64,9 @@ function generateTabsSource (title, includes, tabsEnabled) { } else if (tabsEnabled) { if (title) source.push('.' + title) const lastIdx = includes.length - 1 + const tabAttrs = syncGroupId ? `,sync-group-id=${syncGroupId}` : '' includes.forEach(({ name, lang, lines }, idx) => { - idx ? source.push('') : source.push('[tabs]', '======') + idx ? source.push('') : source.push(`[tabs${tabAttrs}]`, '======') source.push(`${name}::`, '+', `[,${lang}]`, '----', ...lines, '----') if (idx === lastIdx) source.push('======') }) diff --git a/test/include-code-extension-test.js b/test/include-code-extension-test.js index e4da6af..31444c4 100644 --- a/test/include-code-extension-test.js +++ b/test/include-code-extension-test.js @@ -281,6 +281,66 @@ describe('include-code-extension', () => { expect(actual.getBlocks()[0].getAttributes()).to.include(expectedAttrs) }) + it('should support sync-group-id', () => { + addExample( + 'kotlin/hello.kt', + heredoc` + fun main(args : Array) { + println("Hello, World!") + } + ` + ) + addExample( + 'java/hello.java', + heredoc` + public class Hello { + public static void main (String[] args) { + System.out.println("Hello, World!"); + } + } + ` + ) + const input = heredoc` + = Test + :tabs-sync-option: + + include-code::hello[sync-group-id=thisisauniqueid] + ` + const actual = run(input, { registerAsciidoctorTabs: true }) + expect(actual.convert()).to.contain( + 'class="openblock tabs is-sync data-sync-group-id=thisisauniqueid is-loading"' + ) + }) + + it('should support sync-group-id not defined', () => { + addExample( + 'kotlin/hello.kt', + heredoc` + fun main(args : Array) { + println("Hello, World!") + } + ` + ) + addExample( + 'java/hello.java', + heredoc` + public class Hello { + public static void main (String[] args) { + System.out.println("Hello, World!"); + } + } + ` + ) + const input = heredoc` + = Test + :tabs-sync-option: + + include-code::hello[] + ` + const actual = run(input, { registerAsciidoctorTabs: true }) + expect(actual.convert()).to.contain('class="openblock tabs is-sync is-loading"') + }) + it('should report line number of block macro when include tag not found', () => { const inputSource = heredoc` fun main(args : Array) {