|
| 1 | +exports.canContainEols = ['mdxJsxTextElement'] |
| 2 | +exports.enter = { |
| 3 | + mdxJsxFlowTag: enterMdxJsxTag, |
| 4 | + mdxJsxFlowTagClosingMarker: enterMdxJsxTagClosingMarker, |
| 5 | + mdxJsxFlowTagAttribute: enterMdxJsxTagAttribute, |
| 6 | + mdxJsxFlowTagExpressionAttribute: enterMdxJsxTagExpressionAttribute, |
| 7 | + mdxJsxFlowTagAttributeValueLiteral: buffer, |
| 8 | + mdxJsxFlowTagAttributeValueExpression: buffer, |
| 9 | + mdxJsxFlowTagSelfClosingMarker: enterMdxJsxTagSelfClosingMarker, |
| 10 | + |
| 11 | + mdxJsxTextTag: enterMdxJsxTag, |
| 12 | + mdxJsxTextTagClosingMarker: enterMdxJsxTagClosingMarker, |
| 13 | + mdxJsxTextTagAttribute: enterMdxJsxTagAttribute, |
| 14 | + mdxJsxTextTagExpressionAttribute: enterMdxJsxTagExpressionAttribute, |
| 15 | + mdxJsxTextTagAttributeValueLiteral: buffer, |
| 16 | + mdxJsxTextTagAttributeValueExpression: buffer, |
| 17 | + mdxJsxTextTagSelfClosingMarker: enterMdxJsxTagSelfClosingMarker |
| 18 | +} |
| 19 | +exports.exit = { |
| 20 | + mdxJsxFlowTagClosingMarker: exitMdxJsxTagClosingMarker, |
| 21 | + mdxJsxFlowTagNamePrimary: exitMdxJsxTagNamePrimary, |
| 22 | + mdxJsxFlowTagNameMember: exitMdxJsxTagNameMember, |
| 23 | + mdxJsxFlowTagNameLocal: exitMdxJsxTagNameLocal, |
| 24 | + mdxJsxFlowTagExpressionAttribute: exitMdxJsxTagExpressionAttribute, |
| 25 | + mdxJsxFlowTagExpressionAttributeValue: data, |
| 26 | + mdxJsxFlowTagAttributeNamePrimary: exitMdxJsxTagAttributeNamePrimary, |
| 27 | + mdxJsxFlowTagAttributeNameLocal: exitMdxJsxTagAttributeNameLocal, |
| 28 | + mdxJsxFlowTagAttributeValueLiteral: exitMdxJsxTagAttributeValueLiteral, |
| 29 | + mdxJsxFlowTagAttributeValueLiteralValue: data, |
| 30 | + mdxJsxFlowTagAttributeValueExpression: exitMdxJsxTagAttributeValueExpression, |
| 31 | + mdxJsxFlowTagAttributeValueExpressionValue: data, |
| 32 | + mdxJsxFlowTagSelfClosingMarker: exitMdxJsxTagSelfClosingMarker, |
| 33 | + mdxJsxFlowTag: exitMdxJsxTag, |
| 34 | + |
| 35 | + mdxJsxTextTagClosingMarker: exitMdxJsxTagClosingMarker, |
| 36 | + mdxJsxTextTagNamePrimary: exitMdxJsxTagNamePrimary, |
| 37 | + mdxJsxTextTagNameMember: exitMdxJsxTagNameMember, |
| 38 | + mdxJsxTextTagNameLocal: exitMdxJsxTagNameLocal, |
| 39 | + mdxJsxTextTagExpressionAttribute: exitMdxJsxTagExpressionAttribute, |
| 40 | + mdxJsxTextTagExpressionAttributeValue: data, |
| 41 | + mdxJsxTextTagAttributeNamePrimary: exitMdxJsxTagAttributeNamePrimary, |
| 42 | + mdxJsxTextTagAttributeNameLocal: exitMdxJsxTagAttributeNameLocal, |
| 43 | + mdxJsxTextTagAttributeValueLiteral: exitMdxJsxTagAttributeValueLiteral, |
| 44 | + mdxJsxTextTagAttributeValueLiteralValue: data, |
| 45 | + mdxJsxTextTagAttributeValueExpression: exitMdxJsxTagAttributeValueExpression, |
| 46 | + mdxJsxTextTagAttributeValueExpressionValue: data, |
| 47 | + mdxJsxTextTagSelfClosingMarker: exitMdxJsxTagSelfClosingMarker, |
| 48 | + mdxJsxTextTag: exitMdxJsxTag |
| 49 | +} |
| 50 | + |
| 51 | +var parseEntities = require('parse-entities') |
| 52 | +var stringifyPosition = require('unist-util-stringify-position') |
| 53 | +var VMessage = require('vfile-message') |
| 54 | + |
| 55 | +function buffer() { |
| 56 | + this.buffer() |
| 57 | +} |
| 58 | + |
| 59 | +function data(token) { |
| 60 | + this.config.enter.data.call(this, token) |
| 61 | + this.config.exit.data.call(this, token) |
| 62 | +} |
| 63 | + |
| 64 | +function enterMdxJsxTag(token) { |
| 65 | + if (!this.getData('mdxJsxTagStack')) this.setData('mdxJsxTagStack', []) |
| 66 | + |
| 67 | + this.setData('mdxJsxTag', { |
| 68 | + name: null, |
| 69 | + attributes: [], |
| 70 | + start: token.start, |
| 71 | + end: token.end |
| 72 | + }) |
| 73 | + |
| 74 | + this.buffer() |
| 75 | +} |
| 76 | + |
| 77 | +function enterMdxJsxTagClosingMarker(token) { |
| 78 | + if (!this.getData('mdxJsxTagStack').length) { |
| 79 | + throw new VMessage( |
| 80 | + 'Unexpected closing slash `/` in tag, expected an open tag first', |
| 81 | + {start: token.start, end: token.end}, |
| 82 | + 'mdast-util-mdx-jsx:unexpected-closing-slash' |
| 83 | + ) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +function enterMdxJsxTagAnyAttribute(token) { |
| 88 | + if (this.getData('mdxJsxTag').close) { |
| 89 | + throw new VMessage( |
| 90 | + 'Unexpected attribute in closing tag, expected the end of the tag', |
| 91 | + {start: token.start, end: token.end}, |
| 92 | + 'mdast-util-mdx-jsx:unexpected-attribute' |
| 93 | + ) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +function enterMdxJsxTagSelfClosingMarker(token) { |
| 98 | + if (this.getData('mdxJsxTag').close) { |
| 99 | + throw new VMessage( |
| 100 | + 'Unexpected self-closing slash `/` in closing tag, expected the end of the tag', |
| 101 | + {start: token.start, end: token.end}, |
| 102 | + 'mdast-util-mdx-jsx:unexpected-self-closing-slash' |
| 103 | + ) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +function exitMdxJsxTagClosingMarker() { |
| 108 | + this.getData('mdxJsxTag').close = true |
| 109 | +} |
| 110 | + |
| 111 | +function exitMdxJsxTagNamePrimary(token) { |
| 112 | + this.getData('mdxJsxTag').name = this.sliceSerialize(token) |
| 113 | +} |
| 114 | + |
| 115 | +function exitMdxJsxTagNameMember(token) { |
| 116 | + this.getData('mdxJsxTag').name += '.' + this.sliceSerialize(token) |
| 117 | +} |
| 118 | + |
| 119 | +function exitMdxJsxTagNameLocal(token) { |
| 120 | + this.getData('mdxJsxTag').name += ':' + this.sliceSerialize(token) |
| 121 | +} |
| 122 | + |
| 123 | +function enterMdxJsxTagAttribute(token) { |
| 124 | + enterMdxJsxTagAnyAttribute.call(this, token) |
| 125 | + this.getData('mdxJsxTag').attributes.push({ |
| 126 | + type: 'mdxJsxAttribute', |
| 127 | + name: null, |
| 128 | + value: null |
| 129 | + }) |
| 130 | +} |
| 131 | + |
| 132 | +function enterMdxJsxTagExpressionAttribute(token) { |
| 133 | + enterMdxJsxTagAnyAttribute.call(this, token) |
| 134 | + this.getData('mdxJsxTag').attributes.push({ |
| 135 | + type: 'mdxJsxExpressionAttribute', |
| 136 | + value: null |
| 137 | + }) |
| 138 | + this.buffer() |
| 139 | +} |
| 140 | + |
| 141 | +function exitMdxJsxTagExpressionAttribute(token) { |
| 142 | + var attributes = this.getData('mdxJsxTag').attributes |
| 143 | + attributes[attributes.length - 1].value = this.resume() |
| 144 | + |
| 145 | + if (token.estree) { |
| 146 | + attributes[attributes.length - 1].data = {estree: token.estree} |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +function exitMdxJsxTagAttributeNamePrimary(token) { |
| 151 | + var attributes = this.getData('mdxJsxTag').attributes |
| 152 | + attributes[attributes.length - 1].name = this.sliceSerialize(token) |
| 153 | +} |
| 154 | + |
| 155 | +function exitMdxJsxTagAttributeNameLocal(token) { |
| 156 | + var attributes = this.getData('mdxJsxTag').attributes |
| 157 | + attributes[attributes.length - 1].name += ':' + this.sliceSerialize(token) |
| 158 | +} |
| 159 | + |
| 160 | +function exitMdxJsxTagAttributeValueLiteral() { |
| 161 | + var attributes = this.getData('mdxJsxTag').attributes |
| 162 | + attributes[attributes.length - 1].value = parseEntities(this.resume(), { |
| 163 | + nonTerminated: false |
| 164 | + }) |
| 165 | +} |
| 166 | + |
| 167 | +function exitMdxJsxTagAttributeValueExpression(token) { |
| 168 | + var attributes = this.getData('mdxJsxTag').attributes |
| 169 | + |
| 170 | + attributes[attributes.length - 1].value = { |
| 171 | + type: 'mdxJsxAttributeValueExpression', |
| 172 | + value: this.resume() |
| 173 | + } |
| 174 | + |
| 175 | + if (token.estree) { |
| 176 | + attributes[attributes.length - 1].value.data = {estree: token.estree} |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +function exitMdxJsxTagSelfClosingMarker() { |
| 181 | + this.getData('mdxJsxTag').selfClosing = true |
| 182 | +} |
| 183 | + |
| 184 | +function exitMdxJsxTag(token) { |
| 185 | + var tag = this.getData('mdxJsxTag') |
| 186 | + var stack = this.getData('mdxJsxTagStack') |
| 187 | + var tail = stack[stack.length - 1] |
| 188 | + |
| 189 | + if (tag.close && tail.name !== tag.name) { |
| 190 | + throw new VMessage( |
| 191 | + 'Unexpected closing tag `' + |
| 192 | + serializeAbbreviatedTag(tag) + |
| 193 | + '`, expected corresponding closing tag for `' + |
| 194 | + serializeAbbreviatedTag(tail) + |
| 195 | + '` (' + |
| 196 | + stringifyPosition(tail) + |
| 197 | + ')', |
| 198 | + {start: token.start, end: token.end}, |
| 199 | + 'mdast-util-mdx-jsx:end-tag-mismatch' |
| 200 | + ) |
| 201 | + } |
| 202 | + |
| 203 | + // End of a tag, so drop the buffer. |
| 204 | + this.resume() |
| 205 | + |
| 206 | + if (tag.close) { |
| 207 | + stack.pop() |
| 208 | + } else { |
| 209 | + this.enter( |
| 210 | + { |
| 211 | + type: |
| 212 | + token.type === 'mdxJsxTextTag' |
| 213 | + ? 'mdxJsxTextElement' |
| 214 | + : 'mdxJsxFlowElement', |
| 215 | + name: tag.name, |
| 216 | + attributes: tag.attributes, |
| 217 | + children: [] |
| 218 | + }, |
| 219 | + token |
| 220 | + ) |
| 221 | + } |
| 222 | + |
| 223 | + if (tag.selfClosing || tag.close) { |
| 224 | + this.exit(token) |
| 225 | + } else { |
| 226 | + stack.push(tag) |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +// Serialize a tag, excluding attributes. |
| 231 | +// `self-closing` is not supported, because we don’t need it yet. |
| 232 | +function serializeAbbreviatedTag(tag) { |
| 233 | + return '<' + (tag.close ? '/' : '') + (tag.name || '') + '>' |
| 234 | +} |
0 commit comments