-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
index.js
952 lines (769 loc) · 27.8 KB
/
index.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
'use strict';
require('chai').should();
const { encodeURL, escapeHTML, url_for } = require('hexo-util');
const Hexo = require('hexo');
const { join } = require('path').posix;
const { sep } = require('path');
describe('Marked renderer', () => {
const hexo = new Hexo(__dirname, {silent: true});
const defaultCfg = JSON.parse(JSON.stringify(Object.assign(hexo.config, {
marked: {}
})));
before(async () => {
hexo.config.permalink = ':title';
await hexo.init();
});
beforeEach(() => {
hexo.config = JSON.parse(JSON.stringify(defaultCfg));
});
const r = require('../lib/renderer').bind(hexo);
it('default', () => {
const code = 'console.log("Hello world");';
const body = [
'# Hello world',
'',
'```',
code,
'```',
'',
'## Hello world',
'',
'hello'
].join('\n');
const result = r({text: body});
result.should.eql([
'<h1 id="Hello-world"><a href="#Hello-world" class="headerlink" title="Hello world"></a>Hello world</h1>',
'<pre><code>' + escapeHTML(code) + '\n</code></pre>\n',
'<h2 id="Hello-world-1"><a href="#Hello-world-1" class="headerlink" title="Hello world"></a>Hello world</h2>',
'<p>hello</p>'
].join('') + '\n');
});
it('should render headings with links', () => {
const body = [
'## [hexo-server]',
'',
'[hexo-server]: https://github.com/hexojs/hexo-server'
].join('\n');
const result = r({text: body});
result.should.eql([
'<h2 id="hexo-server"><a href="#hexo-server" class="headerlink" title="hexo-server"></a>',
'<a href="https://github.com/hexojs/hexo-server">hexo-server</a></h2>'
].join(''));
});
it('should render headings with links - parentheses', () => {
const body = '## [hexo-server](https://github.com/hexojs/hexo-server)';
const result = r({text: body});
result.should.eql([
'<h2 id="hexo-server"><a href="#hexo-server" class="headerlink" title="hexo-server"></a>',
'<a href="https://github.com/hexojs/hexo-server">hexo-server</a></h2>'
].join(''));
});
describe('anchorAlias', () => {
beforeEach(() => { hexo.config.marked.anchorAlias = true; });
it('default', () => {
const body = '## [foo](#alias)';
const result = r({text: body});
result.should.eql('<h2 id="alias"><a href="#alias" class="headerlink" title="foo"></a><a href="#alias">foo</a></h2>');
});
it('duplicate anchors', () => {
const body = [
'## [foo](#alias)',
'## [bar](#alias)'
].join('\n');
const result = r({text: body});
result.should.eql([
'<h2 id="alias"><a href="#alias" class="headerlink" title="foo"></a><a href="#alias">foo</a></h2>',
'<h2 id="alias-1"><a href="#alias-1" class="headerlink" title="bar"></a><a href="#alias-1">bar</a></h2>'
].join(''));
});
it('modifyAnchors', () => {
hexo.config.marked.modifyAnchors = 2;
const body = '## [foo](#alias)';
const result = r({text: body});
result.should.eql('<h2 id="ALIAS"><a href="#ALIAS" class="headerlink" title="foo"></a><a href="#ALIAS">foo</a></h2>');
});
});
it('should handle duplicate headings properly', () => {
const body = [
'## foo',
'## foo'
].join('\n');
const result = r({text: body});
result.should.eql([
'<h2 id="foo"><a href="#foo" class="headerlink" title="foo"></a>foo</h2>',
'<h2 id="foo-1"><a href="#foo-1" class="headerlink" title="foo"></a>foo</h2>'
].join(''));
});
it('should handle chinese headers properly', () => {
const body = '# 中文';
const result = r({text: body});
result.should.eql('<h1 id="中文"><a href="#中文" class="headerlink" title="中文"></a>中文</h1>');
});
it('should render headings without headerIds when disabled', () => {
const body = '## hexo-server';
hexo.config.marked.headerIds = false;
const result = r({text: body});
result.should.eql([
'<h2>hexo-server</h2>'
].join(''));
});
// Description List tests
it('shouldn\'t render description lists when options.descriptionLists is disabled', () => {
hexo.config.marked.descriptionLists = false;
const result = r({text: 'Description Term<br>: This is the Description'});
result.should.eql('<p>Description Term<br>: This is the Description</p>\n');
});
it('should render description lists with a single space after the colon', () => {
const result = r({text: 'Description Term<br>: This is the Description'});
result.should.eql('<dl><dt>Description Term</dt><dd>This is the Description</dd></dl>');
});
it('should render description lists with multiple spaces after the colon', () => {
const result = r({text: 'Description Term<br>: This is the Description'});
result.should.eql('<dl><dt>Description Term</dt><dd>This is the Description</dd></dl>');
});
it('should render description lists with a tab after the colon', () => {
const result = r({text: 'Description Term<br>: This is the Description'});
result.should.eql('<dl><dt>Description Term</dt><dd>This is the Description</dd></dl>');
});
it('should render description lists with a carriage return after the colon', () => {
const result = r({text: 'Description Term<br>:\nThis is the Description'});
result.should.eql('<dl><dt>Description Term</dt><dd>This is the Description</dd></dl>');
});
it('should not render regular paragraphs as description lists', () => {
const result = r({text: 'Description Term<br>:This is the Description'});
result.should.eql('<p>Description Term<br>:This is the Description</p>\n');
});
it('should encode URL properly', () => {
const urlA = '/foo/bár.jpg';
const urlB = 'http://fóo.com/bar.jpg';
const body = [
`[foo](${urlA})`,
`[bar](${urlB})`
].join('\n');
const result = r({text: body});
result.should.eql([
`<p><a href="${encodeURL(urlA)}">foo</a>`,
`<a href="${encodeURL(urlB)}">bar</a></p>\n`
].join('\n'));
});
it('shouldn\'t encode when not a valid URL', () => {
const url = 'http://localhost:4000你好';
const body = `[foo](${url})`;
const result = r({text: body});
result.should.eql(`<p><a href="${url}">foo</a></p>\n`);
});
describe('quotes', () => {
beforeEach(() => {
hexo.config.marked.smartypants = true;
});
it('default', () => {
const body = '"foo" \'bar\'';
const quotes = '«»“”';
hexo.config.marked.quotes = quotes;
const result = r({text: body});
result.should.eql('<p>«foo» “bar”</p>\n');
});
it('invalid option', () => {
const body = '"foo" \'bar\'';
const quotes = '«»';
hexo.config.marked.quotes = quotes;
const result = r({text: body});
result.should.eql('<p>“foo” ‘bar’</p>\n');
});
it('smartypants disabled', () => {
const body = '"foo" \'bar\'';
const quotes = '«»“”';
hexo.config.marked = { quotes, smartypants: false };
const result = r({text: body});
result.should.eql(`<p>${escapeHTML(body)}</p>\n`);
});
it('should render other markdown syntax - quotes disabled', () => {
const body = '"[\'foo\'](bar)"\n["foo"](bar)\n ## "foo"\n!["joe"](bar)\n"foo---bar"';
const result = r({text: body});
const expected = [
'<p>',
'“<a href="bar">‘foo’</a>“\n',
'<a href="bar">“foo”</a></p>\n',
'<h2 id="“foo”"><a href="#“foo”" class="headerlink" title="“foo”"></a>“foo”</h2>',
'<p>',
'<img src="bar" alt=""joe"">\n',
'“foo—bar”',
'</p>\n'
].join('');
result.should.eql(expected);
});
it('should render other markdown syntax', () => {
const body = '"[\'foo\'](bar)"\n["foo"](bar)\n ## "foo"\n!["joe"](bar)\n"foo---bar"';
const quotes = '«»“”';
hexo.config.marked.quotes = quotes;
const result = r({text: body});
const expected = [
'<p>',
'«<a href="bar">“foo”</a>«\n',
'<a href="bar">«foo»</a></p>\n',
'<h2 id="«foo»"><a href="#«foo»" class="headerlink" title="«foo»"></a>«foo»</h2>',
'<p>',
'<img src="bar" alt=""joe"">\n',
'«foo—bar»',
'</p>\n'
].join('');
result.should.eql(expected);
});
it('inRawBlock - quotes disabled', () => {
const body = '<kbd class="foo">ctrl</kbd>"bar"';
const result = r({text: body});
result.should.eql('<p><kbd class="foo">ctrl</kbd>“bar”</p>\n');
});
it('inRawBlock - quotes disabled + wrapped', () => {
const body = '"<kbd class="foo">ctrl</kbd>"';
const result = r({text: body});
result.should.eql('<p>“<kbd class="foo">ctrl</kbd>“</p>\n');
});
it('inRawBlock ', () => {
const quotes = '«»“”';
hexo.config.marked.quotes = quotes;
const body = '<kbd class="foo">ctrl</kbd>"bar"';
const result = r({text: body});
result.should.eql('<p><kbd class="foo">ctrl</kbd>«bar»</p>\n');
});
it('inRawBlock - wrapped', () => {
const quotes = '«»“”';
hexo.config.marked.quotes = quotes;
const body = '"<kbd class="foo">ctrl</kbd>"';
const result = r({text: body});
result.should.eql('<p>«<kbd class="foo">ctrl</kbd>«</p>\n');
});
});
describe('autolink option tests', () => {
beforeEach(() => { hexo.config.marked.autolink = true; });
const body = [
'Great website http://hexo.io',
'',
'A webpage www.example.com',
'',
'[Hexo](http://hexo.io)',
'',
'[http://lorem.com/foo/](http://lorem.com/foo/)',
'',
'<http://dolor.com>'
].join('\n');
it('autolink enabled', () => {
const result = r({text: body});
result.should.eql([
'<p>Great website <a href="http://hexo.io/">http://hexo.io</a></p>',
'<p>A webpage <a href="http://www.example.com/">www.example.com</a></p>',
'<p><a href="http://hexo.io/">Hexo</a></p>',
'<p><a href="http://lorem.com/foo/">http://lorem.com/foo/</a></p>',
'<p><a href="http://dolor.com/">http://dolor.com</a></p>'
].join('\n') + '\n');
});
it('autolink disabled', () => {
hexo.config.marked.autolink = false;
const result = r({text: body});
result.should.eql([
'<p>Great website http://hexo.io</p>',
'<p>A webpage www.example.com</p>',
'<p><a href="http://hexo.io/">Hexo</a></p>',
'<p><a href="http://lorem.com/foo/">http://lorem.com/foo/</a></p>',
'<p><a href="http://dolor.com/">http://dolor.com</a></p>'
].join('\n') + '\n');
});
});
describe('mangle', () => {
const body = 'Contact: hi@example.com';
const expected = '<p>Contact: <a href="mailto:hi@example.com">hi@example.com</a></p>\n';
// https://stackoverflow.com/a/39243641
const unescape = str => {
return str.replace(/&([^;]+);/g, (entity, entityCode) => {
const hex = entityCode.match(/^#x([\da-fA-F]+)$/);
const digit = entityCode.match(/^#(\d+)$/);
if (hex) {
return String.fromCharCode(parseInt(hex[1], 16));
} else if (digit) {
return String.fromCharCode(~~digit[1]);
}
return entity;
});
};
// mangle option only applies to autolinked email address
beforeEach(() => { hexo.config.marked.autolink = true; });
it('default', () => {
const result = r({text: body});
result.should.include('&#');
unescape(result).should.eql(expected);
});
it('disabled', () => {
hexo.config.marked.mangle = false;
const result = r({text: body});
result.should.eql(expected);
});
});
it('should render link with title', () => {
const body = [
'[text](http://link.com/ "a-title")',
'[a<b](http://link.com/ "b>a")'
].join('\n');
const result = r({ text: body });
result.should.eql([
'<p><a href="http://link.com/" title="a-title">text</a>',
'<a href="http://link.com/" title="b>a">a<b</a></p>\n'
].join('\n'));
});
describe('sanitizeUrl option tests', () => {
const body = [
'[script](javascript:foo)',
'',
'[Hexo](http://hexo.io)'
].join('\n');
it('sanitizeUrl enabled', () => {
hexo.config.marked.sanitizeUrl = true;
const result = r({text: body});
result.should.eql([
'<p><a href="">script</a></p>\n',
'<p><a href="http://hexo.io/">Hexo</a></p>\n'
].join(''));
});
it('sanitizeUrl disabled', () => {
hexo.config.marked.sanitizeUrl = false;
const result = r({text: body});
result.should.eql([
'<p><a href="javascript:foo">script</a></p>\n',
'<p><a href="http://hexo.io/">Hexo</a></p>\n'
].join(''));
});
});
describe('modifyAnchors option tests', () => {
const body = [
'- [Example](#example)',
'',
'# Example'
].join('\n');
const renderer = require('../lib/renderer');
const hexo = new Hexo(__dirname, {silent: true});
const ctx = Object.assign(hexo, {
config: {
marked: {
modifyAnchors: 0
}
}
});
it('should not modify anchors with default options', () => {
const r = renderer.bind(ctx);
const result = r({text: body});
result.should.eql([
'<ul>',
'<li><a href="#example">Example</a></li>',
'</ul>',
'<h1 id="Example"><a href="#Example" class="headerlink" title="Example"></a>Example</h1>'
].join('\n'));
});
it('should set anchors to upperCase in case of modifyAnchors option is 2', () => {
ctx.config.marked.modifyAnchors = 2;
const r = renderer.bind(ctx);
const result = r({text: body});
result.should.eql([
'<ul>',
'<li><a href="#example">Example</a></li>',
'</ul>',
'<h1 id="EXAMPLE"><a href="#EXAMPLE" class="headerlink" title="Example"></a>Example</h1>'
].join('\n'));
});
it('should set anchors to lowerCase in case of modifyAnchors option is 1', () => {
ctx.config.marked.modifyAnchors = 1;
const r = renderer.bind(ctx);
const result = r({text: body});
result.should.eql([
'<ul>',
'<li><a href="#example">Example</a></li>',
'</ul>',
'<h1 id="example"><a href="#example" class="headerlink" title="Example"></a>Example</h1>'
].join('\n'));
});
});
describe('prependRoot option tests', () => {
const body = [
'![](/bar/baz.jpg)',
'![foo](/aaa/bbb.jpg)'
].join('\n');
const renderer = require('../lib/renderer');
const hexo = new Hexo(__dirname, {silent: true});
const ctx = Object.assign(hexo, {
config: {
marked: {
prependRoot: false
},
url: 'http://example.com',
root: '/blog/',
relative_link: false
}
});
afterEach(() => {
ctx.config = {
marked: {
prependRoot: false
},
url: 'http://example.com',
root: '/blog/',
relative_link: false
};
});
it('should not modify image path with default option', () => {
const r = renderer.bind(ctx);
const result = r({text: body});
result.should.eql([
'<p><img src="/bar/baz.jpg">',
'<img src="/aaa/bbb.jpg" alt="foo"></p>\n'
].join('\n'));
});
it('should not modify image path when enable relative_link', () => {
ctx.config.relative_link = true;
const r = renderer.bind(ctx);
const result = r({text: body});
result.should.eql([
'<p><img src="/bar/baz.jpg">',
'<img src="/aaa/bbb.jpg" alt="foo"></p>\n'
].join('\n'));
});
it('should prepend image path with root', () => {
ctx.config.marked.prependRoot = true;
const r = renderer.bind(ctx);
const result = r({text: body});
result.should.eql([
'<p><img src="/blog/bar/baz.jpg">',
'<img src="/blog/aaa/bbb.jpg" alt="foo"></p>\n'
].join('\n'));
});
});
describe('external_link', () => {
const hexo = new Hexo(__dirname, {silent: true});
const ctx = Object.assign(hexo, {
config: {
marked: {
external_link: {
enable: false
}
},
url: 'http://example.com'
}
});
const r = require('../lib/renderer').bind(ctx);
it('disable', () => {
const body = '[foo](http://bar.com/)';
const result = r({text: body});
result.should.eql('<p><a href="http://bar.com/">foo</a></p>\n');
});
it('enable', () => {
ctx.config.marked.external_link.enable = true;
const body = [
'[foo](http://bar.com/)',
'[text](http://example.com/)',
'[baz](/foo/bar)'
].join('\n');
const result = r({text: body});
result.should.eql([
'<p><a href="http://bar.com/" target="_blank" rel="noopener">foo</a>',
'<a href="http://example.com/">text</a>',
'<a href="/foo/bar">baz</a></p>\n'
].join('\n'));
});
it('exclude - string', () => {
ctx.config.marked.external_link.exclude = 'bar.com';
const body = [
'[foo](http://foo.com/)',
'[bar](http://bar.com/)',
'[baz](http://baz.com/)'
].join('\n');
const result = r({text: body});
result.should.eql([
'<p><a href="http://foo.com/" target="_blank" rel="noopener">foo</a>',
'<a href="http://bar.com/">bar</a>',
'<a href="http://baz.com/" target="_blank" rel="noopener">baz</a></p>\n'
].join('\n'));
});
it('exclude - array', () => {
ctx.config.marked.external_link.exclude = ['bar.com', 'baz.com'];
const body = [
'[foo](http://foo.com/)',
'[bar](http://bar.com/)',
'[baz](http://baz.com/)'
].join('\n');
const result = r({text: body});
result.should.eql([
'<p><a href="http://foo.com/" target="_blank" rel="noopener">foo</a>',
'<a href="http://bar.com/">bar</a>',
'<a href="http://baz.com/">baz</a></p>\n'
].join('\n'));
});
});
describe('nofollow', () => {
const hexo = new Hexo(__dirname, {silent: true});
const ctx = Object.assign(hexo, {
config: {
marked: {
external_link: {
enable: false,
exclude: [],
nofollow: false
}
},
url: 'http://example.com'
}
});
const r = require('../lib/renderer').bind(ctx);
afterEach(() => {
ctx.config = {
marked: {
external_link: {
enable: false,
exclude: [],
nofollow: false
}
},
url: 'http://example.com'
};
});
const body = [
'[foo](http://foo.com/)',
'[bar](http://bar.com/)',
'[baz](http://baz.com/)',
'[internal](http://example.com/)',
'[relative](/foo/bar)'
].join('\n');
it('disable', () => {
const result = r({text: body});
result.should.eql([
'<p><a href="http://foo.com/">foo</a>',
'<a href="http://bar.com/">bar</a>',
'<a href="http://baz.com/">baz</a>',
'<a href="http://example.com/">internal</a>',
'<a href="/foo/bar">relative</a></p>\n'
].join('\n'));
});
it('enable', () => {
ctx.config.marked.external_link.nofollow = true;
const result = r({text: body});
result.should.eql([
'<p><a href="http://foo.com/" rel="noopener external nofollow noreferrer">foo</a>',
'<a href="http://bar.com/" rel="noopener external nofollow noreferrer">bar</a>',
'<a href="http://baz.com/" rel="noopener external nofollow noreferrer">baz</a>',
'<a href="http://example.com/">internal</a>',
'<a href="/foo/bar">relative</a></p>\n'
].join('\n'));
});
it('exclude - string', () => {
ctx.config.marked.external_link.nofollow = true;
ctx.config.marked.external_link.exclude = 'bar.com';
const result = r({text: body});
result.should.eql([
'<p><a href="http://foo.com/" rel="noopener external nofollow noreferrer">foo</a>',
'<a href="http://bar.com/">bar</a>',
'<a href="http://baz.com/" rel="noopener external nofollow noreferrer">baz</a>',
'<a href="http://example.com/">internal</a>',
'<a href="/foo/bar">relative</a></p>\n'
].join('\n'));
});
it('exclude - array', () => {
ctx.config.marked.external_link.nofollow = true;
ctx.config.marked.external_link.exclude = ['bar.com', 'baz.com'];
const result = r({text: body});
result.should.eql([
'<p><a href="http://foo.com/" rel="noopener external nofollow noreferrer">foo</a>',
'<a href="http://bar.com/">bar</a>',
'<a href="http://baz.com/">baz</a>',
'<a href="http://example.com/">internal</a>',
'<a href="/foo/bar">relative</a></p>\n'
].join('\n'));
});
it('nofollow + external_link', () => {
ctx.config.marked.external_link.nofollow = true;
ctx.config.marked.external_link.enable = true;
const result = r({text: body});
result.should.eql([
'<p><a href="http://foo.com/" target="_blank" rel="noopener external nofollow noreferrer">foo</a>',
'<a href="http://bar.com/" target="_blank" rel="noopener external nofollow noreferrer">bar</a>',
'<a href="http://baz.com/" target="_blank" rel="noopener external nofollow noreferrer">baz</a>',
'<a href="http://example.com/">internal</a>',
'<a href="/foo/bar">relative</a></p>\n'
].join('\n'));
});
});
it('should encode image url', () => {
const urlA = '/foo/bár.jpg';
const urlB = 'http://fóo.com/bar.jpg';
const body = [
`![](${urlA})`,
`![](${urlB})`
].join('\n');
const r = require('../lib/renderer').bind(hexo);
const result = r({text: body});
result.should.eql([
`<p><img src="${encodeURL(urlA)}">`,
`<img src="${encodeURL(urlB)}"></p>\n`
].join('\n'));
});
it('should include image caption & title', () => {
const body = [
'![caption](http://foo.com/a.jpg)',
'![caption](http://bar.com/b.jpg "a-title")',
'![a"b](http://bar.com/b.jpg "c>d")'
].join('\n');
const r = require('../lib/renderer').bind(hexo);
const result = r({text: body});
result.should.eql([
'<p><img src="http://foo.com/a.jpg" alt="caption">',
'<img src="http://bar.com/b.jpg" alt="caption" title="a-title">',
'<img src="http://bar.com/b.jpg" alt="a"b" title="c>d"></p>\n'
].join('\n'));
});
it('lazyload image', () => {
const body = [
'![](/bar/baz.jpg)',
'![foo](/aaa/bbb.jpg)'
].join('\n');
hexo.config.marked.lazyload = true;
const r = require('../lib/renderer').bind(hexo);
const result = r({ text: body });
result.should.eql([
'<p><img src="/bar/baz.jpg" loading="lazy">',
'<img src="/aaa/bbb.jpg" alt="foo" loading="lazy"></p>\n'
].join('\n'));
});
describe('postAsset', () => {
const Post = hexo.model('Post');
const PostAsset = hexo.model('PostAsset');
beforeEach(() => {
hexo.config.post_asset_folder = true;
hexo.config.marked = {
prependRoot: true,
postAsset: true
};
});
it('default', async () => {
const asset = 'img/bar.svg';
const slug = asset.replace(/\//g, sep);
const content = `![](${asset})`;
const post = await Post.insert({
source: '_posts/foo.md',
slug: 'foo'
});
const postasset = await PostAsset.insert({
_id: `source/_posts/foo/${asset}`,
slug,
post: post._id
});
const expected = url_for.call(hexo, join(post.path, asset));
const result = r({ text: content, path: post.full_source });
result.should.eql(`<p><img src="${expected}"></p>\n`);
// should not be Windows path
expected.includes('\\').should.eql(false);
await PostAsset.removeById(postasset._id);
await Post.removeById(post._id);
});
it('should not modify non-post asset', async () => {
const asset = 'bar.svg';
const siteasset = '/logo/brand.png';
const site = 'http://lorem.ipsum/dolor/huri.bun';
const content = `![](${asset})\n![](${siteasset})\n![](${site})`;
const post = await Post.insert({
source: '_posts/foo.md',
slug: 'foo'
});
const postasset = await PostAsset.insert({
_id: `source/_posts/foo/${asset}`,
slug: asset,
post: post._id
});
const result = r({ text: content, path: post.full_source });
result.should.eql([
`<p><img src="${url_for.call(hexo, join(post.path, asset))}">`,
`<img src="${siteasset}">`,
`<img src="${site}"></p>`
].join('\n') + '\n');
await PostAsset.removeById(postasset._id);
await Post.removeById(post._id);
});
// #170
it('post located in subfolder', async () => {
const asset = 'img/bar.svg';
const slug = asset.replace(/\//g, sep);
const content = `![](${asset})`;
const post = await Post.insert({
source: '_posts/lorem/foo.md',
slug: 'foo'
});
const postasset = await PostAsset.insert({
_id: `source/_posts/lorem/foo/${asset}`,
slug,
post: post._id
});
const expected = url_for.call(hexo, join(post.path, asset));
const result = r({ text: content, path: post.full_source });
result.should.eql(`<p><img src="${expected}"></p>\n`);
await PostAsset.removeById(postasset._id);
await Post.removeById(post._id);
});
});
describe('exec filter to extend', () => {
const hexo = new Hexo(__dirname, {silent: true});
hexo.config.marked = {};
it('should execute filter registered to marked:renderer', () => {
hexo.extend.filter.register('marked:renderer', renderer => {
renderer.image = function(href, title, text) {
return `<img data-src="${encodeURL(href)}">`;
};
});
const urlA = '/foo/bár.jpg';
const urlB = 'http://fóo.com/bar.jpg';
const body = [
`![](${urlA})`,
`![](${urlB})`
].join('\n');
const r = require('../lib/renderer').bind(hexo);
const result = r({text: body});
result.should.eql([
`<p><img data-src="${encodeURL(urlA)}">`,
`<img data-src="${encodeURL(urlB)}"></p>\n`
].join('\n'));
});
it('should execute filter registered to marked:renderer', () => {
const smartypants = str => {
return str.replace(/---/g, '\u2014');
};
hexo.extend.filter.register('marked:tokenizer', tokenizer => {
tokenizer.inlineText = function(src) {
return {
type: 'text',
raw: src,
text: smartypants(src)
};
};
});
const body = '"---"';
const r = require('../lib/renderer').bind(hexo);
const result = r({text: body});
result.should.eql(`<p>${smartypants(body)}</p>\n`);
});
});
describe('nunjucks', () => {
const hexo = new Hexo(__dirname, { silent: true });
const loremFn = () => { return 'ipsum'; };
const engine = 'md';
before(async () => {
await hexo.init();
hexo.extend.tag.register('lorem', loremFn);
hexo.extend.renderer.register('md', 'html', require('../lib/renderer'), true);
});
beforeEach(() => { hexo.config.marked = {}; });
it('default', async () => {
const result = await hexo.post.render(null, { content: '**foo** {% lorem %}', engine });
result.content.should.eql('<p><strong>foo</strong> ipsum</p>\n');
});
it('enable disableNunjucks', async () => {
const renderer = hexo.render.renderer.get('md');
renderer.disableNunjucks = true;
hexo.extend.renderer.register('md', 'html', renderer, true);
const result = await hexo.post.render(null, { content: '**foo** {% lorem %}', engine });
result.content.should.eql('<p><strong>foo</strong> {% lorem %}</p>\n');
});
});
});