UNPKG

1.76 kBJavaScriptView Raw
1"use strict";
2
3var whitespace = require('is-whitespace-character');
4
5var C_PIPE = '|';
6var DOUBLE = '||';
7
8function locator(value, fromIndex) {
9 var index = value.indexOf(DOUBLE, fromIndex);
10 return index;
11}
12
13function plugin() {
14 function inlineTokenizer(eat, value, silent) {
15 if (!this.options.gfm || value.substr(0, 2) !== DOUBLE || value.substr(0, 4) === DOUBLE + DOUBLE || whitespace(value.charAt(2))) {
16 return;
17 }
18
19 var character = '';
20 var previous = '';
21 var preceding = '';
22 var subvalue = '';
23 var index = 1;
24 var length = value.length;
25 var now = eat.now();
26 now.column += 2;
27 now.offset += 2;
28
29 while (++index < length) {
30 character = value.charAt(index);
31
32 if (character === C_PIPE && previous === C_PIPE && (!preceding || !whitespace(preceding))) {
33 /* istanbul ignore if - never used (yet) */
34 if (silent) return true;
35 return eat(DOUBLE + subvalue + DOUBLE)({
36 type: 'kbd',
37 children: this.tokenizeInline(subvalue, now),
38 data: {
39 hName: 'kbd'
40 }
41 });
42 }
43
44 subvalue += previous;
45 preceding = previous;
46 previous = character;
47 }
48 }
49
50 inlineTokenizer.locator = locator;
51 var Parser = this.Parser; // Inject inlineTokenizer
52
53 var inlineTokenizers = Parser.prototype.inlineTokenizers;
54 var inlineMethods = Parser.prototype.inlineMethods;
55 inlineTokenizers.kbd = inlineTokenizer;
56 inlineMethods.splice(inlineMethods.indexOf('text'), 0, 'kbd');
57 var Compiler = this.Compiler; // Stringify
58
59 if (Compiler) {
60 var visitors = Compiler.prototype.visitors;
61
62 visitors.kbd = function (node) {
63 return "||".concat(this.all(node).join(''), "||");
64 };
65 }
66}
67
68module.exports = plugin;
\No newline at end of file