|
| 1 | +var decode = require('parse-entities/decode-entity') |
| 2 | + |
| 3 | +exports.canContainEols = ['textDirective'] |
| 4 | +exports.enter = { |
| 5 | + directiveContainer: enterContainer, |
| 6 | + directiveContainerAttributes: enterAttributes, |
| 7 | + directiveContainerLabel: enterContainerLabel, |
| 8 | + |
| 9 | + directiveLeaf: enterLeaf, |
| 10 | + directiveLeafAttributes: enterAttributes, |
| 11 | + |
| 12 | + directiveText: enterText, |
| 13 | + directiveTextAttributes: enterAttributes |
| 14 | +} |
| 15 | +exports.exit = { |
| 16 | + directiveContainer: exit, |
| 17 | + directiveContainerAttributeClassValue: exitAttributeClassValue, |
| 18 | + directiveContainerAttributeIdValue: exitAttributeIdValue, |
| 19 | + directiveContainerAttributeName: exitAttributeName, |
| 20 | + directiveContainerAttributeValue: exitAttributeValue, |
| 21 | + directiveContainerAttributes: exitAttributes, |
| 22 | + directiveContainerLabel: exitContainerLabel, |
| 23 | + directiveContainerName: exitName, |
| 24 | + |
| 25 | + directiveLeaf: exit, |
| 26 | + directiveLeafAttributeClassValue: exitAttributeClassValue, |
| 27 | + directiveLeafAttributeIdValue: exitAttributeIdValue, |
| 28 | + directiveLeafAttributeName: exitAttributeName, |
| 29 | + directiveLeafAttributeValue: exitAttributeValue, |
| 30 | + directiveLeafAttributes: exitAttributes, |
| 31 | + directiveLeafName: exitName, |
| 32 | + |
| 33 | + directiveText: exit, |
| 34 | + directiveTextAttributeClassValue: exitAttributeClassValue, |
| 35 | + directiveTextAttributeIdValue: exitAttributeIdValue, |
| 36 | + directiveTextAttributeName: exitAttributeName, |
| 37 | + directiveTextAttributeValue: exitAttributeValue, |
| 38 | + directiveTextAttributes: exitAttributes, |
| 39 | + directiveTextName: exitName |
| 40 | +} |
| 41 | + |
| 42 | +function enterContainer(token) { |
| 43 | + enter.call(this, 'containerDirective', token) |
| 44 | +} |
| 45 | + |
| 46 | +function enterLeaf(token) { |
| 47 | + enter.call(this, 'leafDirective', token) |
| 48 | +} |
| 49 | + |
| 50 | +function enterText(token) { |
| 51 | + enter.call(this, 'textDirective', token) |
| 52 | +} |
| 53 | + |
| 54 | +function enter(type, token) { |
| 55 | + this.enter({type: type, name: '', attributes: {}, children: []}, token) |
| 56 | +} |
| 57 | + |
| 58 | +function exitName(token) { |
| 59 | + this.stack[this.stack.length - 1].name = this.sliceSerialize(token) |
| 60 | +} |
| 61 | + |
| 62 | +function enterContainerLabel(token) { |
| 63 | + this.enter( |
| 64 | + {type: 'paragraph', data: {directiveLabel: true}, children: []}, |
| 65 | + token |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +function exitContainerLabel(token) { |
| 70 | + this.exit(token) |
| 71 | +} |
| 72 | + |
| 73 | +function enterAttributes() { |
| 74 | + this.setData('directiveAttributes', []) |
| 75 | + this.buffer() // Capture EOLs |
| 76 | +} |
| 77 | + |
| 78 | +function exitAttributeIdValue(token) { |
| 79 | + this.getData('directiveAttributes').push([ |
| 80 | + 'id', |
| 81 | + decodeLight(this.sliceSerialize(token)) |
| 82 | + ]) |
| 83 | +} |
| 84 | + |
| 85 | +function exitAttributeClassValue(token) { |
| 86 | + this.getData('directiveAttributes').push([ |
| 87 | + 'class', |
| 88 | + decodeLight(this.sliceSerialize(token)) |
| 89 | + ]) |
| 90 | +} |
| 91 | + |
| 92 | +function exitAttributeValue(token) { |
| 93 | + var attributes = this.getData('directiveAttributes') |
| 94 | + attributes[attributes.length - 1][1] = decodeLight(this.sliceSerialize(token)) |
| 95 | +} |
| 96 | + |
| 97 | +function exitAttributeName(token) { |
| 98 | + // Attribute names in CommonMark are significantly limited, so character |
| 99 | + // references can’t exist. |
| 100 | + this.getData('directiveAttributes').push([this.sliceSerialize(token), '']) |
| 101 | +} |
| 102 | + |
| 103 | +function exitAttributes() { |
| 104 | + var attributes = this.getData('directiveAttributes') |
| 105 | + var cleaned = {} |
| 106 | + var index = -1 |
| 107 | + var attribute |
| 108 | + |
| 109 | + while (++index < attributes.length) { |
| 110 | + attribute = attributes[index] |
| 111 | + |
| 112 | + if (attribute[0] === 'class' && cleaned.class) { |
| 113 | + cleaned.class += ' ' + attribute[1] |
| 114 | + } else { |
| 115 | + cleaned[attribute[0]] = attribute[1] |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + this.setData('directiveAttributes') |
| 120 | + this.resume() // Drop EOLs |
| 121 | + this.stack[this.stack.length - 1].attributes = cleaned |
| 122 | +} |
| 123 | + |
| 124 | +function exit(token) { |
| 125 | + this.exit(token) |
| 126 | +} |
| 127 | + |
| 128 | +function decodeLight(value) { |
| 129 | + return value.replace( |
| 130 | + /&(#(\d{1,7}|x[\da-f]{1,6})|[\da-z]{1,31});/gi, |
| 131 | + decodeIfPossible |
| 132 | + ) |
| 133 | +} |
| 134 | + |
| 135 | +function decodeIfPossible($0, $1) { |
| 136 | + return decode($1) || $0 |
| 137 | +} |
0 commit comments