( token: Token, nodes: Node[], file?: string, handleSlots?: boolean, addLocation?: boolean, inlineParent?: Node )
| 97 | } |
| 98 | |
| 99 | function handleToken( |
| 100 | token: Token, |
| 101 | nodes: Node[], |
| 102 | file?: string, |
| 103 | handleSlots?: boolean, |
| 104 | addLocation?: boolean, |
| 105 | inlineParent?: Node |
| 106 | ) { |
| 107 | if (token.type === 'frontmatter') { |
| 108 | nodes[0].attributes.frontmatter = token.content; |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | if (token.hidden || (token.type === 'text' && token.content === '')) return; |
| 113 | |
| 114 | const errors = token.errors || []; |
| 115 | const parent = nodes[nodes.length - 1]; |
| 116 | const { tag, attributes, error } = token.meta || {}; |
| 117 | |
| 118 | if (token.type === 'annotation') { |
| 119 | if (inlineParent) return annotate(inlineParent, attributes); |
| 120 | |
| 121 | return parent.errors.push({ |
| 122 | id: 'no-inline-annotations', |
| 123 | level: 'error', |
| 124 | message: `Can't apply inline annotations to '${parent.type}'`, |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | let typeName = token.type.replace(/_(open|close)$/, ''); |
| 129 | if (mappings[typeName]) typeName = mappings[typeName]; |
| 130 | |
| 131 | if (typeName === 'error') { |
| 132 | const { message, location } = error; |
| 133 | errors.push({ id: 'parse-error', level: 'critical', message, location }); |
| 134 | } |
| 135 | |
| 136 | if (token.nesting < 0) { |
| 137 | if (parent.type === typeName && parent.tag === tag) { |
| 138 | if (parent.lines && token.map) parent.lines.push(...token.map); |
| 139 | return nodes.pop(); |
| 140 | } |
| 141 | |
| 142 | errors.push({ |
| 143 | id: 'missing-opening', |
| 144 | level: 'critical', |
| 145 | message: `Node '${typeName}' is missing opening`, |
| 146 | }); |
| 147 | } |
| 148 | |
| 149 | const attrs = handleAttrs(token, typeName); |
| 150 | const node = new Node(typeName, attrs, undefined, tag || undefined); |
| 151 | const { position = {} } = token; |
| 152 | |
| 153 | node.errors = errors; |
| 154 | if (addLocation !== false) { |
| 155 | node.lines = token.map || parent.lines || []; |
| 156 | node.location = { |
no test coverage detected
searching dependent graphs…