MCPcopy Create free account
hub / github.com/Noumena-Network/code / parseToSpansUncached

Function parseToSpansUncached

src/ink/Ansi.tsx:157–192  ·  view source on GitHub ↗
(input: string)

Source from the content-addressed store, hash-verified

155}
156
157function parseToSpansUncached(input: string): Span[] {
158 const parser = new Parser();
159 const actions = parser.feed(input);
160 const spans: Span[] = [];
161 let currentHyperlink: string | undefined;
162 for (const action of actions) {
163 if (action.type === 'link') {
164 if (action.action.type === 'start') {
165 currentHyperlink = action.action.url;
166 } else {
167 currentHyperlink = undefined;
168 }
169 continue;
170 }
171 if (action.type === 'text') {
172 const text = action.graphemes.map(g => g.value).join('');
173 if (!text) continue;
174 const props = textStyleToSpanProps(action.style);
175 if (currentHyperlink) {
176 props.hyperlink = currentHyperlink;
177 }
178
179 // Try to merge with previous span if props match
180 const lastSpan = spans[spans.length - 1];
181 if (lastSpan && propsEqual(lastSpan.props, props)) {
182 lastSpan.text += text;
183 } else {
184 spans.push({
185 text,
186 props
187 });
188 }
189 }
190 }
191 return spans;
192}
193
194/**
195 * Convert termio's TextStyle to SpanProps.

Callers 1

parseToSpansFunction · 0.85

Calls 3

feedMethod · 0.95
textStyleToSpanPropsFunction · 0.85
propsEqualFunction · 0.85

Tested by

no test coverage detected