-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathschema.js
More file actions
342 lines (312 loc) · 10.3 KB
/
Copy pathschema.js
File metadata and controls
342 lines (312 loc) · 10.3 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
* Defines a custom Schema for DokuWiki
*
* extends the basic schemas provided by ProseMirror
*/
import { schema as schemaBasic } from 'prosemirror-schema-basic';
import { tableNodes } from 'prosemirror-tables';
import { bulletList, listItem, orderedList } from 'prosemirror-schema-list';
export default function getSpec() {
let { nodes, marks } = schemaBasic.spec;
const doc = nodes.get('doc');
doc.content = '(block | baseonly | container | protected_block | substitution_block)+';
doc.attrs = {
nocache: { default: false },
notoc: { default: false },
};
nodes = nodes.update('doc', doc);
// heading shall only contain unmarked text
const heading = nodes.get('heading');
heading.content = 'text*';
heading.marks = '';
heading.group = 'baseonly';
nodes = nodes.update('heading', heading);
orderedList.group = 'container';
orderedList.content = 'list_item+';
nodes = nodes.update('ordered_list', orderedList);
bulletList.group = 'container';
bulletList.content = 'list_item+';
nodes = nodes.update('bullet_list', bulletList);
listItem.content = '(paragraph | protected_block | substitution_block)+ (ordered_list | bullet_list)?';
nodes = nodes.update('list_item', listItem);
nodes = nodes.append(tableNodes({
tableGroup: 'container',
cellContent: '(paragraph | protected_block | substitution_block)+',
cellAttributes: {
align: {
default: '',
setDOMAttr(val, attr) {
if (!val) {
// eslint-disable-next-line no-param-reassign
attr.class = null;
return;
}
// eslint-disable-next-line no-param-reassign
attr.class = `${val}align`;
},
},
},
}));
const tableNode = nodes.get('table');
tableNode.toDOM = function toDOM() {
return ['div', { class: 'table' }, ['table', { class: 'inline' }, ['tbody', 0]]];
};
nodes.update('table', tableNode);
// Nodes: https://prosemirror.net/docs/ref/#model.NodeSpec
nodes = nodes.addToEnd('preformatted', {
content: 'text*',
marks: '',
group: 'protected_block',
code: true,
toDOM() {
return ['pre', { class: 'code' }, 0];
},
});
const codeBlock = nodes.get('code_block');
codeBlock.attrs = {
class: { default: 'code' },
'data-filename': { default: '' },
'data-language': { default: '' },
};
codeBlock.toDOM = function toDOM(node) {
return ['pre', node.attrs, 0];
};
codeBlock.group = 'protected_block';
nodes = nodes.update('code_block', codeBlock);
const quote = nodes.get('blockquote');
quote.content = '(block | blockquote | protected_block)+';
quote.group = 'container';
quote.toDom = () => ['blockquote', {}, ['div', { class: 'no' }, 0]];
nodes.update('blockquote', quote);
const imageNode = nodes.get('image');
imageNode.attrs.width = { default: '' };
imageNode.attrs.height = { default: '' };
imageNode.attrs.align = { default: '' };
imageNode.attrs.linking = { default: '' };
imageNode.attrs.cache = { default: '' };
imageNode.attrs['data-resolvedHtml'] = { default: '' };
imageNode.attrs.id = {};
delete imageNode.attrs.src;
imageNode.parseDOM = [
{
tag: 'img.media,img.medialeft,img.mediacenter,img.mediaright', // FIXME: handle a.media as well
getAttrs: function getAttrs(dom) {
const src = dom.getAttribute('src');
const dokuWikiFetch = `${DOKU_BASE}lib/exe/fetch.php`;
if (!src.includes(dokuWikiFetch)) {
return undefined; // let another rule handle this case
}
const [, query] = src.split('?');
const attrs = query.split('&')
.map(item => item.split('='))
.reduce((acc, [key, value]) => {
acc[key] = decodeURIComponent(value);
return acc;
}, {});
let align = '';
if (dom.classList.contains('medialeft')) {
align = 'left';
} else if (dom.classList.contains('mediaright')) {
align = 'right';
} else if (dom.classList.contains('mediacenter')) {
align = 'center';
}
return {
id: attrs.media,
title: dom.getAttribute('alt'),
width: attrs.w,
height: attrs.h,
align,
'data-resolvedHtml': dom.outerHTML,
};
},
},
{
// handle generice images copied from other pages
tag: 'img[src]:not(.icon)',
getAttrs(dom) {
const src = dom.getAttribute('src');
return {
id: src,
title: dom.getAttribute('alt') || dom.getAttribute('title'),
width: dom.getAttribute('width'),
height: dom.getAttribute('height'),
};
},
},
];
nodes = nodes.update('image', imageNode);
const imageAttrs = {};
Object.keys(imageNode.attrs).forEach((key) => {
imageAttrs[`image-${key}`] = { default: null };
});
nodes = nodes.addToEnd('link', {
group: 'inline',
inline: true,
attrs: {
'data-type': {},
'data-inner': {},
'data-name': { default: null },
'data-resolvedID': { default: null },
'data-resolvedUrl': { default: null },
'data-resolvedName': { default: null },
'data-resolvedClass': { default: null },
'data-resolvedTitle': { default: null },
'data-resolvedImage': { default: '' },
...imageAttrs,
},
toDOM(node) {
return ['a', node.attrs];
},
});
nodes = nodes.addToEnd('footnote', {
content: '',
marks: '',
attrs: {
contentJSON: { default: '' },
},
group: 'inline',
inline: true,
atom: true,
});
nodes = nodes.addToEnd('smiley', {
attrs: {
icon: {},
syntax: {},
},
inline: true,
group: 'inline',
draggable: true,
toDOM: node => ['img', {
src: `${DOKU_BASE}lib/images/smileys/${node.attrs.icon}`,
alt: node.attrs.syntax,
class: 'icon smiley',
}],
parseDOM: [{
tag: 'img.icon',
getAttrs: (dom) => {
const src = dom.getAttribute('src').split('/');
const icon = src.pop();
if (!src.join('/').endsWith('lib/images/smileys')) {
return false;
}
const syntax = dom.getAttribute('alt');
return { icon, syntax };
},
}],
});
nodes = nodes.addToEnd('rss', {
group: 'substitution_block',
atom: true,
attrs: {
class: { default: 'rss' },
url: {},
max: { default: 8 },
reverse: { default: null },
author: { default: null },
date: { default: null },
details: { default: null },
refresh: { default: '' },
renderedHTML: { default: null },
},
});
nodes = nodes.addToEnd('dwplugin_block', {
content: 'text*',
marks: '',
attrs: {
class: { default: 'dwplugin' },
'data-pluginname': { default: ' ' },
},
draggable: true,
inline: false,
group: 'protected_block',
defining: true,
isolating: true,
code: true,
toDOM(node) {
return ['pre', node.attrs, 0];
},
});
nodes = nodes.addToEnd('dwplugin_inline', {
content: 'text*',
attrs: {
class: { default: 'dwplugin' },
'data-pluginname': { default: ' ' },
},
marks: '',
draggable: true,
inline: true,
group: 'inline',
defining: true,
isolating: true,
code: true,
});
// FIXME we need a table header attribute
// FIXME what table cells can accept is to be defined
// FIXME table cells need colspan and rowspan attributes
// FIXME table cells need alignment attributes
// FIXME we don't allow stuff in links
// FIXME extend image node with additional attributes
marks = marks.remove('link');
marks = marks.addToEnd('deleted', {
parseDOM: [
{ tag: 'del' },
{
style: 'text-decoration',
// https://discuss.prosemirror.net/t/dom-parsing-and-getattrs/612
getAttrs: value => value === 'strikethrough' && null,
},
],
toDOM() {
return ['del'];
},
});
marks = marks.addToEnd('underline', {
parseDOM: [
{ tag: 'u' },
{
style: 'text-decoration',
getAttrs: value => value === 'underline' && null,
},
],
toDOM() {
return ['u'];
},
});
marks = marks.addToEnd('subscript', {
parseDOM: [
{ tag: 'sub' },
{
style: 'vertical-align',
getAttrs: value => value === 'sub' && null,
},
],
toDOM() {
return ['sub'];
},
});
marks = marks.addToEnd('superscript', {
parseDOM: [
{ tag: 'sup' },
{
style: 'vertical-align',
getAttrs: value => value === 'super' && null,
},
],
toDOM() {
return ['sup'];
},
});
marks = marks.addToEnd('unformatted', {
excludes: '_',
toDOM() {
return ['span', { class: 'unformatted' }];
},
});
if (window.Prosemirror && window.Prosemirror.pluginSchemas) {
window.Prosemirror.pluginSchemas.forEach((addSchema) => {
({ nodes, marks } = addSchema(nodes, marks));
});
}
return { nodes, marks };
}