diff --git a/lib/asciinema-extension.js b/lib/asciinema-extension.js index ef17cf4..23d12c0 100644 --- a/lib/asciinema-extension.js +++ b/lib/asciinema-extension.js @@ -157,12 +157,19 @@ const toBlock = (attrs, parent, source, context, uiCatalog, defaultOptions, file file.asciidoc.attributes['page-asciinemacasts'] = asciinemaId } - uiCatalog.addFile({ - contents: Buffer.from(source), - path: '_asciinema/' + asciinemaId + '.cast', - type: 'asset', - out: { path: '_asciinema/' + asciinemaId + '.cast' }, - }) + const castFilePath = '_asciinema/' + asciinemaId + '.cast' + const castFile = uiCatalog.findByType('asset').some(({ path }) => path === castFilePath) + + // same file is either duplicate in other page or in other branch + // no need to come up with real global guid + if (!castFile) { + uiCatalog.addFile({ + contents: Buffer.from(source), + path: castFilePath, + type: 'asset', + out: { path: castFilePath }, + }) + } const asciinemaOptions = JSON.stringify(buildOptions(attrs, defaultOptions)) file.asciidoc.attributes['page-asciinema-options-' + asciinemaId] = asciinemaOptions diff --git a/test/asciinema-extension-test.js b/test/asciinema-extension-test.js index 36c395c..2ecf2ca 100644 --- a/test/asciinema-extension-test.js +++ b/test/asciinema-extension-test.js @@ -30,10 +30,13 @@ describe('asciinema-extension', () => { const createUiCatalog = () => ({ files: [], addFile (f) { + if (this.files.some(({ path }) => path === f.path)) { + throw new Error('duplicate file') + } this.files.push(f) }, - findByType (f) { - return [] + findByType (t) { + return this.files.filter(({ type }) => type === t) }, }) @@ -137,5 +140,32 @@ describe('asciinema-extension', () => { expect(uiCatalog.files.length).to.equal(1) expect(out).to.contains('video') }) + + it('should work with duplicate blocks', async () => { + const input = heredoc` + [asciinema] + ---- + foobar + ---- + + [asciinema] + ---- + foobar + ---- + ` + addPage(input) + + ext.register.call(generatorContext, { config: {} }) + generatorContext.updateVariables({ contentCatalog, siteAsciiDocConfig, uiCatalog }) + await generatorContext.contentClassified(generatorContext.variables) + + const registry = asciidoctor.Extensions.create() + siteAsciiDocConfig.extensions[0].register.call({}, registry, { file: { asciidoc: { attributes: {} } } }) + const out = asciidoctor.convert(input, { extension_registry: registry }) + + expect(siteAsciiDocConfig.extensions.length).to.equal(1) + expect(uiCatalog.files.length).to.equal(1) + expect(out).to.contains('video') + }) }) })