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

resolves #201 resolve relative PlantUML includes from the diagram directory #216

Merged
merged 2 commits into from
Feb 16, 2021
Merged
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
4 changes: 0 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 - diagram base directory
* @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
3 changes: 3 additions & 0 deletions test/fixtures/docs/diagrams/hello.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
!include ./style.puml

Bob->Alice: Hello
1 change: 1 addition & 0 deletions test/fixtures/docs/diagrams/style.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
skinparam monochrome true
1 change: 1 addition & 0 deletions test/fixtures/docs/hello.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
plantuml::diagrams/hello.puml[]
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
11 changes: 11 additions & 0 deletions test/preprocess.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const url = require('url')

chai.use(dirtyChai)

const asciidoctorKroki = require('../src/asciidoctor-kroki.js')
const asciidoctor = require('@asciidoctor/core')()

const { preprocessVegaLite } = require('../src/preprocess.js')

describe('Vega-Lite preprocessing', () => {
Expand Down Expand Up @@ -520,4 +523,12 @@ skinparam BackgroundColor black
here -> there`
)
})

it('should resolve PlantUML includes from the diagram directory', () => {
const registry = asciidoctor.Extensions.create()
asciidoctorKroki.register(registry)
const file = path.join(__dirname, 'fixtures', 'docs', 'hello.adoc')
const html = asciidoctor.convertFile(file, { safe: 'safe', extension_registry: registry, to_file: false })
expect(html).to.contain('https://kroki.io/plantuml/svg/eNorzs7MK0gsSsxVyM3Py0_OKMrPTVUoKSpN5eJyyk_StXPMyUxOtVLwSM3JyQcAc1EPvA==')
})
})
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