-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
asciidoctor-kroki.js
231 lines (219 loc) · 7.38 KB
/
asciidoctor-kroki.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// @ts-check
const { KrokiDiagram, KrokiClient } = require('./kroki-client.js')
function UnsupportedFormatError (message) {
this.name = 'UnsupportedFormatError'
this.message = message
this.stack = (new Error()).stack
}
// eslint-disable-next-line new-parens
UnsupportedFormatError.prototype = new Error
function InvalidConfigurationError (message) {
this.name = 'InvalidConfigurationError'
this.message = message
this.stack = (new Error()).stack
}
// eslint-disable-next-line new-parens
InvalidConfigurationError.prototype = new Error
const isBrowser = () => {
return typeof window === 'object' && typeof window.XMLHttpRequest === 'object'
}
const createImageSrc = (doc, krokiDiagram, target, vfs, krokiClient) => {
const shouldFetch = doc.isAttribute('kroki-fetch-diagram')
let imageUrl
if (shouldFetch) {
imageUrl = require('./fetch.js').save(krokiDiagram, doc, target, vfs, krokiClient)
} else {
imageUrl = krokiDiagram.getDiagramUri(krokiClient.getServerUrl())
}
return imageUrl
}
/**
* Get the option defined on the block or macro.
*
* First, check if an option is defined as an attribute.
* If there is no match, check if an option is defined as a default settings in the document attributes.
*
* @param attrs - list of attributes
* @param document - Asciidoctor document
* @returns {string|undefined} - the option name or undefined
*/
function getOption (attrs, document) {
const availableOptions = ['inline', 'interactive', 'none']
for (const option of availableOptions) {
if (attrs[`${option}-option`] === '') {
return option
}
}
for (const option of availableOptions) {
if (document.getAttribute('kroki-default-options') === option) {
return option
}
}
}
const processKroki = (processor, parent, attrs, diagramType, diagramText, context) => {
const doc = parent.getDocument()
// If "subs" attribute is specified, substitute accordingly.
// Be careful not to specify "specialcharacters" or your diagram code won't be valid anymore!
const subs = attrs.subs
if (subs) {
diagramText = parent.applySubstitutions(diagramText, parent.$resolve_subs(subs))
}
if (diagramType === 'vegalite') {
diagramText = require('./preprocess.js').preprocessVegaLite(diagramText, context, doc.getBaseDir())
} else if (diagramType === 'plantuml') {
const plantUmlInclude = doc.getAttribute('kroki-plantuml-include')
if (plantUmlInclude) {
diagramText = `!include ${plantUmlInclude}\n${diagramText}`
}
diagramText = require('./preprocess.js').preprocessPlantUML(diagramText, context, doc.getBaseDir())
}
const blockId = attrs.id
const format = attrs.format || doc.getAttribute('kroki-default-format') || 'svg'
const caption = attrs.caption
const title = attrs.title
let role = attrs.role
if (role) {
if (format) {
role = `${role} kroki-format-${format} kroki`
} else {
role = `${role} kroki`
}
} else {
role = 'kroki'
}
const blockAttrs = Object.assign({}, attrs)
blockAttrs.role = role
blockAttrs.format = format
delete blockAttrs.title
delete blockAttrs.caption
delete blockAttrs.opts
const option = getOption(attrs, doc)
if (option && option !== 'none') {
blockAttrs[`${option}-option`] = ''
}
if (blockId) {
blockAttrs.id = blockId
}
const krokiDiagram = new KrokiDiagram(diagramType, format, diagramText)
const httpClient = isBrowser() ? require('./http/browser-http.js') : require('./http/node-http.js')
const krokiClient = new KrokiClient(doc, httpClient)
let block
if (format === 'txt' || format === 'atxt' || format === 'utxt') {
const textContent = krokiClient.getTextContent(krokiDiagram)
block = processor.createBlock(parent, 'literal', textContent, blockAttrs)
} else {
let alt
if (attrs.title) {
alt = attrs.title
} else if (attrs.target) {
alt = attrs.target
} else {
alt = 'Diagram'
}
blockAttrs.target = createImageSrc(doc, krokiDiagram, attrs.target, context.vfs, krokiClient)
blockAttrs.alt = alt
block = processor.createImageBlock(parent, blockAttrs)
}
if (title) {
block['$title='](title)
}
block.$assign_caption(caption, 'figure')
return block
}
function diagramBlock (context) {
return function () {
const self = this
self.onContext(['listing', 'literal'])
self.positionalAttributes(['target', 'format'])
self.process((parent, reader, attrs) => {
const diagramType = this.name.toString()
const role = attrs.role
const diagramText = reader.$read()
try {
return processKroki(this, parent, attrs, diagramType, diagramText, context)
} catch (e) {
console.warn(`Skipping ${diagramType} block. ${e.message}`)
attrs.role = role ? `${role} kroki-error` : 'kroki-error'
return this.createBlock(parent, attrs['cloaked-context'], diagramText, attrs)
}
})
}
}
function diagramBlockMacro (name, context) {
return function () {
const self = this
self.named(name)
self.positionalAttributes(['format'])
self.process((parent, target, attrs) => {
let vfs = context.vfs
target = parent.applySubstitutions(target, ['attributes'])
if (isBrowser()) {
if (!['file://', 'https://', 'http://'].some(prefix => target.startsWith(prefix))) {
// if not an absolute URL, prefix with baseDir in the browser environment
const doc = parent.getDocument()
const baseDir = doc.getBaseDir()
const startDir = typeof baseDir !== 'undefined' ? baseDir : '.'
target = startDir !== '.' ? doc.normalizeWebPath(target, startDir) : target
}
} else {
if (typeof vfs === 'undefined' || typeof vfs.read !== 'function') {
vfs = require('./node-fs.js')
target = parent.normalizeSystemPath(target)
}
}
const role = attrs.role
const diagramType = name
try {
const diagramText = vfs.read(target)
return processKroki(this, parent, attrs, diagramType, diagramText, context)
} catch (e) {
console.warn(`Skipping ${diagramType} block macro. ${e.message}`)
attrs.role = role ? `${role} kroki-error` : 'kroki-error'
return this.createBlock(parent, 'paragraph', `${e.message} - ${diagramType}::${target}[]`, attrs)
}
})
}
}
module.exports.register = function register (registry, context = {}) {
// patch context in case of Antora
if (typeof context.contentCatalog !== 'undefined' && typeof context.contentCatalog.addFile === 'function' && typeof context.file !== 'undefined') {
context.vfs = require('./antora-adapter.js')(context.file, context.contentCatalog, context.vfs)
}
const names = [
'actdiag',
'blockdiag',
'bpmn',
'bytefield',
'c4plantuml',
'ditaa',
'erd',
'excalidraw',
'graphviz',
'mermaid',
'nomnoml',
'nwdiag',
'packetdiag',
'plantuml',
'rackdiag',
'seqdiag',
'svgbob',
'umlet',
'vega',
'vegalite',
'wavedrom'
]
if (typeof registry.register === 'function') {
registry.register(function () {
for (const name of names) {
this.block(name, diagramBlock(context))
this.blockMacro(diagramBlockMacro(name, context))
}
})
} else if (typeof registry.block === 'function') {
for (const name of names) {
registry.block(name, diagramBlock(context))
registry.blockMacro(diagramBlockMacro(name, context))
}
}
return registry
}