This repository was archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathfragments.js.html
More file actions
1468 lines (1329 loc) · 38.7 KB
/
Copy pathfragments.js.html
File metadata and controls
1468 lines (1329 loc) · 38.7 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: fragments.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: fragments.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>"use strict";
var documents = require('./documents');
var DateUtils = require('./utils/date');
var WithFragments = documents.WithFragments;
var GroupDoc = documents.GroupDoc;
/**
* Embodies a plain text fragment (beware: not a structured text)
* @constructor
* @global
* @alias Fragments:Text
*/
function Text(data) {
this.value = data;
}
Text.prototype = {
/**
* Turns the fragment into a useable HTML version of it.
* If the native HTML code doesn't suit your design, this function is meant to be overriden.
*
* @returns {string} - basic HTML code for the fragment
*/
asHtml: function () {
return "<span>" + this.value + "</span>";
},
/**
* Turns the fragment into a useable text version of it.
*
* @returns {string} - basic text version of the fragment
*/
asText: function() {
return this.value;
}
};
/**
* Embodies a document link fragment (a link that is internal to a prismic.io repository)
* @constructor
* @global
* @alias Fragments:DocumentLink
*/
function DocumentLink(data) {
this.value = data;
this.document = data.document;
/**
* @field
* @description the linked document id
*/
this.id = data.document.id;
/**
* @field
* @description the linked document uid
*/
this.uid = data.document.uid;
/**
* @field
* @description the linked document tags
*/
this.tags = data.document.tags;
/**
* @field
* @description the linked document slug
*/
this.slug = data.document.slug;
/**
* @field
* @description the linked document type
*/
this.type = data.document.type;
/**
* @field
* @description the linked document language
*/
this.lang = data.document.lang;
var fragmentsData = {};
if (data.document.data) {
for (var field in data.document.data[data.document.type]) {
fragmentsData[data.document.type + '.' + field] = data.document.data[data.document.type][field];
}
}
/**
* @field
* @description the fragment list, if the fetchLinks parameter was used in at query time
*/
this.fragments = parseFragments(fragmentsData);
/**
* @field
* @description true if the link is broken, false otherwise
*/
this.isBroken = data.isBroken;
}
DocumentLink.prototype = Object.create(WithFragments.prototype);
/**
* Turns the fragment into a useable HTML version of it.
* If the native HTML code doesn't suit your design, this function is meant to be overriden.
*
* @params {object} ctx - mandatory ctx object, with a useable linkResolver function (please read prismic.io online documentation about this)
* @returns {string} - basic HTML code for the fragment
*/
DocumentLink.prototype.asHtml = function (ctx) {
return "<a href=\""+this.url(ctx)+"\">"+this.url(ctx)+"</a>";
};
/**
* Returns the URL of the document link.
*
* @params {object} linkResolver - mandatory linkResolver function (please read prismic.io online documentation about this)
* @returns {string} - the proper URL to use
*/
DocumentLink.prototype.url = function (linkResolver) {
return linkResolver(this, this.isBroken);
};
/**
* Turns the fragment into a useable text version of it.
*
* @returns {string} - basic text version of the fragment
*/
DocumentLink.prototype.asText = function(linkResolver) {
return this.url(linkResolver);
};
/**
* Embodies a web link fragment
* @constructor
* @global
* @alias Fragments:WebLink
*/
function WebLink(data) {
/**
* @field
* @description the JSON object exactly as is returned in the "data" field of the JSON responses (see API documentation: https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation#json-responses)
*/
this.value = data;
}
WebLink.prototype = {
/**
* Turns the fragment into a useable HTML version of it.
* If the native HTML code doesn't suit your design, this function is meant to be overriden.
*
* @returns {string} - basic HTML code for the fragment
*/
asHtml: function () {
var target = this.value.target ? 'target="' + this.value.target + '"' : '';
return "<a " + target + " href=\""+this.url()+"\">"+this.url()+"</a>";
},
/**
* Returns the URL of the link.
*
* @returns {string} - the proper URL to use
*/
url: function() {
return this.value.url;
},
/**
* Turns the fragment into a useable text version of it.
*
* @returns {string} - basic text version of the fragment
*/
asText: function() {
return this.url();
}
};
/**
* Embodies a file link fragment
* @constructor
* @global
* @alias Fragments:FileLink
*/
function FileLink(data) {
/**
* @field
* @description the JSON object exactly as is returned in the "data" field of the JSON responses (see API documentation: https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation#json-responses)
*/
this.value = data;
}
FileLink.prototype = {
/**
* Turns the fragment into a useable HTML version of it.
* If the native HTML code doesn't suit your design, this function is meant to be overriden.
*
* @returns {string} - basic HTML code for the fragment
*/
asHtml: function () {
return "<a href=\""+this.url()+"\">"+this.value.file.name+"</a>";
},
/**
* Returns the URL of the link.
*
* @returns {string} - the proper URL to use
*/
url: function() {
return this.value.file.url;
},
/**
* Turns the fragment into a useable text version of it.
*
* @returns {string} - basic text version of the fragment
*/
asText: function() {
return this.url();
}
};
/**
* Embodies an image link fragment
* @constructor
* @global
* @alias Fragments:ImageLink
*/
function ImageLink(data) {
/**
*
* @field
* @description the JSON object exactly as is returned in the "data" field of the JSON responses (see API documentation: https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation#json-responses)
*/
this.value = data;
}
ImageLink.prototype = {
/**
* Turns the fragment into a useable HTML version of it.
* If the native HTML code doesn't suit your design, this function is meant to be overriden.
*
* @returns {string} - basic HTML code for the fragment
*/
asHtml: function () {
return "<a href=\"" + this.url() + "\"><img src=\"" + this.url() + "\" alt=\"" + (this.alt || "") + " copyright=\"" + (this.copyright || "") + "\" \"></a>";
},
/**
* Returns the URL of the link.
*
* @returns {string} - the proper URL to use
*/
url: function() {
return this.value.image.url;
},
/**
* Turns the fragment into a useable text version of it.
*
* @returns {string} - basic text version of the fragment
*/
asText: function() {
return this.url();
}
};
/**
* Embodies a select fragment
* @constructor
* @global
* @alias Fragments:Select
*/
function Select(data) {
/**
* @field
* @description the text value of the fragment
*/
this.value = data;
}
Select.prototype = {
/**
* Turns the fragment into a useable HTML version of it.
* If the native HTML code doesn't suit your design, this function is meant to be overriden.
*
* @returns {string} - basic HTML code for the fragment
*/
asHtml: function () {
return "<span>" + this.value + "</span>";
},
/**
* Turns the fragment into a useable text version of it.
*
* @returns {string} - basic text version of the fragment
*/
asText: function() {
return this.value;
}
};
/**
* Embodies a color fragment
* @constructor
* @global
* @alias Fragments:Color
*/
function Color(data) {
/**
* @field
* @description the text value of the fragment
*/
this.value = data;
}
Color.prototype = {
/**
* Turns the fragment into a useable HTML version of it.
* If the native HTML code doesn't suit your design, this function is meant to be overriden.
*
* @returns {string} - basic HTML code for the fragment
*/
asHtml: function () {
return "<span>" + this.value + "</span>";
},
/**
* Turns the fragment into a useable text version of it.
*
* @returns {string} - basic text version of the fragment
*/
asText: function() {
return this.value;
}
};
/**
* Embodies a geopoint
* @constructor
* @global
* @alias Fragments:GeoPoint
*/
function GeoPoint(data) {
/**
* @field
* @description the latitude of the geo point
*/
this.latitude = data.latitude;
/**
* @field
* @description the longitude of the geo point
*/
this.longitude = data.longitude;
}
GeoPoint.prototype = {
/**
* Turns the fragment into a useable HTML version of it.
* If the native HTML code doesn't suit your design, this function is meant to be overriden.
*
* @returns {string} - basic HTML code for the fragment
*/
asHtml: function () {
return '<div class="geopoint"><span class="latitude">' + this.latitude + '</span><span class="longitude">' + this.longitude + '</span></div>';
},
/**
* Turns the fragment into a useable text version of it.
*
* @returns {string} - basic text version of the fragment
*/
asText: function() {
return '(' + this.latitude + "," + this.longitude + ')';
}
};
/**
* Embodies a Number fragment
* @constructor
* @global
* @alias Fragments:Num
*/
function Num(data) {
/**
* @field
* @description the integer value of the fragment
*/
this.value = data;
}
Num.prototype = {
/**
* Turns the fragment into a useable HTML version of it.
* If the native HTML code doesn't suit your design, this function is meant to be overriden.
*
* @returns {string} - basic HTML code for the fragment
*/
asHtml: function () {
return "<span>" + this.value + "</span>";
},
/**
* Turns the fragment into a useable text version of it.
*
* @returns {string} - basic text version of the fragment
*/
asText: function() {
if (this.value === null) {
return null;
} else {
return this.value.toString();
}
}
};
/**
* Embodies a Date fragment
* @constructor
* @global
* @alias Fragments:Date
*/
function DateFragment(data) {
/**
* @field
* @description the Date value of the fragment (as a regular JS Date object)
*/
this.value = new Date(data);
}
DateFragment.prototype = {
/**
* Turns the fragment into a useable HTML version of it.
* If the native HTML code doesn't suit your design, this function is meant to be overriden.
*
* @returns {string} - basic HTML code for the fragment
*/
asHtml: function () {
return "<time>" + this.value + "</time>";
},
/**
* Turns the fragment into a useable text version of it.
*
* @returns {string} - basic text version of the fragment
*/
asText: function() {
if (this.value === null) {
return null;
} else {
return this.value.toString();
}
}
};
/**
* Embodies a Timestamp fragment
* @constructor
* @global
* @alias Fragments:Timestamp
*/
function Timestamp(data) {
/**
* @field
* @description the Date value of the fragment (as a regular JS Date object)
*/
this.value = DateUtils.parse(data);
}
Timestamp.prototype = {
/**
* Turns the fragment into a useable HTML version of it.
* If the native HTML code doesn't suit your design, this function is meant to be overriden.
*
* @returns {string} - basic HTML code for the fragment
*/
asHtml: function () {
return "<time>" + this.value + "</time>";
},
/**
* Turns the fragment into a useable text version of it.
*
* @returns {string} - basic text version of the fragment
*/
asText: function() {
if (this.value === null) {
return null;
} else {
return this.value.toString();
}
}
};
/**
* Embodies an embed fragment
* @constructor
* @global
* @alias Fragments:Embed
*/
function Embed(data) {
/**
* @field
* @description the JSON object exactly as is returned in the "data" field of the JSON responses (see API documentation: https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation#json-responses)
*/
this.value = data;
}
Embed.prototype = {
/**
* Turns the fragment into a useable HTML version of it.
* If the native HTML code doesn't suit your design, this function is meant to be overriden.
*
* @returns {string} - basic HTML code for the fragment
*/
asHtml: function () {
return this.value.oembed.html;
},
/**
* Turns the fragment into a useable text version of it.
*
* @returns {string} - basic text version of the fragment
*/
asText: function() {
return "";
}
};
/**
* Embodies an Image fragment
* @constructor
* @global
* @alias Fragments:ImageEl
*/
function ImageEl(main, views) {
/**
* @field
* @description the main ImageView for this image
*/
this.main = main;
/**
* @field
* @description the url of the main ImageView for this image
*/
this.url = main.url;
/**
* @field
* @description an array of all the other ImageViews for this image
*/
this.views = views || {};
}
ImageEl.prototype = {
/**
* Gets the view of the image, from its name
*
* @param {string} name - the name of the view to get
* @returns {ImageView} - the proper view
*/
getView: function(name) {
if (name === "main") {
return this.main;
} else {
return this.views[name];
}
},
/**
* Turns the fragment into a useable HTML version of it.
* If the native HTML code doesn't suit your design, this function is meant to be overriden.
*
* @returns {string} - basic HTML code for the fragment
*/
asHtml: function () {
return this.main.asHtml();
},
/**
* Turns the fragment into a useable text version of it.
*
* @returns {string} - basic text version of the fragment
*/
asText: function() {
return "";
}
};
/**
* Embodies an image view (an image in prismic.io can be defined with several different thumbnail sizes, each size is called a "view")
* @constructor
* @global
* @alias Fragments:ImageView
*/
function ImageView(url, width, height, alt, copyright) {
/**
* @field
* @description the URL of the ImageView (useable as it, in a <img> tag in HTML, for instance)
*/
this.url = url;
/**
* @field
* @description the width of the ImageView
*/
this.width = width;
/**
* @field
* @description the height of the ImageView
*/
this.height = height;
/**
* @field
* @description the alt text for the ImageView
*/
this.alt = alt;
/**
* @field
* @description the copyright for the ImageView
*/
this.copyright = copyright;
}
ImageView.prototype = {
ratio: function () {
return this.width / this.height;
},
/**
* Turns the fragment into a useable HTML version of it.
* If the native HTML code doesn't suit your design, this function is meant to be overriden.
*
* @returns {string} - basic HTML code for the fragment
*/
asHtml: function () {
return '<img src="' + this.url + '" width="' + this.width + '" height="' + this.height + '" alt="' + (this.alt || "") + '" copyright="' + (this.copyright || "") + '">';
},
/**
* Turns the fragment into a useable text version of it.
*
* @returns {string} - basic text version of the fragment
*/
asText: function() {
return "";
}
};
/**
* Embodies a fragment of type "Separator" (only used in Slices)
* @constructor
* @global
* @alias Fragments:Separator
*/
function Separator() {
}
Separator.prototype = {
asHtml: function() {
return "<hr/>";
},
asText: function() {
return "----";
}
};
/**
* Embodies a fragment of type "Group" (which is a group of subfragments)
* @constructor
* @global
* @alias Fragments:Group
*/
function Group(data) {
this.value = [];
for (var i = 0; i < data.length; i++) {
this.value.push(new GroupDoc(data[i]));
}
}
Group.prototype = {
/**
* Turns the fragment into a useable HTML version of it.
* If the native HTML code doesn't suit your design, this function is meant to be overriden.
* @params {function} linkResolver - linkResolver function (please read prismic.io online documentation about this)
* @returns {string} - basic HTML code for the fragment
*/
asHtml: function(linkResolver) {
var output = "";
for (var i = 0; i < this.value.length; i++) {
output += this.value[i].asHtml(linkResolver);
}
return output;
},
/**
* Turns the Group fragment into an array in order to access its items (groups of fragments),
* or to loop through them.
* @params {object} ctx - mandatory ctx object, with a useable linkResolver function (please read prismic.io online documentation about this)
* @returns {Array} - the array of groups, each group being a JSON object with subfragment name as keys, and subfragment as values
*/
toArray: function(){
return this.value;
},
/**
* Turns the fragment into a useable text version of it.
*
* @returns {string} - basic text version of the fragment
*/
asText: function(linkResolver) {
var output = "";
for (var i=0; i<this.value.length; i++) {
output += this.value[i].asText(linkResolver) + '\n';
}
return output;
},
getFirstImage: function() {
return this.toArray().reduce(function(image, fragment) {
if (image) return image;
else {
return fragment.getFirstImage();
}
}, null);
},
getFirstTitle: function() {
return this.toArray().reduce(function(st, fragment) {
if (st) return st;
else {
return fragment.getFirstTitle();
}
}, null);
},
getFirstParagraph: function() {
return this.toArray().reduce(function(st, fragment) {
if (st) return st;
else {
return fragment.getFirstParagraph();
}
}, null);
}
};
/**
* Embodies a structured text fragment
* @constructor
* @global
* @alias Fragments:StructuredText
*/
function StructuredText(blocks) {
this.blocks = blocks;
}
StructuredText.prototype = {
/**
* @returns {object} the first heading block in the text
*/
getTitle: function () {
for(var i=0; i<this.blocks.length; i++) {
var block = this.blocks[i];
if(block.type.indexOf('heading') === 0) {
return block;
}
}
return null;
},
/**
* @returns {object} the first block of type paragraph
*/
getFirstParagraph: function() {
for(var i=0; i<this.blocks.length; i++) {
var block = this.blocks[i];
if(block.type == 'paragraph') {
return block;
}
}
return null;
},
/**
* @returns {array} all paragraphs
*/
getParagraphs: function() {
var paragraphs = [];
for(var i=0; i<this.blocks.length; i++) {
var block = this.blocks[i];
if(block.type == 'paragraph') {
paragraphs.push(block);
}
}
return paragraphs;
},
/**
* @returns {object} the nth paragraph
*/
getParagraph: function(n) {
return this.getParagraphs()[n];
},
/**
* @returns {object}
*/
getFirstImage: function() {
for(var i=0; i<this.blocks.length; i++) {
var block = this.blocks[i];
if(block.type == 'image') {
return new ImageView(
block.url,
block.dimensions.width,
block.dimensions.height,
block.alt,
block.copyright
);
}
}
return null;
},
/**
* Turns the fragment into a useable HTML version of it.
* If the native HTML code doesn't suit your design, this function is meant to be overriden.
* @params {function} linkResolver - please read prismic.io online documentation about link resolvers
* @params {function} htmlSerializer optional HTML serializer to customize the output
* @returns {string} - basic HTML code for the fragment
*/
asHtml: function(linkResolver, htmlSerializer) {
var blockGroups = [],
blockGroup,
block,
html = [];
if (!isFunction(linkResolver)) {
// Backward compatibility with the old ctx argument
var ctx = linkResolver;
linkResolver = function(doc, isBroken) {
return ctx.linkResolver(ctx, doc, isBroken);
};
}
if (Array.isArray(this.blocks)) {
for(var i=0; i < this.blocks.length; i++) {
block = this.blocks[i];
// Resolve image links
if (block.type == "image" && block.linkTo) {
var link = initField(block.linkTo);
block.linkUrl = link.url(linkResolver);
}
if (block.type !== "list-item" && block.type !== "o-list-item") {
// it's not a type that groups
blockGroups.push(block);
blockGroup = null;
} else if (!blockGroup || blockGroup.type != ("group-" + block.type)) {
// it's a new type or no BlockGroup was set so far
blockGroup = {
type: "group-" + block.type,
blocks: [block]
};
blockGroups.push(blockGroup);
} else {
// it's the same type as before, no touching blockGroup
blockGroup.blocks.push(block);
}
}
var blockContent = function(block) {
var content = "";
if (block.blocks) {
block.blocks.forEach(function (block2) {
content = content + serialize(block2, blockContent(block2), htmlSerializer);
});
} else {
content = insertSpans(block.text, block.spans, linkResolver, htmlSerializer);
}
return content;
};
blockGroups.forEach(function (blockGroup) {
html.push(serialize(blockGroup, blockContent(blockGroup), htmlSerializer));
});
}
return html.join('');
},
/**
* Turns the fragment into a useable text version of it.
*
* @returns {string} - basic text version of the fragment
*/
asText: function() {
var output = [];
for(var i=0; i<this.blocks.length; i++) {
var block = this.blocks[i];
if (block.text) {
output.push(block.text);
}
}
return output.join(' ');
}
};
function htmlEscape(input) {
return input && input.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/\n/g, "<br>");
}
/**
* Parses a block that has spans, and inserts the proper HTML code.
*
* @param {string} text - the original text of the block
* @param {object} spans - the spans as returned by the API
* @param {object} linkResolver - the function to build links that may be in the fragment (please read prismic.io's online documentation about this)
* @param {function} htmlSerializer - optional serializer
* @returns {string} - the HTML output
*/
function insertSpans(text, spans, linkResolver, htmlSerializer) {
if (!spans || !spans.length) {
return htmlEscape(text);
}
var tagsStart = {};
var tagsEnd = {};
spans.forEach(function (span) {
if (!tagsStart[span.start]) { tagsStart[span.start] = []; }
if (!tagsEnd[span.end]) { tagsEnd[span.end] = []; }
tagsStart[span.start].push(span);
tagsEnd[span.end].unshift(span);
});
var c;
var html = "";
var stack = [];
for (var pos = 0, len = text.length + 1; pos < len; pos++) { // Looping to length + 1 to catch closing tags
if (tagsEnd[pos]) {
tagsEnd[pos].forEach(function () {
// Close a tag
var tag = stack.pop();
// Continue only if block contains content.
if (typeof tag !== 'undefined') {
var innerHtml = serialize(tag.span, tag.text, htmlSerializer);
if (stack.length === 0) {
// The tag was top level
html += innerHtml;
} else {
// Add the content to the parent tag
stack[stack.length - 1].text += innerHtml;
}
}
});
}
if (tagsStart[pos]) {
// Sort bigger tags first to ensure the right tag hierarchy
tagsStart[pos].sort(function (a, b) {
return (b.end - b.start) - (a.end - a.start);
});
tagsStart[pos].forEach(function (span) {
// Open a tag
var url = null;
if (span.type == "hyperlink") {
var fragment = initField(span.data);
if (fragment) {
url = fragment.url(linkResolver);
} else {
if (console && console.error) console.error('Impossible to convert span.data as a Fragment', span);
return;
}
span.url = url;
}
var elt = {
span: span,
text: ""
};
stack.push(elt);
});
}