-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
extend-node.js
1391 lines (1261 loc) · 36.8 KB
/
extend-node.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
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
const { graphql } = require(`gatsby/graphql`)
const { onCreateNode } = require(`../gatsby-node`)
const extendNodeType = require(`../extend-node-type`)
const { createContentDigest } = require(`gatsby-core-utils`)
const { typeDefs } = require(`../create-schema-customization`)
jest.mock(`gatsby/reporter`, () => {
return {
log: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
activityTimer: () => {
return {
start: jest.fn(),
setStatus: jest.fn(),
end: jest.fn(),
}
},
}
})
// given a set of nodes and a query, return the result of the query
async function queryResult(
nodes,
fragment,
{ additionalParameters = {}, pluginOptions = {} }
) {
const cache = {
// GatsbyCache
get: async () => null,
set: async () => null,
}
const extendNodeTypeFields = await extendNodeType(
{
type: { name: `MarkdownRemark` },
cache,
getCache: () => cache,
getNodesByType: type => [],
...additionalParameters,
},
{
plugins: [],
...pluginOptions,
}
)
const {
createSchemaComposer,
} = require(`../../../gatsby/src/schema/schema-composer`)
const {
addInferredFields,
} = require(`../../../gatsby/src/schema/infer/add-inferred-fields`)
const {
addNodes,
} = require(`../../../gatsby/src/schema/infer/inference-metadata`)
const {
getExampleObject,
} = require(`../../../gatsby/src/schema/infer/build-example-data`)
const typeName = `MarkdownRemark`
const sc = createSchemaComposer()
const tc = sc.createObjectTC(typeName)
sc.addTypeDefs(typeDefs)
const inferenceMetadata = addNodes({ typeName }, nodes)
addInferredFields({
schemaComposer: sc,
typeComposer: tc,
exampleValue: getExampleObject(inferenceMetadata),
})
tc.addFields(extendNodeTypeFields)
sc.Query.addFields({
listNode: { type: [tc], resolve: () => nodes },
})
const schema = sc.buildSchema()
const result = await graphql(
schema,
`query {
listNode {
${fragment}
}
}
`
)
return result
}
const bootstrapTest = (
label,
content,
query,
test,
{ additionalParameters = {}, pluginOptions = {} } = {}
) => {
const node = {
id: `whatever`,
children: [],
internal: {
contentDigest: `whatever`,
mediaType: `text/markdown`,
},
}
// Make some fake functions its expecting.
const loadNodeContent = node => Promise.resolve(node.content)
it(label, async done => {
node.content = content
async function createNode(markdownNode) {
const result = await queryResult([markdownNode], query, {
additionalParameters,
pluginOptions,
})
if (result.errors) {
done.fail(result.errors)
}
try {
test(result.data.listNode[0])
done()
} catch (err) {
done.fail(err)
}
}
const createParentChildLink = jest.fn()
const actions = { createNode, createParentChildLink }
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
// Used to verify that console.warn is called when field not found
jest.spyOn(global.console, `warn`)
await onCreateNode(
{
node,
loadNodeContent,
actions,
createNodeId,
createContentDigest,
},
{ ...additionalParameters, ...pluginOptions }
)
})
}
describe(`Excerpt is generated correctly from schema`, () => {
bootstrapTest(
`correctly loads an excerpt`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
Where oh where is my little pony?`,
`excerpt
excerptAst
frontmatter {
title
}
`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt).toBe(`Where oh where is my little pony?`)
expect(node.excerptAst).toMatchObject({
children: [
{
children: [
{
type: `text`,
value: `Where oh where is my little pony?`,
},
],
properties: {},
tagName: `p`,
type: `element`,
},
],
data: { quirksMode: false },
type: `root`,
})
}
)
bootstrapTest(
`correctly loads a default excerpt`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---`,
`excerpt
excerptAst
frontmatter {
title
}
`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt).toBe(``)
expect(node.excerptAst).toMatchObject({
children: [],
data: { quirksMode: false },
type: `root`,
})
}
)
bootstrapTest(
`correctly uses excerpt separator`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
Where oh where is my little pony?
<!-- end -->
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi auctor sit amet velit id facilisis. Nulla viverra, eros at efficitur pulvinar, lectus orci accumsan nisi, eu blandit elit nulla nec lectus. Integer porttitor imperdiet sapien. Quisque in orci sed nisi consequat aliquam. Aenean id mollis nisi. Sed auctor odio id erat facilisis venenatis. Quisque posuere faucibus libero vel fringilla.
In quis lectus sed eros efficitur luctus. Morbi tempor, nisl eget feugiat tincidunt, sem velit vulputate enim, nec interdum augue enim nec mauris. Nulla iaculis ante sed enim placerat pretium. Nulla metus odio, facilisis vestibulum lobortis vitae, bibendum at nunc. Donec sit amet efficitur metus, in bibendum nisi. Vivamus tempus vel turpis sit amet auctor. Maecenas luctus vestibulum velit, at sagittis leo volutpat quis. Praesent posuere nec augue eget sodales. Pellentesque vitae arcu ut est varius venenatis id maximus sem. Curabitur non consectetur turpis.
`,
`excerpt
excerptAst
frontmatter {
title
}
`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt).toBe(`Where oh where is my little pony?`)
expect(node.excerptAst).toMatchObject({
children: [
{
children: [
{
type: `text`,
value: `Where oh where is my little pony?`,
},
],
properties: {},
tagName: `p`,
type: `element`,
},
{
type: `text`,
value: `\n`,
},
],
data: { quirksMode: false },
type: `root`,
})
},
{ pluginOptions: { excerpt_separator: `<!-- end -->` } }
)
const contentWithSeparator = `---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
Where oh where **is** my little pony?
<!-- end -->
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi auctor sit amet velit id facilisis. Nulla viverra, eros at efficitur pulvinar, lectus orci accumsan nisi, eu blandit elit nulla nec lectus. Integer porttitor imperdiet sapien. Quisque in orci sed nisi consequat aliquam. Aenean id mollis nisi. Sed auctor odio id erat facilisis venenatis. Quisque posuere faucibus libero vel fringilla.
In quis lectus sed eros efficitur luctus. Morbi tempor, nisl eget feugiat tincidunt, sem velit vulputate enim, nec interdum augue enim nec mauris. Nulla iaculis ante sed enim placerat pretium. Nulla metus odio, facilisis vestibulum lobortis vitae, bibendum at nunc. Donec sit amet efficitur metus, in bibendum nisi. Vivamus tempus vel turpis sit amet auctor. Maecenas luctus vestibulum velit, at sagittis leo volutpat quis. Praesent posuere nec augue eget sodales. Pellentesque vitae arcu ut est varius venenatis id maximus sem. Curabitur non consectetur turpis.
`
bootstrapTest(
`given PLAIN correctly uses excerpt separator`,
contentWithSeparator,
`excerpt(format: PLAIN)`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt).toBe(`Where oh where is my little pony?`)
},
{ pluginOptions: { excerpt_separator: `<!-- end -->` } }
)
bootstrapTest(
`given HTML correctly uses excerpt separator`,
contentWithSeparator,
`excerpt(format: HTML)`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt).toBe(
`<p>Where oh where <strong>is</strong> my little pony?</p>\n`
)
},
{ pluginOptions: { excerpt_separator: `<!-- end -->` } }
)
bootstrapTest(
`given MARKDOWN correctly uses excerpt separator`,
contentWithSeparator,
`excerpt(format: MARKDOWN)`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt).toBe(`Where oh where **is** my little pony?\n`)
},
{ pluginOptions: { excerpt_separator: `<!-- end -->` } }
)
const contentWithoutSeparator = `---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
Where oh where **is** my little pony? Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi auctor sit amet velit id facilisis. Nulla viverra, eros at efficitur pulvinar, lectus orci accumsan nisi, eu blandit elit nulla nec lectus. Integer porttitor imperdiet sapien. Quisque in orci sed nisi consequat aliquam. Aenean id mollis nisi. Sed auctor odio id erat facilisis venenatis. Quisque posuere faucibus libero vel fringilla.
In quis lectus sed eros efficitur luctus. Morbi tempor, nisl eget feugiat tincidunt, sem velit vulputate enim, nec interdum augue enim nec mauris. Nulla iaculis ante sed enim placerat pretium. Nulla metus odio, facilisis vestibulum lobortis vitae, bibendum at nunc. Donec sit amet efficitur metus, in bibendum nisi. Vivamus tempus vel turpis sit amet auctor. Maecenas luctus vestibulum velit, at sagittis leo volutpat quis. Praesent posuere nec augue eget sodales. Pellentesque vitae arcu ut est varius venenatis id maximus sem. Curabitur non consectetur turpis.
`
bootstrapTest(
`given MARKDOWN without excerpt separator, falls back to pruneLength`,
contentWithoutSeparator,
`excerpt(pruneLength: 40, format: MARKDOWN)`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt.length).toBe(45)
expect(node.excerpt).toBe(
`Where oh where **is** my little pony? Lorem…\n`
)
},
{ pluginOptions: { excerpt_separator: `<!-- end -->` } }
)
bootstrapTest(
`given MARKDOWN, pruning is done not counting markdown characters`,
contentWithoutSeparator,
`excerpt(pruneLength: 19, format: MARKDOWN)`,
node => {
expect(node).toMatchSnapshot()
// we want the pruning to preserve markdown chars and not count them in the length
expect(node.excerpt.length).toBe(23)
expect(node.excerpt).toBe(`Where oh where **is**…\n`)
}
)
const content = `---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
Where oh where is my little pony? Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi auctor sit amet velit id facilisis. Nulla viverra, eros at efficitur pulvinar, lectus orci accumsan nisi, eu blandit elit nulla nec lectus. Integer porttitor imperdiet sapien. Quisque in orci sed nisi consequat aliquam. Aenean id mollis nisi. Sed auctor odio id erat facilisis venenatis. Quisque posuere faucibus libero vel fringilla.
In quis lectus sed eros efficitur luctus. Morbi tempor, nisl eget feugiat tincidunt, sem velit vulputate enim, nec interdum augue enim nec mauris. Nulla iaculis ante sed enim placerat pretium. Nulla metus odio, facilisis vestibulum lobortis vitae, bibendum at nunc. Donec sit amet efficitur metus, in bibendum nisi. Vivamus tempus vel turpis sit amet auctor. Maecenas luctus vestibulum velit, at sagittis leo volutpat quis. Praesent posuere nec augue eget sodales. Pellentesque vitae arcu ut est varius venenatis id maximus sem. Curabitur non consectetur turpis.
`
bootstrapTest(
`correctly prunes length to default value`,
content,
`excerpt
excerptAst
frontmatter {
title
}
`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt.length).toBe(139)
expect(node.excerptAst.children.length).toBe(1)
expect(node.excerptAst.children[0].children.length).toBe(1)
expect(node.excerptAst.children[0].children[0].value.length).toBe(139)
}
)
bootstrapTest(
`correctly prunes length to provided parameter`,
content,
`excerpt(pruneLength: 50)
excerptAst(pruneLength: 50)
frontmatter {
title
}
`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt.length).toBe(46)
expect(node.excerptAst.children.length).toBe(1)
expect(node.excerptAst.children[0].children.length).toBe(1)
expect(node.excerptAst.children[0].children[0].value.length).toBe(46)
}
)
bootstrapTest(
`correctly prunes length to provided parameter with truncate`,
content,
`excerpt(pruneLength: 50, truncate: true)
excerptAst(pruneLength: 50, truncate: true)
frontmatter {
title
}
`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt.length).toBe(50)
expect(node.excerptAst.children.length).toBe(1)
expect(node.excerptAst.children[0].children.length).toBe(1)
expect(node.excerptAst.children[0].children[0].value.length).toBe(50)
}
)
describe(`when plugins options has a excerpt_separator defined`, () => {
bootstrapTest(
`correctly prunes length to default value`,
content,
`excerpt
excerptAst
frontmatter {
title
}
`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt.length).toBe(139)
expect(node.excerptAst.children.length).toBe(1)
expect(node.excerptAst.children[0].children.length).toBe(1)
expect(node.excerptAst.children[0].children[0].value.length).toBe(139)
},
{ pluginOptions: { excerpt_separator: `<!-- end -->` } }
)
bootstrapTest(
`correctly prunes length to provided parameter`,
content,
`excerpt(pruneLength: 50)
excerptAst(pruneLength: 50)
frontmatter {
title
}
`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt.length).toBe(46)
expect(node.excerptAst.children.length).toBe(1)
expect(node.excerptAst.children[0].children.length).toBe(1)
expect(node.excerptAst.children[0].children[0].value.length).toBe(46)
},
{ pluginOptions: { excerpt_separator: `<!-- end -->` } }
)
bootstrapTest(
`correctly prunes length to provided parameter with truncate`,
content,
`excerpt(pruneLength: 50, truncate: true)
excerptAst(pruneLength: 50, truncate: true)
frontmatter {
title
}
`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt.length).toBe(50)
expect(node.excerptAst.children.length).toBe(1)
expect(node.excerptAst.children[0].children.length).toBe(1)
expect(node.excerptAst.children[0].children[0].value.length).toBe(50)
},
{ pluginOptions: { excerpt_separator: `<!-- end -->` } }
)
})
bootstrapTest(
`given an html format, it correctly maps nested markdown to html`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
Where oh [*where*](nick.com) **_is_** ![that pony](pony.png)?`,
`excerpt(format: HTML)
excerptAst
frontmatter {
title
}
`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt).toBe(
`<p>Where oh <a href="nick.com"><em>where</em></a> <strong><em>is</em></strong> <img src="pony.png" alt="that pony">?</p>`
)
expect(node.excerptAst).toMatchObject({
children: [
{
children: [
{
type: `text`,
value: `Where oh `,
},
{
children: [
{
children: [
{
type: `text`,
value: `where`,
},
],
properties: {},
tagName: `em`,
type: `element`,
},
],
properties: {
href: `nick.com`,
},
tagName: `a`,
type: `element`,
},
{
type: `text`,
value: ` `,
},
{
children: [
{
children: [
{
type: `text`,
value: `is`,
},
],
properties: {},
tagName: `em`,
type: `element`,
},
],
properties: {},
tagName: `strong`,
type: `element`,
},
{
type: `text`,
value: ` `,
},
{
children: [],
properties: {
alt: `that pony`,
src: `pony.png`,
},
tagName: `img`,
type: `element`,
},
{
type: `text`,
value: `?`,
},
],
properties: {},
tagName: `p`,
type: `element`,
},
],
data: { quirksMode: false },
type: `root`,
})
}
)
bootstrapTest(
`excerpt does have missing words and extra spaces`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
Where oh [*where*](nick.com) **_is_** ![that pony](pony.png)?`,
`excerpt
frontmatter {
title
}
`,
node => {
expect(node.excerpt).toBe(`Where oh where is that pony?`)
},
{}
)
bootstrapTest(
`excerpt does not have leading or trailing spaces`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
My pony likes space on the left and right! `,
`excerpt`,
node => {
expect(node.excerpt).toBe(`My pony likes space on the left and right!`)
},
{}
)
bootstrapTest(
`excerpt has spaces between paragraphs`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
My pony is little.
Little is my pony.`,
`excerpt`,
node => {
expect(node.excerpt).toBe(`My pony is little. Little is my pony.`)
},
{}
)
bootstrapTest(
`excerpt has spaces between headings`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
# Ponies: The Definitive Guide
# What time is it?
It's pony time.`,
`excerpt`,
node => {
expect(node.excerpt).toBe(
`Ponies: The Definitive Guide What time is it? It's pony time.`
)
},
{}
)
bootstrapTest(
`excerpt has spaces between table cells`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
| Pony | Owner |
| -------------- | -------- |
| My Little Pony | Me, Duh |`,
`excerpt`,
node => {
expect(node.excerpt).toBe(`Pony Owner My Little Pony Me, Duh`)
},
{}
)
bootstrapTest(
`excerpt converts linebreaks into spaces`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
If my pony ain't broke,${` `}
don't fix it.`,
// ^ Explicit syntax for trailing spaces to not get accidentally trimmed.
`excerpt`,
node => {
expect(node.excerpt).toBe(`If my pony ain't broke, don't fix it.`)
},
{}
)
bootstrapTest(
`excerpt does not have more than one space between elements`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
# Pony express
[some-link]: https://pony.my
Pony express had nothing on my little pony.`,
`excerpt`,
node => {
expect(node.excerpt).toBe(
`Pony express Pony express had nothing on my little pony.`
)
},
{}
)
bootstrapTest(
`given raw html in the text body, this html is not escaped`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
Where is my <code>pony</code> named leo?`,
`excerpt(format: HTML)
excerptAst
frontmatter {
title
}
`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt).toBe(
`<p>Where is my <code>pony</code> named leo?</p>`
)
expect(node.excerptAst).toMatchObject({
children: [
{
children: [
{
type: `text`,
value: `Where is my `,
},
{
children: [
{
type: `text`,
value: `pony`,
},
],
properties: {},
tagName: `code`,
type: `element`,
},
{
type: `text`,
value: ` named leo?`,
},
],
properties: {},
tagName: `p`,
type: `element`,
},
],
data: { quirksMode: false },
type: `root`,
})
},
{ pluginOptions: { excerpt_separator: `<!-- end -->` } }
)
bootstrapTest(
`given an html format, it prunes large excerpts`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
Where oh where is that pony? Is he in the stable or down by the stream?`,
`excerpt(format: HTML, pruneLength: 50)
excerptAst(pruneLength: 50)
frontmatter {
title
}
`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt).toBe(
`<p>Where oh where is that pony? Is he in the stable…</p>`
)
expect(node.excerptAst).toMatchObject({
children: [
{
children: [
{
type: `text`,
value: `Where oh where is that pony? Is he in the stable…`,
},
],
properties: {},
tagName: `p`,
type: `element`,
},
],
data: { quirksMode: false },
type: `root`,
})
}
)
bootstrapTest(
`given an html format, it respects the excerpt_separator`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
Where oh where is that *pony*? Is he in the stable or by the stream?
<!-- end -->
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi auctor sit amet velit id facilisis. Nulla viverra, eros at efficitur pulvinar, lectus orci accumsan nisi, eu blandit elit nulla nec lectus. Integer porttitor imperdiet sapien. Quisque in orci sed nisi consequat aliquam. Aenean id mollis nisi. Sed auctor odio id erat facilisis venenatis. Quisque posuere faucibus libero vel fringilla.
`,
`excerpt(format: HTML, pruneLength: 50)
excerptAst(pruneLength: 50)
frontmatter {
title
}
`,
node => {
expect(node).toMatchSnapshot()
expect(node.excerpt).toBe(
`<p>Where oh where is that <em>pony</em>? Is he in the stable or by the stream?</p>\n`
)
expect(node.excerptAst).toMatchObject({
children: [
{
children: [
{
type: `text`,
value: `Where oh where is that `,
},
{
children: [
{
type: `text`,
value: `pony`,
},
],
properties: {},
tagName: `em`,
type: `element`,
},
{
type: `text`,
value: `? Is he in the stable or by the stream?`,
},
],
properties: {},
tagName: `p`,
type: `element`,
},
{
type: `text`,
value: `\n`,
},
],
data: { quirksMode: false },
type: `root`,
})
},
{ pluginOptions: { excerpt_separator: `<!-- end -->` } }
)
})
describe(`Wordcount and timeToRead are generated correctly from schema`, () => {
bootstrapTest(
`correctly uses wordCount parameters`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
Where oh where is my little pony? Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi auctor sit amet velit id facilisis. Nulla viverra, eros at efficitur pulvinar, lectus orci accumsan nisi, eu blandit elit nulla nec lectus. Integer porttitor imperdiet sapien. Quisque in orci sed nisi consequat aliquam. Aenean id mollis nisi. Sed auctor odio id erat facilisis venenatis. Quisque posuere faucibus libero vel fringilla.
In quis lectus sed eros efficitur luctus. Morbi tempor, nisl eget feugiat tincidunt, sem velit vulputate enim, nec interdum augue enim nec mauris. Nulla iaculis ante sed enim placerat pretium. Nulla metus odio, facilisis vestibulum lobortis vitae, bibendum at nunc. Donec sit amet efficitur metus, in bibendum nisi. Vivamus tempus vel turpis sit amet auctor. Maecenas luctus vestibulum velit, at sagittis leo volutpat quis. Praesent posuere nec augue eget sodales. Pellentesque vitae arcu ut est varius venenatis id maximus sem. Curabitur non consectetur turpis.
`,
`wordCount {
words
paragraphs
sentences
}
frontmatter {
title
}`,
node => {
expect(node).toMatchSnapshot()
expect(node.wordCount).toEqual({
paragraphs: 2,
sentences: 19,
words: 150,
})
}
)
bootstrapTest(
`correctly generate timeToRead for CJK`,
`React 使创建交互式 UI 变得轻而易举。为你应用的每一个状态设计简洁的视图,当数据改变时 React 能有效地更新并正确地渲染组件。
以声明式编写 UI,可以让你的代码更加可靠,且方便调试。
创建拥有各自状态的组件,再由这些组件构成更加复杂的 UI。
组件逻辑使用 JavaScript 编写而非模版,因此你可以轻松地在应用中传递数据,并使得状态与 DOM 分离。
宣言的な View React は、インタラクティブなユーザインターフェイスの作成にともなう苦痛を取り除きます。アプリケーションの各状態に対応するシンプルな View を設計するだけで、React はデータの変更を検知し、関連するコンポーネントだけを効率的に更新、描画します。 宣言的な View を用いてアプリケーションを構築することで、コードはより見通しが立ちやすく、デバッグのしやすいものになります。
コンポーネントベース 自分自身の状態を管理するカプセル化されたコンポーネントをまず作成し、これらを組み合わせることで複雑なユーザインターフェイスを構築します。 コンポーネントのロジックは、Template ではなく JavaScript そのもので書くことができるので、様々なデータをアプリケーション内で簡単に取り回すことができ、かつ DOM に状態を持たせないようにすることができます。
`,
`timeToRead`,
node => {
expect(node).toMatchSnapshot()
expect(node.timeToRead).toEqual(1)
}
)
const content = `---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
`
bootstrapTest(
`correctly uses a default value for wordCount`,
content,
`wordCount {
words
paragraphs
sentences
}
frontmatter {
title
}`,
node => {
expect(node).toMatchSnapshot()
expect(node.wordCount).toEqual({
paragraphs: null,
sentences: null,
words: null,
})
}
)
bootstrapTest(
`correctly uses a default value for timeToRead`,
content,
`timeToRead
frontmatter {
title
}`,
node => {
expect(node).toMatchSnapshot()
expect(node.timeToRead).toBe(1)
}
)
})
describe(`Table of contents is generated correctly from schema`, () => {
bootstrapTest(
`returns null on non existing table of contents field`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
# first title
some text
## second title
some other text
`,
`tableOfContents(absolute: true, pathToSlugField: "fields.slug")
frontmatter {
title
}`,
node => {
expect(node).toMatchSnapshot()
expect(console.warn).toBeCalled()
}
)
bootstrapTest(
`correctly generates table of contents`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
# first title
some text
## second title
some other text
# third title
final text
`,
`tableOfContents(pathToSlugField: "frontmatter.title", absolute: true)
frontmatter {
title
}`,
node => {
expect(node).toMatchSnapshot()
}
)
bootstrapTest(
`correctly generates table of contents in relative path`,
`---
title: "my little pony"
date: "2017-09-18T23:19:51.246Z"
---
# first title
some text
## second title
some other text
# third title
final text
`,
`tableOfContents
frontmatter {
title
}`,
node => {
expect(node).toMatchSnapshot()