Skip to content

Commit

Permalink
Fix asciinema duplicate ui asset
Browse files Browse the repository at this point in the history
- Now simply not trying to add duplicate ui assets
  which should fix errors in full builds.
- Fixes #17
  • Loading branch information
jvalkeal committed Nov 9, 2023
1 parent 7c5967c commit e024072
Show file tree
Hide file tree
Showing 2 changed files with 46 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
35 changes: 33 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 "duplicate file"

Check failure on line 34 in test/asciinema-extension-test.js

View workflow job for this annotation

GitHub Actions / build

Expected an error object to be thrown

Check failure on line 34 in test/asciinema-extension-test.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
}
this.files.push(f)
},
findByType (f) {
return []
findByType (t) {
return this.files.filter(({ type }) => type === t)
},
})

Expand Down Expand Up @@ -137,5 +140,33 @@ 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')
})

Check failure on line 169 in test/asciinema-extension-test.js

View workflow job for this annotation

GitHub Actions / build

Block must not be padded by blank lines

})
})

0 comments on commit e024072

Please sign in to comment.