1 | const whitespace = require('is-whitespace-character')
|
2 |
|
3 | const C_PIPE = '|'
|
4 | const DOUBLE = '||'
|
5 |
|
6 | function locator (value, fromIndex) {
|
7 | const index = value.indexOf(DOUBLE, fromIndex)
|
8 | return index
|
9 | }
|
10 |
|
11 | function plugin () {
|
12 | function inlineTokenizer (eat, value, silent) {
|
13 | if (
|
14 | !this.options.gfm ||
|
15 | value.charAt(0) !== C_PIPE ||
|
16 | value.charAt(1) !== C_PIPE ||
|
17 | value.startsWith(C_PIPE.repeat(4)) ||
|
18 | whitespace(value.charAt(2))
|
19 | ) {
|
20 | return
|
21 | }
|
22 |
|
23 | let character = ''
|
24 | let previous = ''
|
25 | let preceding = ''
|
26 | let subvalue = ''
|
27 | let index = 1
|
28 | const length = value.length
|
29 | const now = eat.now()
|
30 | now.column += 2
|
31 | now.offset += 2
|
32 |
|
33 | while (++index < length) {
|
34 | character = value.charAt(index)
|
35 |
|
36 | if (
|
37 | character === C_PIPE &&
|
38 | previous === C_PIPE &&
|
39 | (!preceding || !whitespace(preceding))
|
40 | ) {
|
41 |
|
42 |
|
43 | if (silent) return true
|
44 |
|
45 | return eat(DOUBLE + subvalue + DOUBLE)({
|
46 | type: 'kbd',
|
47 | children: this.tokenizeInline(subvalue, now),
|
48 | data: {
|
49 | hName: 'kbd',
|
50 | },
|
51 | })
|
52 | }
|
53 |
|
54 | subvalue += previous
|
55 | preceding = previous
|
56 | previous = character
|
57 | }
|
58 | }
|
59 | inlineTokenizer.locator = locator
|
60 |
|
61 | const Parser = this.Parser
|
62 |
|
63 |
|
64 | const inlineTokenizers = Parser.prototype.inlineTokenizers
|
65 | const inlineMethods = Parser.prototype.inlineMethods
|
66 | inlineTokenizers.kbd = inlineTokenizer
|
67 | inlineMethods.splice(inlineMethods.indexOf('text'), 0, 'kbd')
|
68 |
|
69 | const Compiler = this.Compiler
|
70 |
|
71 |
|
72 | if (Compiler) {
|
73 | const visitors = Compiler.prototype.visitors
|
74 | visitors.kbd = function (node) {
|
75 | return `||${this.all(node).join('')}||`
|
76 | }
|
77 | }
|
78 | }
|
79 |
|
80 | module.exports = plugin
|