Skip to content

Commit

Permalink
convertRelativePathsToAbsolute upon request
Browse files Browse the repository at this point in the history
  • Loading branch information
sponbergbadger committed Feb 9, 2020
1 parent 3d26291 commit b3e88ee
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/SvgDiagramFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = class SvgDiagramFactory {
constructor(basePath, pluginPathArray) {
this.basePath = basePath
this.parsersProducersRenderers = new ParsersProducersRenderers([...builtinPaths, ...pluginPathArray])
this.convertRelativePathsToAbsolute = false
}

getParsers() {
Expand Down
5 changes: 3 additions & 2 deletions lib/SvgDiagramRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module.exports = class SvgDiagramRenderer {
this.diagramFactory = diagramFactory
this.context = {
basePath: diagramFactory.basePath,
renderers: diagramFactory.getRenderers()
renderers: diagramFactory.getRenderers(),
convertRelativeToAbsolute: diagramFactory.convertRelativePathsToAbsolute,
}
}

Expand Down Expand Up @@ -85,7 +86,7 @@ function renderFonts(fonts, context, outputPath) {
buf += `@import url("${i.trim()}");`
}
} else {
const url = Util.relativeAbsoluteOrRemoteUrl(font.src, context.basePath, outputPath)
const url = Util.relativeAbsoluteOrRemoteUrl(font.src, context.basePath, outputPath, context.convertRelativeToAbsolute)
buf += '@font-face {'
buf += ` font-family: '${font.family}';`
buf += ` src: url('${url}');`
Expand Down
8 changes: 6 additions & 2 deletions lib/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ module.exports = {
}
},

relativeAbsoluteOrRemoteUrl(url, basePath, outputPath) {
relativeAbsoluteOrRemoteUrl(url, basePath, outputPath, convertRelativeToAbsolute = false) {
if (!url.startsWith('http://')
&& !url.startsWith('https://')
&& !url.startsWith('/')) {
Expand All @@ -178,7 +178,11 @@ module.exports = {
} else {
outputPath = path.dirname(outputPath);
}
url = path.relative(outputPath, absoluteUrl)
if (convertRelativeToAbsolute) {
url = absoluteUrl
} else {
url = path.relative(outputPath, absoluteUrl)
}
}
return url
},
Expand Down
2 changes: 1 addition & 1 deletion lib/builtins/Renderers.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function renderImage(obj, sizeAndPosition, styleBlock, svgBlock, context, styleD
const x1 = cx - obj.width / 2
const y1 = cy - obj.height / 2

const url = Util.relativeAbsoluteOrRemoteUrl(obj.url, context.basePath, outputPath)
const url = Util.relativeAbsoluteOrRemoteUrl(obj.url, context.basePath, outputPath, context.convertRelativeToAbsolute)
return `<image x="${round(x1)}" y="${round(y1)}" width="${round(width)}" height="${round(height)}" href="${url}"${styleBlock}${svgBlock}></image>`
}

Expand Down
25 changes: 25 additions & 0 deletions test/SvgDiagramFactory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,28 @@ test('it should resolve absolute file paths', () => {
expect(fs.existsSync(outputPath)).toBeTruthy()
expect(svg.g.image._attributes.href).toBe('../images/image.svg')
})

test('it should output absolute paths upon request', () => {
const basePath = path.resolve(__dirname, 'assets')
const pluginPaths = [path.resolve(__dirname, 'assets/js/Plugins.js')]
const filename = 'diagram19'

const outputPath = path.resolve(basePath, 'output', filename + '.svg')
if (fs.existsSync(outputPath)) {
fs.unlinkSync(outputPath)
}

const diagramFactory = new SvgDiagramFactory(basePath, pluginPaths)
diagramFactory.convertRelativePathsToAbsolute = true

const input = path.resolve(basePath, filename + '.txt')
const output = path.resolve(basePath, 'output', filename + '.svg')

const xml = diagramFactory.renderDiagram(input, output)
const svg = XmlJS.xml2js(xml, {compact: true}).svg

const absoluteImagePath = path.resolve(__dirname, 'assets', 'images', 'image.svg')

expect(fs.existsSync(outputPath)).toBeTruthy()
expect(svg.g.image._attributes.href).toBe(absoluteImagePath)
})

0 comments on commit b3e88ee

Please sign in to comment.