Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix/fix-kroki-fetch-diagram-attribute-in-js-version #453

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/browser/asciidoctor-kroki.js
Original file line number Diff line number Diff line change
Expand Up @@ -20057,7 +20057,7 @@ function extend() {
},{}],70:[function(require,module,exports){
module.exports={
"name": "asciidoctor-kroki",
"version": "0.17.0",
"version": "0.18.2",
"description": "Asciidoctor extension to convert diagrams to images using Kroki",
"type": "commonjs",
"main": "./src/asciidoctor-kroki.js",
Expand Down
31 changes: 23 additions & 8 deletions src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ module.exports.save = function (krokiDiagram, doc, target, vfs, krokiClient) {
const add = typeof vfs !== 'undefined' && typeof vfs.add === 'function' ? vfs.add : require('./node-fs.js').add

const imagesOutputDirectory = getImagesOutputDirectory(doc)
const dataUri = doc.isAttribute('data-uri')
const diagramUrl = krokiDiagram.getDiagramUri(krokiClient.getServerUrl())
const format = krokiDiagram.format
const diagramName = `${target || 'diag'}-${rusha.createHash().update(diagramUrl).digest('hex')}.${format}`
const filePath = path.format({ dir: imagesOutputDirectory, base: diagramName })

let encoding
let mediaType
if (format === 'txt' || format === 'atxt' || format === 'utxt') {
Expand All @@ -43,13 +45,26 @@ module.exports.save = function (krokiDiagram, doc, target, vfs, krokiClient) {
mediaType = 'image/png'
encoding = 'binary'
}

// file is either (already) on the file system or we should read it from Kroki
const contents = exists(filePath) ? read(filePath, encoding) : krokiClient.getImage(krokiDiagram, encoding)
add({
relative: imagesOutputDirectory,
basename: diagramName,
mediaType,
contents: Buffer.from(contents, encoding)
})
return diagramName

let contents

if(!exists(filePath)) {
contents = read(diagramUrl)

add({
relative: imagesOutputDirectory,
basename: diagramName,
mediaType,
contents: Buffer.from(contents, encoding)
})
}

if (dataUri) {
if (!contents) contents = read(filePath)
return 'data:' + mediaType + ";base64," + Buffer.from(contents, encoding).toString('base64')
} else {
return filePath
}
}
6 changes: 4 additions & 2 deletions src/http/http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ const httpRequest = (XMLHttpRequest, uri, method, headers, encoding = 'utf8', bo
try {
const xhr = new XMLHttpRequest()
xhr.open(method, uri, false)
for (const [name, value] in Object.entries(headers)) {
xhr.setRequestHeader(name, value)
if (headers) {
for (const [name, value] in Object.entries(headers)) {
xhr.setRequestHeader(name, value)
}
}
if (encoding === 'binary') {
xhr.responseType = 'arraybuffer'
Expand Down
20 changes: 16 additions & 4 deletions test/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ plantuml::test/fixtures/alice.puml[png,role=sequence]
expect(html).to.contain(`https://kroki.io/plantuml/svg/${encode(macroFile)}`)
expect(html).to.contain('<div class="imageblock sequence kroki-format-svg kroki">')
})
it('should download and embed an SVG image with kroki-fetch-diagram and kroki-data-uri', () => {
const registry = asciidoctor.Extensions.create()
asciidoctorKroki.register(registry)
const html = asciidoctor.convert(fs.readFileSync(fixturePath('fetch', 'doc.adoc')), {
attributes: {
'kroki-data': ''
},
extension_registry: registry,
safe: 'unsafe'
})
expect(html).to.contain('<img src="data:image/svg+xml;base64,PD94bWwgdmVyc2l')
})
it('should create diagrams in imagesdir if kroki-fetch-diagram is set', async () => {
const registry = asciidoctor.Extensions.create()
asciidoctorKroki.register(registry)
Expand Down Expand Up @@ -427,7 +439,7 @@ Bob->Alice : hello
http.get.restore()
}
})
it('should embed an SVG image with built-in allow-uri-read and data-uri (available in Asciidoctor.js 2+)', () => {
it('should embed an SVG image with built-in allow-uri-read and data-uri', () => {
const input = `
:imagesdir: .asciidoctor/kroki

Expand Down Expand Up @@ -1276,7 +1288,7 @@ paragraph
}
]
for (const { location, pageAttr, blockAttr } of inlineOptionsFixtures) {
it(`should inline (via ${location}) an SVG image with built-in allow-uri-read (available in Asciidoctor.js 2+)`, () => {
it(`should inline (via ${location}) an SVG image with built-in allow-uri-read`, () => {
const input = `
:imagesdir: .asciidoctor/kroki
${pageAttr}
Expand Down Expand Up @@ -1333,7 +1345,7 @@ bytefield::test/fixtures/simple.bytefield[svg,role=bytefield${blockAttr}]
}
]
for (const { location, pageAttr, blockAttr } of interactiveOptionsFixtures) {
it(`should include an interactive (via ${location}) SVG image with built-in allow-uri-read and data-uri (available in Asciidoctor.js 2+)`, () => {
it(`should include an interactive (via ${location}) SVG image with built-in allow-uri-read and data-uri`, () => {
const input = `
:imagesdir: .asciidoctor/kroki
${pageAttr}
Expand Down Expand Up @@ -1370,4 +1382,4 @@ plantuml::test/fixtures/alice.puml[svg,role=sequence${blockAttr}]
}
})
})
})
})