Skip to content

Commit

Permalink
Add sync-group-id support
Browse files Browse the repository at this point in the history
Closes gh-21
  • Loading branch information
rwinch committed Sep 18, 2024
1 parent 4f9f28f commit 8758e87
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/include-code-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -56,16 +56,17 @@ 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)
source.push(`[,${includes[0].lang}]`, '----', ...includes[0].lines, '----')
} 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('======')
})
Expand Down
60 changes: 60 additions & 0 deletions test/include-code-extension-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) {
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<String>) {
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<String>) {
Expand Down

0 comments on commit 8758e87

Please sign in to comment.