Skip to content

Commit 5357d8d

Browse files
MSSandroidRokt33r
authored andcommitted
remove code redundancy in parsing of PlantUml
1 parent d069722 commit 5357d8d

File tree

1 file changed

+18
-52
lines changed

1 file changed

+18
-52
lines changed

browser/lib/markdown.js

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -183,81 +183,47 @@ class Markdown {
183183
})
184184

185185
const deflate = require('markdown-it-plantuml/lib/deflate')
186-
this.md.use(require('markdown-it-plantuml'), {
187-
generateSource: function (umlCode) {
188-
const stripTrailingSlash = (url) => url.endsWith('/') ? url.slice(0, -1) : url
189-
const serverAddress = stripTrailingSlash(config.preview.plantUMLServerAddress) + '/svg'
190-
const s = unescape(encodeURIComponent(umlCode))
191-
const zippedCode = deflate.encode64(
192-
deflate.zip_deflate(`@startuml\n${s}\n@enduml`, 9)
193-
)
194-
return `${serverAddress}/${zippedCode}`
195-
}
186+
const plantuml = require('markdown-it-plantuml')
187+
const plantUmlStripTrailingSlash = (url) => url.endsWith('/') ? url.slice(0, -1) : url
188+
const plantUmlServerAddress = plantUmlStripTrailingSlash(config.preview.plantUMLServerAddress)
189+
const parsePlantUml = function (umlCode, openMarker, closeMarker, type){
190+
const s = unescape(encodeURIComponent(umlCode))
191+
const zippedCode = deflate.encode64(
192+
deflate.zip_deflate(`${openMarker}\n${s}\n${closeMarker}`, 9)
193+
)
194+
return `${plantUmlServerAddress}/${type}/${zippedCode}`
195+
}
196+
197+
this.md.use(plantuml, {
198+
generateSource: (umlCode) => parsePlantUml(umlCode, '@startuml', '@enduml', 'svg')
196199
})
197200

198-
const plantuml = require('markdown-it-plantuml')
199-
// Ditaa support
201+
// Ditaa support. PlantUML server doesn't support Ditaa in SVG, so we set the format as PNG at the moment.
200202
this.md.use(plantuml, {
201203
openMarker: '@startditaa',
202204
closeMarker: '@endditaa',
203-
generateSource: function (umlCode) {
204-
const stripTrailingSlash = (url) => url.endsWith('/') ? url.slice(0, -1) : url
205-
// Currently PlantUML server doesn't support Ditaa in SVG, so we set the format as PNG at the moment.
206-
const serverAddress = stripTrailingSlash(config.preview.plantUMLServerAddress) + '/png'
207-
const s = unescape(encodeURIComponent(umlCode))
208-
const zippedCode = deflate.encode64(
209-
deflate.zip_deflate(`@startditaa\n${s}\n@endditaa`, 9)
210-
)
211-
return `${serverAddress}/${zippedCode}`
212-
}
205+
generateSource: (umlCode) => parsePlantUml(umlCode, '@startditaa', '@endditaa', 'png')
213206
})
214207

215208
// Mindmap support
216209
this.md.use(plantuml, {
217210
openMarker: '@startmindmap',
218211
closeMarker: '@endmindmap',
219-
generateSource: function (umlCode) {
220-
const stripTrailingSlash = (url) => url.endsWith('/') ? url.slice(0, -1) : url
221-
// Currently PlantUML server doesn't support Ditaa in SVG, so we set the format as PNG at the moment.
222-
const serverAddress = stripTrailingSlash(config.preview.plantUMLServerAddress) + '/svg'
223-
const s = unescape(encodeURIComponent(umlCode))
224-
const zippedCode = deflate.encode64(
225-
deflate.zip_deflate(`@startmindmap\n${s}\n@endmindmap`, 9)
226-
)
227-
return `${serverAddress}/${zippedCode}`
228-
}
212+
generateSource: (umlCode) => parsePlantUml(umlCode, '@startmindmap', '@endmindmap', 'svg')
229213
})
230214

231215
// WBS support
232216
this.md.use(plantuml, {
233217
openMarker: '@startwbs',
234218
closeMarker: '@endwbs',
235-
generateSource: function (umlCode) {
236-
const stripTrailingSlash = (url) => url.endsWith('/') ? url.slice(0, -1) : url
237-
// Currently PlantUML server doesn't support Ditaa in SVG, so we set the format as PNG at the moment.
238-
const serverAddress = stripTrailingSlash(config.preview.plantUMLServerAddress) + '/svg'
239-
const s = unescape(encodeURIComponent(umlCode))
240-
const zippedCode = deflate.encode64(
241-
deflate.zip_deflate(`@startwbs\n${s}\n@endwbs`, 9)
242-
)
243-
return `${serverAddress}/${zippedCode}`
244-
}
219+
generateSource: (umlCode) => parsePlantUml(umlCode, '@startwbs', '@endwbs', 'svg')
245220
})
246221

247222
// Gantt support
248223
this.md.use(plantuml, {
249224
openMarker: '@startgantt',
250225
closeMarker: '@endgantt',
251-
generateSource: function (umlCode) {
252-
const stripTrailingSlash = (url) => url.endsWith('/') ? url.slice(0, -1) : url
253-
// Currently PlantUML server doesn't support Ditaa in SVG, so we set the format as PNG at the moment.
254-
const serverAddress = stripTrailingSlash(config.preview.plantUMLServerAddress) + '/svg'
255-
const s = unescape(encodeURIComponent(umlCode))
256-
const zippedCode = deflate.encode64(
257-
deflate.zip_deflate(`@startgantt\n${s}\n@endgantt`, 9)
258-
)
259-
return `${serverAddress}/${zippedCode}`
260-
}
226+
generateSource: (umlCode) => parsePlantUml(umlCode, '@startgantt', '@endgantt', 'svg')
261227
})
262228

263229
// Override task item

0 commit comments

Comments
 (0)