Commit e336e6404f9feff7276310fc4aa97f0367f55d9a

niblo 2019-11-04T15:20:59

Add support for Wiki links (#92) With a new flag MD_FLAG_WIKILINKS, recoginize wiki-style links as [[foo]] and [[foo|bar]]. Update also the HTML renderer accordingly, to output a custom HTML tag <x-wikilink> when seeing it.

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
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a5104e5..c874e2d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,9 +14,16 @@ Changes:
    follows the table has to be delimited with a blank line.
 
  * With `MD_FLAG_LATEXMATHSPANS`, LaTeX math spans (`$...$`) and LaTeX display
-   math spans (`$$...$$`) are recognized. (Note though that the HTML renderer
-   outputs them verbatim.) Thanks for the feature belong to [Tilman Roeder](
-   https://github.com/dyedgreen).
+   math spans (`$$...$$`) are now recognized. (Note though that the HTML
+   renderer outputs them verbatim in a custom `<x-equation>` tag.)
+
+   Contributed by [Tilman Roeder](https://github.com/dyedgreen).
+
+ * With `MD_FLAG_WIKILINKS`, Wiki-style links (`[[...]]`) are now recognized.
+   (Note htough that the HTML renderer renders them as a custom `<x-wikilink>`
+   tag.)
+
+   Contributed by [Nils Blomqvist](https://github.com/niblo).
 
 Fixes:
 
diff --git a/md2html/md2html.c b/md2html/md2html.c
index a8e47a5..ce79943 100644
--- a/md2html/md2html.c
+++ b/md2html/md2html.c
@@ -209,6 +209,7 @@ static const option cmdline_options[] = {
     { "ftables",                     0,  'T', OPTION_ARG_NONE },
     { "fstrikethrough",              0,  'S', OPTION_ARG_NONE },
     { "flatex-math",                 0,  'L', OPTION_ARG_NONE },
+    { "fwiki-links",                 0,  'K', OPTION_ARG_NONE },
     { "ftasklists",                  0,  'X', OPTION_ARG_NONE },
     { 0 }
 };
@@ -258,6 +259,7 @@ usage(void)
         "      --ftables        Enable tables\n"
         "      --fstrikethrough Enable strikethrough spans\n"
         "      --flatex-math    Enable LaTeX style mathematics spans (e.g. $a+b=c$ becomes <equation>a+b=c</equation>)\n"
+        "      --fwiki-links    Enable wiki links\n"
         "      --ftasklists     Enable task lists\n"
     );
 }
@@ -307,6 +309,7 @@ cmdline_callback(int opt, char const* value, void* data)
         case 'T':   parser_flags |= MD_FLAG_TABLES; break;
         case 'S':   parser_flags |= MD_FLAG_STRIKETHROUGH; break;
         case 'L':   parser_flags |= MD_FLAG_LATEXMATHSPANS; break;
+        case 'K':   parser_flags |= MD_FLAG_WIKILINKS; break;
         case 'X':   parser_flags |= MD_FLAG_TASKLISTS; break;
 
         default:
diff --git a/md2html/render_html.c b/md2html/render_html.c
index 7f7e780..fcfb100 100644
--- a/md2html/render_html.c
+++ b/md2html/render_html.c
@@ -353,6 +353,15 @@ render_close_img_span(MD_RENDER_HTML* r, const MD_SPAN_IMG_DETAIL* det)
     r->image_nesting_level--;
 }
 
+static void
+render_open_wikilink_span(MD_RENDER_HTML* r, const MD_SPAN_WIKILINK_DETAIL* det)
+{
+    RENDER_LITERAL(r, "<x-wikilink data-target=\"");
+    render_attribute(r, &det->target, render_html_escaped);
+
+    RENDER_LITERAL(r, "\">");
+}
+
 
 /**************************************
  ***  HTML renderer implementation  ***
@@ -434,6 +443,7 @@ enter_span_callback(MD_SPANTYPE type, void* detail, void* userdata)
         case MD_SPAN_DEL:               RENDER_LITERAL(r, "<del>"); break;
         case MD_SPAN_LATEXMATH:         RENDER_LITERAL(r, "<x-equation>"); break;
         case MD_SPAN_LATEXMATH_DISPLAY: RENDER_LITERAL(r, "<x-equation type=\"display\">"); break;
+        case MD_SPAN_WIKILINK:          render_open_wikilink_span(r, (MD_SPAN_WIKILINK_DETAIL*) detail); break;
     }
 
     return 0;
@@ -461,6 +471,7 @@ leave_span_callback(MD_SPANTYPE type, void* detail, void* userdata)
         case MD_SPAN_DEL:               RENDER_LITERAL(r, "</del>"); break;
         case MD_SPAN_LATEXMATH:         /*fall through*/
         case MD_SPAN_LATEXMATH_DISPLAY: RENDER_LITERAL(r, "</x-equation>"); break;
+        case MD_SPAN_WIKILINK:          RENDER_LITERAL(r, "</x-wikilink>"); break;
     }
 
     return 0;
diff --git a/md4c/md4c.c b/md4c/md4c.c
index ecd9656..27164b9 100644
--- a/md4c/md4c.c
+++ b/md4c/md4c.c
@@ -204,12 +204,14 @@ struct MD_LINE_ANALYSIS_tag {
     OFF beg;
     OFF end;
     unsigned indent;        /* Indentation level. */
+    unsigned total_indent;  /* Total indent in characters. */
 };
 
 typedef struct MD_LINE_tag MD_LINE;
 struct MD_LINE_tag {
     OFF beg;
     OFF end;
+    unsigned total_indent;  /* Total indent in characters. */
 };
 
 typedef struct MD_VERBATIMLINE_tag MD_VERBATIMLINE;
@@ -2696,7 +2698,7 @@ md_build_mark_char_map(MD_CTX* ctx)
     if(ctx->parser.flags & MD_FLAG_PERMISSIVEWWWAUTOLINKS)
         ctx->mark_char_map['.'] = 1;
 
-    if(ctx->parser.flags & MD_FLAG_TABLES)
+    if((ctx->parser.flags & MD_FLAG_TABLES) || (ctx->parser.flags & MD_FLAG_WIKILINKS))
         ctx->mark_char_map['|'] = 1;
 
     if(ctx->parser.flags & MD_FLAG_COLLAPSEWHITESPACE) {
@@ -3236,8 +3238,8 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int table_mode)
                 continue;
             }
 
-            /* A potential table cell boundary. */
-            if(table_mode  &&  ch == _T('|')) {
+            /* A potential table cell boundary or wiki link label delimiter. */
+            if((table_mode || ctx->parser.flags & MD_FLAG_WIKILINKS) && ch == _T('|')) {
                 PUSH_MARK(ch, off, off+1, 0);
                 off++;
                 continue;
@@ -3398,6 +3400,92 @@ md_resolve_links(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
             continue;
         }
 
+        /* Detect and resolve wiki links. */
+        if ((ctx->parser.flags & MD_FLAG_WIKILINKS) &&
+            next_opener != NULL && next_closer != NULL &&
+            (opener->end - opener->beg == 1) &&
+            (next_opener->beg == opener->beg - 1) &&
+            (next_closer->beg == closer->beg + 1) &&
+            (next_opener->end - next_opener->beg == 1) &&
+            (next_closer->end - next_closer->beg == 1) &&
+            (next_opener->ch == '[' && next_closer->ch == ']'))
+        {
+            is_link = TRUE;
+
+            if (opener->end == closer->beg)
+                is_link = FALSE;
+
+            int delim_index = opener_index;
+            MD_MARK* delim = &ctx->marks[delim_index];
+
+            /* To prevent runaway O(n^2) performance, don't look too far for the delimiter (.. < 100). */
+            while(is_link && delim_index < closer_index && (delim_index - opener_index) < 100 ) {
+                if(delim->ch == '|' && delim->beg == opener->end) {
+                    is_link = FALSE;
+                } else if(delim->ch == '|' && delim->end == closer->beg) {
+                    break;
+                } else if(delim->ch == '|') {
+                    opener->end = delim->beg;
+                    break;
+                }
+                delim_index++;
+                delim = &ctx->marks[delim_index];
+            }
+
+            OFF off = closer->beg-1;
+            int count = 0;
+            int has_label = (opener->end - opener->beg > 2);
+            const MD_LINE* line;
+            int line_index = n_lines-1;
+
+            /* An image inside the link target disables the wiki link. */
+            if( (has_label && last_img_beg >= opener->beg && last_img_end <= opener->end) ||
+                (!has_label && last_img_beg >= opener->beg && last_img_end <= closer->end))
+                is_link = FALSE;
+
+            while(is_link && off > opener->beg && count++ < 100) {
+
+                /* Newline not allowed in link target. */
+                if(has_label && (off <= opener->end) && ISNEWLINE(off))
+                    is_link = FALSE;
+                else if(!has_label && off > opener->end && ISNEWLINE(off))
+                    is_link = FALSE;
+                else if(ISNEWLINE(off)) {
+                    line = &lines[line_index--];
+                    count = count - line->total_indent - 1;  /* Count newline too. */
+                }
+
+                off--;
+            }
+
+            if(off > opener->beg)
+                is_link = FALSE;
+
+            if(is_link) {
+                if(delim->ch == '|')
+                    delim->flags |= MD_MARK_RESOLVED;
+
+                opener->beg = next_opener->beg;
+                closer->end = next_closer->end;
+
+                opener->next = closer_index;
+                opener->flags |= MD_MARK_OPENER | MD_MARK_RESOLVED;
+                closer->prev = opener_index;
+                closer->flags |= MD_MARK_CLOSER | MD_MARK_RESOLVED;
+
+                last_link_beg = opener->beg;
+                last_link_end = closer->end;
+
+                if ((opener->end - opener->beg > 2))
+                    md_analyze_link_contents(ctx, lines, n_lines, opener_index+1, closer_index);
+
+                opener_index = next_index;
+                continue;
+            }
+
+        }
+
+
         if(next_opener != NULL  &&  next_opener->beg == closer->end) {
             if(next_closer->beg > closer->end + 1) {
                 /* Might be full reference link. */
@@ -3860,8 +3948,16 @@ md_analyze_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int table_mod
     /* (1) Entities; code spans; autolinks; raw HTML. */
     md_analyze_marks(ctx, lines, n_lines, 0, ctx->n_marks, _T("&"));
 
+    /* (2) Links. */
+    md_analyze_marks(ctx, lines, n_lines, 0, ctx->n_marks, _T("[]!"));
+    MD_CHECK(md_resolve_links(ctx, lines, n_lines));
+    BRACKET_OPENERS.head = -1;
+    BRACKET_OPENERS.tail = -1;
+    ctx->unresolved_link_head = -1;
+    ctx->unresolved_link_tail = -1;
+
     if(table_mode) {
-        /* (2) Analyze table cell boundaries.
+        /* (3) Analyze table cell boundaries.
          * Note we reset TABLECELLBOUNDARIES chain prior to the call md_analyze_marks(),
          * not after, because caller may need it. */
         MD_ASSERT(n_lines == 1);
@@ -3872,14 +3968,6 @@ md_analyze_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int table_mod
         return ret;
     }
 
-    /* (3) Links. */
-    md_analyze_marks(ctx, lines, n_lines, 0, ctx->n_marks, _T("[]!"));
-    MD_CHECK(md_resolve_links(ctx, lines, n_lines));
-    BRACKET_OPENERS.head = -1;
-    BRACKET_OPENERS.tail = -1;
-    ctx->unresolved_link_head = -1;
-    ctx->unresolved_link_tail = -1;
-
     /* (4) Emphasis and strong emphasis; permissive autolinks. */
     md_analyze_link_contents(ctx, lines, n_lines, 0, ctx->n_marks);
 
@@ -3941,6 +4029,27 @@ abort:
     return ret;
 }
 
+static int
+md_enter_leave_span_wikilink(MD_CTX* ctx, int enter, const CHAR* target, SZ target_size)
+{
+    MD_ATTRIBUTE_BUILD target_build = { 0 };
+    MD_SPAN_WIKILINK_DETAIL det;
+    int ret = 0;
+
+    memset(&det, 0, sizeof(MD_SPAN_WIKILINK_DETAIL));
+    MD_CHECK(md_build_attribute(ctx, target, target_size, 0, &det.target, &target_build));
+
+    if (enter)
+        MD_ENTER_SPAN(MD_SPAN_WIKILINK, &det);
+    else
+        MD_LEAVE_SPAN(MD_SPAN_WIKILINK, &det);
+
+abort:
+    md_free_attribute(ctx, &target_build);
+    return ret;
+}
+
+
 /* Render the output, accordingly to the analyzed ctx->marks. */
 static int
 md_process_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
@@ -4036,11 +4145,35 @@ md_process_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
                     }
                     break;
 
-                case '[':       /* Link, image. */
+                case '[':       /* Link, wiki link, image. */
                 case '!':
                 case ']':
                 {
                     const MD_MARK* opener = (mark->ch != ']' ? mark : &ctx->marks[mark->prev]);
+                    const MD_MARK* closer = &ctx->marks[opener->next];
+
+                    if ((opener->ch == '[' && closer->ch == ']') &&
+                        opener->end - opener->beg >= 2 &&
+                        closer->end - closer->beg == 2)
+                    {
+                        const MD_MARK* delim = opener+3;  /* Scan past the two dummy marks. */
+                        int has_label = (opener->end - opener->beg > 2);
+                        int target_sz;
+
+                        if(has_label)
+                            target_sz = opener->end - (opener->beg+2);
+                        else if(delim->ch == '|')
+                            target_sz = (closer->beg-1) - opener->end;
+                        else
+                            target_sz = closer->beg - opener->end;
+
+                        MD_CHECK(md_enter_leave_span_wikilink(ctx, (mark->ch != ']'),
+                                 has_label ? STR(opener->beg+2) : STR(opener->end),
+                                 target_sz));
+
+                        break;
+                    }
+
                     const MD_MARK* dest_mark = opener+1;
                     const MD_MARK* title_mark = opener+2;
 
@@ -4862,6 +4995,7 @@ md_add_line_into_current_block(MD_CTX* ctx, const MD_LINE_ANALYSIS* analysis)
 
         line->beg = analysis->beg;
         line->end = analysis->end;
+        line->total_indent = analysis->total_indent;
     }
     ctx->current_block->n_lines++;
 
@@ -5529,6 +5663,7 @@ md_analyze_line(MD_CTX* ctx, OFF beg, OFF* p_end,
                 line->indent--;
 
             line->beg = off;
+
         } else if(c->ch != _T('>')  &&  line->indent >= c->contents_indent) {
             /* List. */
             line->indent -= c->contents_indent;
@@ -5865,6 +6000,8 @@ md_analyze_line(MD_CTX* ctx, OFF beg, OFF* p_end,
         break;
     }
 
+    line->total_indent = total_indent;
+
     /* Scan for end of the line.
      *
      * Note this is quite a bottleneck of the parsing as we here iterate almost
diff --git a/md4c/md4c.h b/md4c/md4c.h
index 6d9fce5..0f8e806 100644
--- a/md4c/md4c.h
+++ b/md4c/md4c.h
@@ -135,7 +135,12 @@ typedef enum MD_SPANTYPE {
      * Note: Recognized only when MD_FLAG_LATEXMATHSPANS is enabled.
      */
     MD_SPAN_LATEXMATH,
-    MD_SPAN_LATEXMATH_DISPLAY
+    MD_SPAN_LATEXMATH_DISPLAY,
+
+    /* Wiki links
+     * Note: Recognized only when MD_FLAG_WIKILINKS is enabled.
+     */
+    MD_SPAN_WIKILINK
 } MD_SPANTYPE;
 
 /* Text is the actual textual contents of span. */
@@ -268,6 +273,10 @@ typedef struct MD_SPAN_IMG_DETAIL {
     MD_ATTRIBUTE title;
 } MD_SPAN_IMG_DETAIL;
 
+/* Detailed info for MD_SPAN_WIKILINK. */
+typedef struct MD_SPAN_WIKILINK {
+    MD_ATTRIBUTE target;
+} MD_SPAN_WIKILINK_DETAIL;
 
 /* Flags specifying extensions/deviations from CommonMark specification.
  *
@@ -286,6 +295,7 @@ typedef struct MD_SPAN_IMG_DETAIL {
 #define MD_FLAG_PERMISSIVEWWWAUTOLINKS      0x0400  /* Enable WWW autolinks (even without any scheme prefix, if they begin with 'www.') */
 #define MD_FLAG_TASKLISTS                   0x0800  /* Enable task list extension. */
 #define MD_FLAG_LATEXMATHSPANS              0x1000  /* Enable $ and $$ containing LaTeX equations. */
+#define MD_FLAG_WIKILINKS                   0x2000  /* Enable wiki links extension. */
 
 #define MD_FLAG_PERMISSIVEAUTOLINKS         (MD_FLAG_PERMISSIVEEMAILAUTOLINKS | MD_FLAG_PERMISSIVEURLAUTOLINKS | MD_FLAG_PERMISSIVEWWWAUTOLINKS)
 #define MD_FLAG_NOHTML                      (MD_FLAG_NOHTMLBLOCKS | MD_FLAG_NOHTMLSPANS)
diff --git a/scripts/run-tests.sh b/scripts/run-tests.sh
index 88394f8..7e6fe73 100755
--- a/scripts/run-tests.sh
+++ b/scripts/run-tests.sh
@@ -63,5 +63,9 @@ echo "LaTeX extension:"
 $PYTHON "$TEST_DIR/spec_tests.py" -s "$TEST_DIR/latex-math.txt" -p "$PROGRAM --flatex-math"
 
 echo
+echo "Wiki links extension:"
+$PYTHON "$TEST_DIR/spec_tests.py" -s "$TEST_DIR/wiki-links.txt" -p "$PROGRAM --fwiki-links --ftables"
+
+echo
 echo "Pathological input:"
 $PYTHON "$TEST_DIR/pathological_tests.py" -p "$PROGRAM"
diff --git a/test/wiki-links.txt b/test/wiki-links.txt
new file mode 100644
index 0000000..3210354
--- /dev/null
+++ b/test/wiki-links.txt
@@ -0,0 +1,254 @@
+
+# Wiki Links
+
+With the flag `MD_FLAG_WIKILINKS`, MD4C recognizes wiki links.
+
+A wiki link is ...
+
+```````````````````````````````` example
+[[foo]]
+.
+<p><x-wikilink data-target="foo">foo</x-wikilink></p>
+````````````````````````````````
+
+A wiki link cannot be empty.
+
+```````````````````````````````` example
+[[]]
+.
+<p>[[]]</p>
+````````````````````````````````
+
+The link target of a wiki link cannot span more than one line:
+
+```````````````````````````````` example
+[[foo
+bar]]
+.
+<p>[[foo
+bar]]</p>
+````````````````````````````````
+
+The link target is rendered verbatim; inline markup is not recognized.
+
+```````````````````````````````` example
+[[*foo*]]
+.
+<p><x-wikilink data-target="*foo*">*foo*</x-wikilink></p>
+````````````````````````````````
+
+Wiki links can have a label that is delimited by a `|`.
+
+```````````````````````````````` example
+[[foo|bar]]
+.
+<p><x-wikilink data-target="foo">bar</x-wikilink></p>
+````````````````````````````````
+
+A wiki link with a label but without an id is not a wiki link.
+
+```````````````````````````````` example
+[[|foo]]
+.
+<p>[[|foo]]</p>
+````````````````````````````````
+
+With multiple `|` delimiters, only the first is recognized.
+
+```````````````````````````````` example
+[[foo|bar|baz]]
+.
+<p><x-wikilink data-target="foo">bar|baz</x-wikilink></p>
+````````````````````````````````
+
+The delimiter `|` can be escaped with `/`.
+
+```````````````````````````````` example
+[[foo\|bar|baz]]
+.
+<p><x-wikilink data-target="foo|bar">baz</x-wikilink></p>
+````````````````````````````````
+
+The label can contain inline elements.
+
+```````````````````````````````` example
+[[foo|*bar*]]
+.
+<p><x-wikilink data-target="foo"><em>bar</em></x-wikilink></p>
+````````````````````````````````
+
+The label can contain at most one linebreak.
+
+```````````````````````````````` example
+[[foo|*bar
+baz*]]
+.
+<p><x-wikilink data-target="foo"><em>bar
+baz</em></x-wikilink></p>
+````````````````````````````````
+
+A wiki link with an empty label is considered invalid.
+
+```````````````````````````````` example
+[[foo|]]
+.
+<p><x-wikilink data-target="foo">foo</x-wikilink></p>
+````````````````````````````````
+
+The label can span multiple lines.
+
+```````````````````````````````` example
+[[foo|foo
+bar
+baz]]
+.
+<p><x-wikilink data-target="foo">foo
+bar
+baz</x-wikilink></p>
+````````````````````````````````
+
+Wiki links are prioritized over regular links.
+
+```````````````````````````````` example
+[[foo]](foo.jpg)
+.
+<p><x-wikilink data-target="foo">foo</x-wikilink>(foo.jpg)</p>
+````````````````````````````````
+
+Wiki links can be inlined in tables.
+
+```````````````````````````````` example
+| A                | B   |
+|------------------|-----|
+| [[foo|*bar*]]    | baz |
+.
+<table>
+<thead>
+<tr>
+<th>A</th>
+<th>B</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td><x-wikilink data-target="foo"><em>bar</em></x-wikilink></td>
+<td>baz</td>
+</tr>
+</tbody>
+</table>
+````````````````````````````````
+
+Wiki links not prioritzed over images.
+
+```````````````````````````````` example
+![[foo]](foo.jpg)
+.
+<p><img src="foo.jpg" alt="[foo]"></p>
+````````````````````````````````
+
+An image placed inside a wiki link target disables the wiki link.
+
+```````````````````````````````` example
+[[![foo](foo.jpg)]]
+.
+<p>[[<img src="foo.jpg" alt="foo">]]</p>
+
+````````````````````````````````
+
+```````````````````````````````` example
+[[![foo](foo.jpg)|test]]
+.
+<p>[[<img src="foo.jpg" alt="foo">|test]]</p>
+````````````````````````````````
+
+But images can be placed in the wiki link label.
+
+```````````````````````````````` example
+[[foo|![bar](bar.jpg)]]
+.
+<p><x-wikilink data-target="foo"><img src="bar.jpg" alt="bar"></x-wikilink></p>
+````````````````````````````````
+
+Something that may look like a wiki link at first, but turns out not to be,
+is recognized as a normal link.
+
+```````````````````````````````` example
+[[foo]
+
+[foo]: /url
+.
+<p>[<a href="/url">foo</a></p>
+````````````````````````````````
+
+Escaping the opening `[`, we should get a normal link.
+
+```````````````````````````````` example
+\[[foo]]
+
+[foo]: /url
+.
+<p>[<a href="/url">foo</a>]</p>
+````````````````````````````````
+
+Like normal links, the innermost wiki link is preferred.
+
+```````````````````````````````` example
+[[foo[[bar]]]]
+.
+<p>[[foo<x-wikilink data-target="bar">bar</x-wikilink>]]</p>
+````````````````````````````````
+
+
+There is limit of 100 characters inside a wiki link target (or id); 101
+characters is too much.
+
+```````````````````````````````` example
+[[12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901]]
+.
+<p>[[12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901]]</p>
+````````````````````````````````
+
+100 characters inside a wiki link target works.
+
+```````````````````````````````` example
+[[1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890]]
+.
+<p><x-wikilink data-target="1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890">1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890</x-wikilink></p>
+````````````````````````````````
+
+If a label is included, the limit is 99 characters in total, excluding the `|`
+delimiter.
+
+```````````````````````````````` example
+[[12345678901234567890123456789012345678901234567890|1234567890123456789012345678901234567890123456789]]
+.
+<p><x-wikilink data-target="12345678901234567890123456789012345678901234567890">1234567890123456789012345678901234567890123456789</x-wikilink></p>
+````````````````````````````````
+
+This example shows that 101 characters (or 100 exclusive the delimiter) is one
+too many.
+
+```````````````````````````````` example
+[[12345678901234567890123456789012345678901234567890|12345678901234567890123456789012345678901234567890]]
+.
+<p>[[12345678901234567890123456789012345678901234567890|12345678901234567890123456789012345678901234567890]]</p>
+````````````````````````````````
+
+The limit on link content does not include any characters belonging to a block
+quote, if the label spans multiple lines contained in a block quote.
+
+```````````````````````````````` example
+> [[12345678901234567890123456789012345678901234567890|1234567890
+> 1234567890
+> 1234567890
+> 1234567890
+> 123456789]]
+.
+<blockquote>
+<p><x-wikilink data-target="12345678901234567890123456789012345678901234567890">1234567890
+1234567890
+1234567890
+1234567890
+123456789</x-wikilink></p>
+</blockquote>
+````````````````````````````````