Skip to content

Commit

Permalink
Merge pull request #18 from jvalkeal/gh17
Browse files Browse the repository at this point in the history
Fix asciinema duplicate ui asset
  • Loading branch information
rwinch authored Nov 9, 2023
2 parents 7c5967c + 1dd1c8c commit bb28662
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
19 changes: 13 additions & 6 deletions lib/asciinema-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 32 additions & 2 deletions test/asciinema-extension-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
})

Expand Down Expand Up @@ -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')
})
})
})

0 comments on commit bb28662

Please sign in to comment.