Skip to content

Commit

Permalink
Resolve relative PlantUML includes from the diagram directory (not fr…
Browse files Browse the repository at this point in the history
…om the Asciidoctor base directory)

fixes asciidoctor#201
  • Loading branch information
anb0s committed Feb 14, 2021
1 parent a87b2d5 commit d25e860
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/asciidoctor-kroki.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const processKroki = (processor, parent, attrs, diagramType, diagramText, contex
if (plantUmlInclude) {
diagramText = `!include ${plantUmlInclude}\n${diagramText}`
}
diagramText = require('./preprocess.js').preprocessPlantUML(diagramText, context, doc.getBaseDir())
diagramText = require('./preprocess.js').preprocessPlantUML(diagramText, context, diagramDir)
}
}
const blockId = attrs.id
Expand Down
6 changes: 3 additions & 3 deletions src/preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ function removePlantUmlTags (diagramText) {
/**
* @param {string} diagramText
* @param {any} context
* @param {string} baseDir - base directory
* @param {string} diagramDir
* @returns {string}
*/
module.exports.preprocessPlantUML = function (diagramText, context, baseDir = '.') {
module.exports.preprocessPlantUML = function (diagramText, context, diagramDir = '') {
const includeOnce = []
const includeStack = []
diagramText = preprocessPlantUmlIncludes(diagramText, baseDir, includeOnce, includeStack, context.vfs)
diagramText = preprocessPlantUmlIncludes(diagramText, diagramDir, includeOnce, includeStack, context.vfs)
return removePlantUmlTags(diagramText)
}

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/plantuml/hello.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!include ./style-general.iuml
Bob->Alice: Hello
15 changes: 15 additions & 0 deletions test/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ alice -> bob
expect(html).to.contain(`https://kroki.io/plantuml/svg/${encodeText(diagramText)}`)
expect(html).to.contain('<div class="imageblock sequence kroki-format-svg kroki">')
}).timeout(5000)
it('should convert a PlantUML diagram and resolve include relative to diagram directory', () => {
const file = ospath.join(__dirname, 'fixtures', 'plantuml', 'hello.puml')
const diagramText = fs.readFileSync(file, 'utf8')
.replace(/^!include (.*)\r?\n/m, fs.readFileSync(ospath.join(__dirname, 'fixtures', 'plantuml', 'style-general.iuml'), 'utf8') + '\n')
const input = `plantuml::${file}[svg,role=sequence]`
const registry = asciidoctor.Extensions.create()
asciidoctorKroki.register(registry)
const html = asciidoctor.convert(input, {
safe: 'safe',
extension_registry: registry,
base_dir: ospath.join(__dirname, 'fixtures')
})
expect(html).to.contain(`https://kroki.io/plantuml/svg/${encodeText(diagramText)}`)
expect(html).to.contain('<div class="imageblock sequence kroki-format-svg kroki">')
}).timeout(5000)
it('should convert a diagram with a relative path to an image', () => {
const input = `
:imagesdir: .asciidoctor/kroki
Expand Down

0 comments on commit d25e860

Please sign in to comment.