2022-11-11 22:06:29 -05:00
|
|
|
import * as hashtagify from './commonmark-hashtag.js';
|
2022-09-09 22:56:15 -04:00
|
|
|
|
2023-04-08 16:06:45 -04:00
|
|
|
function image(node, entering) {
|
2024-02-24 11:09:34 -05:00
|
|
|
if (
|
|
|
|
node.firstChild?.type === 'text' &&
|
|
|
|
node.firstChild.literal.startsWith('video:')
|
|
|
|
) {
|
2023-04-08 16:06:45 -04:00
|
|
|
if (entering) {
|
2024-02-24 11:09:34 -05:00
|
|
|
this.lit(
|
|
|
|
'<video style="max-width: 100%; max-height: 480px" title="' +
|
|
|
|
this.esc(node.firstChild?.literal) +
|
|
|
|
'" controls>'
|
|
|
|
);
|
2023-04-08 16:06:45 -04:00
|
|
|
this.lit('<source src="' + this.esc(node.destination) + '"></source>');
|
|
|
|
this.disableTags += 1;
|
|
|
|
} else {
|
|
|
|
this.disableTags -= 1;
|
|
|
|
this.lit('</video>');
|
|
|
|
}
|
2024-02-24 11:09:34 -05:00
|
|
|
} else if (
|
|
|
|
node.firstChild?.type === 'text' &&
|
|
|
|
node.firstChild.literal.startsWith('audio:')
|
|
|
|
) {
|
2023-04-11 20:32:14 -04:00
|
|
|
if (entering) {
|
2024-02-24 11:09:34 -05:00
|
|
|
this.lit(
|
|
|
|
'<audio style="height: 32px; max-width: 100%" title="' +
|
|
|
|
this.esc(node.firstChild?.literal) +
|
|
|
|
'" controls>'
|
|
|
|
);
|
2023-04-11 20:32:14 -04:00
|
|
|
this.lit('<source src="' + this.esc(node.destination) + '"></source>');
|
|
|
|
this.disableTags += 1;
|
|
|
|
} else {
|
|
|
|
this.disableTags -= 1;
|
|
|
|
this.lit('</audio>');
|
|
|
|
}
|
2023-04-08 16:06:45 -04:00
|
|
|
} else {
|
|
|
|
if (entering) {
|
|
|
|
if (this.disableTags === 0) {
|
2024-02-24 11:09:34 -05:00
|
|
|
this.lit(
|
|
|
|
'<div class="img_caption">' +
|
|
|
|
this.esc(node.firstChild?.literal || node.destination) +
|
|
|
|
'</div>'
|
|
|
|
);
|
2023-04-08 16:06:45 -04:00
|
|
|
if (this.options.safe && potentiallyUnsafe(node.destination)) {
|
|
|
|
this.lit('<img src="" alt="');
|
|
|
|
} else {
|
|
|
|
this.lit('<img src="' + this.esc(node.destination) + '" alt="');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.disableTags += 1;
|
|
|
|
} else {
|
|
|
|
this.disableTags -= 1;
|
|
|
|
if (this.disableTags === 0) {
|
|
|
|
if (node.title) {
|
|
|
|
this.lit('" title="' + this.esc(node.title));
|
|
|
|
}
|
|
|
|
this.lit('" />');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 19:26:43 -04:00
|
|
|
export function markdown(md) {
|
2024-01-17 19:38:38 -05:00
|
|
|
let reader = new commonmark.Parser({safe: true});
|
|
|
|
let writer = new commonmark.HtmlRenderer();
|
2023-04-08 16:06:45 -04:00
|
|
|
writer.image = image;
|
2024-01-17 19:38:38 -05:00
|
|
|
let parsed = reader.parse(md || '');
|
2023-01-22 12:25:37 -05:00
|
|
|
parsed = hashtagify.transform(parsed);
|
2024-01-17 19:38:38 -05:00
|
|
|
let walker = parsed.walker();
|
|
|
|
let event, node;
|
2022-09-06 19:26:43 -04:00
|
|
|
while ((event = walker.next())) {
|
|
|
|
node = event.node;
|
|
|
|
if (event.entering) {
|
|
|
|
if (node.type == 'link') {
|
2024-02-24 11:09:34 -05:00
|
|
|
if (
|
|
|
|
node.destination.startsWith('@') &&
|
|
|
|
node.destination.endsWith('.ed25519')
|
|
|
|
) {
|
2022-09-06 19:26:43 -04:00
|
|
|
node.destination = '#' + node.destination;
|
2024-02-24 11:09:34 -05:00
|
|
|
} else if (
|
|
|
|
node.destination.startsWith('%') &&
|
|
|
|
node.destination.endsWith('.sha256')
|
|
|
|
) {
|
2022-09-06 19:26:43 -04:00
|
|
|
node.destination = '#' + node.destination;
|
2024-02-24 11:09:34 -05:00
|
|
|
} else if (
|
|
|
|
node.destination.startsWith('&') &&
|
|
|
|
node.destination.endsWith('.sha256')
|
|
|
|
) {
|
2022-09-06 19:26:43 -04:00
|
|
|
node.destination = '/' + node.destination + '/view';
|
|
|
|
}
|
|
|
|
} else if (node.type == 'image') {
|
|
|
|
if (node.destination.startsWith('&')) {
|
|
|
|
node.destination = '/' + node.destination + '/view';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return writer.render(parsed);
|
2022-09-09 22:56:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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}`;
|
2024-02-24 11:09:34 -05:00
|
|
|
}
|