import * as linkify from './commonmark-linkify.js'; var reUnsafeProtocol = /^javascript:|vbscript:|file:|data:/i; var reSafeDataProtocol = /^data:image\/(?:png|gif|jpeg|webp)/i; var potentiallyUnsafe = function(url) { return reUnsafeProtocol.test(url) && !reSafeDataProtocol.test(url); }; function image(node, entering) { if ( node.firstChild?.type === 'text' && node.firstChild.literal.startsWith('video:') ) { if (entering) { this.lit( ''); } } else if ( node.firstChild?.type === 'text' && node.firstChild.literal.startsWith('audio:') ) { if (entering) { this.lit( ''); } } else { if (entering) { if (this.disableTags === 0) { this.lit( '
' ); if (this.options.safe && potentiallyUnsafe(node.destination)) { this.lit(''); } } } } export function markdown(md) { var reader = new commonmark.Parser(); var writer = new commonmark.HtmlRenderer({safe: true}); writer.image = image; var parsed = reader.parse(md || ''); parsed = linkify.transform(parsed); var walker = parsed.walker(); var event, node; while ((event = walker.next())) { node = event.node; if (event.entering) { if (node.type == 'link') { if ( node.destination.startsWith('@') && node.destination.endsWith('.ed25519') ) { node.destination = '#' + node.destination; } else if ( node.destination.startsWith('%') && node.destination.endsWith('.sha256') ) { node.destination = '#' + node.destination; } else if ( node.destination.startsWith('&') && node.destination.endsWith('.sha256') ) { node.destination = '/' + node.destination + '/view'; } } else if (node.type == 'image') { if (node.destination.startsWith('&')) { node.destination = '/' + node.destination + '/view'; } } } } return writer.render(parsed); } export function human_readable_size(bytes) { let v = bytes; let u = 'B'; for (let unit of ['kB', 'MB', 'GB']) { if (v > 1024) { v /= 1024; u = unit; } else { break; } } return `${Math.round(v * 10) / 10} ${u}`; }