UNPKG

@sanity/block-content-to-markdown

Version:
107 lines (82 loc) 2.68 kB
'use strict'; var _require = require('@sanity/block-content-to-hyperscript/internals'), getImageUrl = _require.getImageUrl; function renderChildren(props) { var divider = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; return Array.isArray(props.children) ? props.children.join(divider) : props.children; } function block(props) { var style = props.node.style || 'normal'; if (/^h\d$/.test(style)) { var hashes = new Array(parseInt(style[1], 10) + 1).join('#'); return hashes + ' ' + renderChildren(props); } if (style === 'blockquote') { return '> ' + renderChildren(props); } return renderChildren(props); } function RawMarkSerializer(char, padWhitespace, props) { var children = renderChildren(props); if (padWhitespace) { var startContent = children.search(/\S/); var endContent = children.search(/\S(?=\s*$)/); if (endContent == -1 || startContent == -1) { return children; } var start = children.substring(0, startContent); var end = children.substring(endContent + 1); var content = children.substring(startContent, endContent + 1); return '' + start + char + content + char + end; } return '' + char + children + char; } function link(props) { var _props$mark = props.mark, href = _props$mark.href, title = _props$mark.title; var linkTitle = title ? ' ' + JSON.stringify(title) : ''; return '[' + renderChildren(props) + '](' + href + linkTitle + ')'; } function list(props) { var indentation = new Array(props.level || 1).join(' '); return indentation + renderChildren(props, '\n' + indentation); } function listItem(props) { var isBullet = props.node.listItem === 'bullet'; var char = isBullet ? '*' : props.index + 1 + '.'; return char + ' ' + renderChildren(props); } function image(props) { var title = props.title, alt = props.alt; var url = getImageUrl(props); var imgTitle = title ? ' ' + JSON.stringify(title) : ''; return '![' + (alt || '') + '](' + url + imgTitle + ')'; } function container(props) { return renderChildren(props, '\n\n'); } function hardBreak() { return ' \n'; } module.exports = { types: { block: block, image: image }, marks: { 'strike-through': RawMarkSerializer.bind(null, '~~', true), em: RawMarkSerializer.bind(null, '_', true), code: RawMarkSerializer.bind(null, '`', false), strong: RawMarkSerializer.bind(null, '**', true), underline: renderChildren, link: link }, list: list, listItem: listItem, container: container, hardBreak: hardBreak, markFallback: renderChildren }; //# sourceMappingURL=serializers.js.map