MCPcopy
hub / github.com/markdoc/markdoc / handleToken

Function handleToken

src/parser.ts:99–197  ·  view source on GitHub ↗
(
  token: Token,
  nodes: Node[],
  file?: string,
  handleSlots?: boolean,
  addLocation?: boolean,
  inlineParent?: Node
)

Source from the content-addressed store, hash-verified

97}
98
99function 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 = {

Callers 1

parserFunction · 0.85

Calls 3

annotateFunction · 0.85
handleAttrsFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…