UNPKG

ts-markdown

Version:

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

50 lines (49 loc) 1.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.p = exports.pRenderer = void 0; const rendering_1 = require("../rendering"); /** * The renderer for paragraph entries. * * @param entry The paragraph entry. * @param options Document-level render options. * @returns Block-level paragraph markdown content. */ const pRenderer = (entry, options) => { if ('p' in entry) { let result = getParagraphMarkdown(entry, options); return { markdown: typeof result === 'string' ? result : result.markdown, blockLevel: true, }; } throw new Error('Entry is not a p entry. Unable to render.'); }; exports.pRenderer = pRenderer; function formatParagraphText(text) { return text ?.trimStart() .replace(/(^.*?)[\t]+/g, '') .trimStart(); } function getParagraphMarkdown(entry, options) { if (typeof entry.p === 'string') { return (0, rendering_1.getMarkdownString)(formatParagraphText(entry.p), options); } if (Array.isArray(entry.p)) { return formatParagraphText(entry.p.map((entry) => (0, rendering_1.getMarkdownString)(entry, options)).join('')); } let result = (0, rendering_1.getMarkdownString)(entry.p, options); let resultMarkdown = typeof result === 'string' ? result : result.markdown; return formatParagraphText(resultMarkdown) .split('\n') .map((x) => formatParagraphText(x)) .join('\n'); } function p(content, options) { return { p: content, ...options, }; } exports.p = p;