UNPKG

ts-markdown

Version:

An extensible TypeScript markdown generator that takes JSON and creates a markdown document.

51 lines (50 loc) 1.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ul = exports.ulRenderer = void 0; const rendering_1 = require("../rendering"); /** * The renderer for unordered list entries. * * @param entry The unordered list entry. * @param options Document-level render options. * @returns Block-level unordered list markdown content. */ const ulRenderer = (entry, options) => { if ('ul' in entry) { let markdown = getUnorderedListMarkdown(entry, options); return { markdown, blockLevel: true, }; } throw new Error('Entry is not an ul entry. Unable to render.'); }; exports.ulRenderer = ulRenderer; function getUnorderedListMarkdown(entry, options) { let indicator = entry.indicator ?? options.unorderedListItemIndicator ?? '-'; return entry.ul .map((li) => { if (Array.isArray(li)) { return (0, rendering_1.renderEntries)(li, { ...options, prefix: (liIndex) => (liIndex === 0 ? `${indicator} ` : ' '), }); } return `${indicator} ${(0, rendering_1.getMarkdownString)(li, options)}`; }) .map((x) => x.replace(/^([\-\+\*]\s[\d]+)(\.)/, '$1\\.')) .join('\n'); } /** * Helper which creates an unordered list entry. * * @param options Entry-level options for this element. * @returns an unordered list entry */ function ul(content, options) { return { ul: content, ...options, }; } exports.ul = ul;