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
|
import {marked} from 'marked';
import {bindFind} from '../../util/find.js';
import {parseInput} from '../../util/replacer.js';
import {replacerSpec} from '../../util/transform-content.js';
const linkThingRelationMap = {
album: 'linkAlbum',
albumCommentary: 'linkAlbumCommentary',
albumGallery: 'linkAlbumGallery',
artist: 'linkArtist',
artistGallery: 'linkArtistGallery',
flash: 'linkFlash',
groupInfo: 'linkGroup',
groupGallery: 'linkGroupGallery',
listing: 'linkListing',
newsEntry: 'linkNewsEntry',
staticPage: 'linkStaticPage',
tag: 'linkArtTag',
track: 'linkTrack',
};
const linkValueRelationMap = {
// media: 'linkPathFromMedia',
// root: 'linkPathFromRoot',
// site: 'linkPathFromSite',
};
const linkIndexRelationMap = {
// commentaryIndex: 'linkCommentaryIndex',
// flashIndex: 'linkFlashIndex',
// home: 'linkHome',
// listingIndex: 'linkListingIndex',
// newsIndex: 'linkNewsIndex',
};
function getPlaceholder(node, content) {
return {type: 'text', data: content.slice(node.i, node.iEnd)};
}
export default {
contentDependencies: [
...Object.values(linkThingRelationMap),
...Object.values(linkValueRelationMap),
...Object.values(linkIndexRelationMap),
],
extraDependencies: ['html', 'language', 'wikiData'],
sprawl(wikiData, content) {
const find = bindFind(wikiData);
const parsedNodes = parseInput(content);
return {
nodes: parsedNodes
.map(node => {
if (node.type !== 'tag') {
return node;
}
const placeholder = getPlaceholder(node, content);
const replacerKeyImplied = !node.data.replacerKey;
const replacerKey = replacerKeyImplied ? 'track' : node.data.replacerKey.data;
// TODO: We don't support recursive nodes like before, at the moment. Sorry!
// const replacerValue = transformNodes(node.data.replacerValue, opts);
const replacerValue = node.data.replacerValue[0].data;
const spec = replacerSpec[replacerKey];
if (!spec) {
return placeholder;
}
if (spec.link) {
let data = {key: spec.link};
determineData: {
// No value at all: this is an index link.
if (!replacerValue) {
break determineData;
}
// Nothing to find: the link operates on a path or string, not a data object.
if (!spec.find) {
data.value = replacerValue;
break determineData;
}
const thing =
find[spec.find](
(replacerKeyImplied
? replacerValue
: replacerKey + `:` + replacerValue),
wikiData);
// Nothing was found: this is unexpected, so return placeholder.
if (!thing) {
return placeholder;
}
// Something was found: the link operates on that thing.
data.thing = thing;
}
const {transformName} = spec;
// TODO: Again, no recursive nodes. Sorry!
// const enteredLabel = node.data.label && transformNode(node.data.label, opts);
const enteredLabel = node.data.label?.data;
const enteredHash = node.data.hash?.data;
data.label =
enteredLabel ??
(transformName && data.thing.name
? transformName(data.thing.name, node, content)
: null);
data.hash = enteredHash ?? null;
return {i: node.i, iEnd: node.iEnd, type: 'link', data};
}
// This will be another {type: 'tag'} node which gets processed in
// generate.
return node;
}),
};
},
data(sprawl, content) {
return {
content,
nodes:
sprawl.nodes
.map(node => {
// Replace link nodes with a stub. It'll be replaced (by position)
// with an item from relations.
if (node.type === 'link') {
return {type: 'link'};
}
// Other nodes will get processed in generate.
return node;
}),
};
},
relations(relation, sprawl, content) {
const {nodes} = sprawl;
const relationOrPlaceholder =
(node, name, arg) =>
(name
? {
link: relation(name, arg),
label: node.data.label,
hash: node.data.hash,
}
: getPlaceholder(node, content));
return {
links:
nodes
.filter(({type}) => type === 'link')
.map(node => {
const {key, thing, value} = node.data;
if (thing) {
return relationOrPlaceholder(node, linkThingRelationMap[key], thing);
} else if (value) {
return relationOrPlaceholder(node, linkValueRelationMap[key], value);
} else {
return relationOrPlaceholder(node, linkIndexRelationMap[key]);
}
}),
};
},
generate(data, relations, {html, language}) {
let linkIndex = 0;
// This array contains only straight text and link nodes, which are directly
// representable in html (so no further processing is needed on the level of
// individual nodes).
const contentFromNodes =
data.nodes.map(node => {
if (node.type === 'text') {
return {type: 'text', data: node.data};
}
if (node.type === 'link') {
const linkNode = relations.links[linkIndex++];
if (linkNode.type === 'text') {
return {type: 'text', data: linkNode.data};
}
const {link, label, hash} = linkNode;
return {
type: 'link',
data: link.slots({content: label, hash}),
};
}
if (node.type === 'tag') {
const {replacerKey, replacerValue} = node.data;
const spec = replacerSpec[replacerKey];
if (!spec) {
return getPlaceholder(node, data.content);
}
const {value: valueFn, html: htmlFn} = spec;
const value =
(valueFn
? valueFn(replacerValue)
: replacerValue);
const contents =
(htmlFn
? htmlFn(value, {html, language})
: value);
return {type: 'text', data: contents};
}
return getPlaceholder(node, data.content);
});
return html.template({
annotation: `transformContent`,
slots: {
mode: {
validate: v => v.is('inline', 'multiline', 'lyrics'),
default: 'multiline',
},
},
content(slots) {
// In inline mode, no further processing is needed!
if (slots.mode === 'inline') {
return html.tags(contentFromNodes.map(node => node.data));
}
// Multiline mode has a secondary processing stage where it's passed...
// through marked! Rolling your own Markdown only gets you so far :D
const markedOptions = {
headerIds: false,
mangle: false,
};
// This is separated into its own function just since we're gonna reuse
// it in a minute if everything goes to heck in lyrics mode.
const transformMultiline = () =>
marked.parse(
contentFromNodes
.map(node => {
if (node.type === 'text') {
return node.data.replace(/\n+/g, '\n\n');
} else {
return node.data.toString();
}
})
.join(''),
markedOptions);
if (slots.mode === 'multiline') {
// Unfortunately, we kind of have to be super evil here and stringify
// the links, or else parse marked's output into html tags, which is
// very out of scope at the moment.
return transformMultiline();
}
// Lyrics mode goes through marked too, but line breaks are processed
// differently. Instead of having each line get its own paragraph,
// "adjacent" lines are joined together (with blank lines separating
// each verse/paragraph).
if (slots.mode === 'lyrics') {
// If it looks like old data, using <br> instead of bunched together
// lines... then oh god... just use transformMultiline. Perishes.
if (
contentFromNodes.some(node =>
node.type === 'text' &&
node.data.includes('<br'))
) {
return transformMultiline();
}
// Lyrics mode is also evil for the same stringifying reasons as
// multiline.
return marked.parse(
contentFromNodes
.map(node => {
if (node.type === 'text') {
return node.data.replace(/\b\n\b/g, '<br>\n');
} else {
return node.data.toString();
}
})
.join(''),
markedOptions);
}
},
});
},
}
|