UNPKG

llm-md

Version:

Convert JSON to Markdown optimized for LLM consumption

54 lines (53 loc) 1.57 kB
"use strict"; /** * List Converter - converts mixed arrays to numbered lists */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ListConverter = void 0; class ListConverter { /** * Convert an array to a numbered list * @param data Data to convert * @param options Conversion options * @returns Markdown numbered list */ convert(data, _options) { if (!Array.isArray(data)) { return this.formatValue(data); } return data .map((item, index) => { const prefix = `${index + 1}. `; if (item === null || item === undefined) { return `${prefix}\`${String(item)}\``; } if (typeof item === 'object') { // For objects/arrays, stringify and wrap in code block return `${prefix}\`${JSON.stringify(item)}\``; } if (typeof item === 'string') { return `${prefix}\`"${item}"\``; } return `${prefix}\`${item}\``; }) .join('\n'); } /** * Format a single value * @param value Value to format * @returns Formatted string */ formatValue(value) { if (value === null) { return '`null`'; } if (value === undefined) { return '`undefined`'; } if (typeof value === 'object') { return '`' + JSON.stringify(value) + '`'; } return String(value); } } exports.ListConverter = ListConverter;