MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / renderList

Method renderList

packages/pi-tui/src/components/markdown.ts:604–654  ·  view source on GitHub ↗

* Render a list with proper nesting support

(token: Tokens.List, depth: number, width: number, styleContext?: InlineStyleContext)

Source from the content-addressed store, hash-verified

602 * Render a list with proper nesting support
603 */
604 private renderList(token: Tokens.List, depth: number, width: number, styleContext?: InlineStyleContext): string[] {
605 const lines: string[] = [];
606 const indent = " ".repeat(depth);
607 // Use the list's start property (defaults to 1 for ordered lists)
608 const startNumber = typeof token.start === "number" ? token.start : 1;
609
610 for (let i = 0; i < token.items.length; i++) {
611 const item = token.items[i]!;
612 const isLastItem = i === token.items.length - 1;
613 const bullet = token.ordered
614 ? this.options.preserveOrderedListMarkers
615 ? (this.getOrderedListMarker(item) ?? `${startNumber + i}. `)
616 : `${startNumber + i}. `
617 : this.options.preserveOrderedListMarkers
618 ? (this.getUnorderedListMarker(item) ?? "- ")
619 : "- ";
620 const taskMarker = item.task ? `[${item.checked ? "x" : " "}] ` : "";
621 const marker = bullet + taskMarker;
622 const firstPrefix = indent + this.theme.listBullet(marker);
623 const continuationPrefix = indent + " ".repeat(visibleWidth(marker));
624 const itemWidth = Math.max(1, width - visibleWidth(firstPrefix));
625 let renderedAnyLine = false;
626
627 for (const itemToken of item.tokens) {
628 if (itemToken.type === "list") {
629 lines.push(...this.renderList(itemToken as Tokens.List, depth + 1, width, styleContext));
630 renderedAnyLine = true;
631 continue;
632 }
633
634 const itemLines = this.renderToken(itemToken, itemWidth, undefined, styleContext);
635 for (const line of itemLines) {
636 for (const wrappedLine of wrapTextWithAnsi(line, itemWidth)) {
637 const linePrefix = renderedAnyLine ? continuationPrefix : firstPrefix;
638 lines.push(linePrefix + wrappedLine);
639 renderedAnyLine = true;
640 }
641 }
642 }
643
644 if (!renderedAnyLine) {
645 lines.push(firstPrefix);
646 }
647
648 if (token.loose && !isLastItem) {
649 lines.push("");
650 }
651 }
652
653 return lines;
654 }
655
656 /**
657 * Get the visible width of the longest word in a string.

Callers 1

renderTokenMethod · 0.95

Calls 6

getOrderedListMarkerMethod · 0.95
renderTokenMethod · 0.95
visibleWidthFunction · 0.90
wrapTextWithAnsiFunction · 0.90
pushMethod · 0.45

Tested by

no test coverage detected