From d25e86067a5d118b03cda29ca00a3a52dcf01e92 Mon Sep 17 00:00:00 2001 From: Andre Bossert Date: Sun, 14 Feb 2021 12:28:53 +0100 Subject: [PATCH] Resolve relative PlantUML includes from the diagram directory (not from the Asciidoctor base directory) fixes #201 --- src/asciidoctor-kroki.js | 2 +- src/preprocess.js | 6 +++--- test/fixtures/plantuml/hello.puml | 2 ++ test/test.spec.js | 15 +++++++++++++++ 4 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/plantuml/hello.puml diff --git a/src/asciidoctor-kroki.js b/src/asciidoctor-kroki.js index 255c022c..07481512 100644 --- a/src/asciidoctor-kroki.js +++ b/src/asciidoctor-kroki.js @@ -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 diff --git a/src/preprocess.js b/src/preprocess.js index 0f857490..eb6e0569 100644 --- a/src/preprocess.js +++ b/src/preprocess.js @@ -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) } diff --git a/test/fixtures/plantuml/hello.puml b/test/fixtures/plantuml/hello.puml new file mode 100644 index 00000000..774217fc --- /dev/null +++ b/test/fixtures/plantuml/hello.puml @@ -0,0 +1,2 @@ +!include ./style-general.iuml +Bob->Alice: Hello \ No newline at end of file diff --git a/test/test.spec.js b/test/test.spec.js index 480e7b06..98ba27cc 100644 --- a/test/test.spec.js +++ b/test/test.spec.js @@ -98,6 +98,21 @@ alice -> bob expect(html).to.contain(`https://kroki.io/plantuml/svg/${encodeText(diagramText)}`) expect(html).to.contain('
') }).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('
') + }).timeout(5000) it('should convert a diagram with a relative path to an image', () => { const input = ` :imagesdir: .asciidoctor/kroki