2020-12-09 23:30:51 +02:00
|
|
|
import { Link } from '../MdToHtml';
|
|
|
|
import { toForwardSlashes } from '../pathUtils';
|
|
|
|
const Entities = require('html-entities').AllHtmlEntities;
|
|
|
|
const htmlentities = new Entities().encode;
|
|
|
|
|
|
|
|
export interface Options {
|
|
|
|
audioPlayerEnabled: boolean;
|
|
|
|
videoPlayerEnabled: boolean;
|
|
|
|
pdfViewerEnabled: boolean;
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
function resourceUrl(resourceFullPath: string): string {
|
|
|
|
if (resourceFullPath.indexOf('http://') === 0 || resourceFullPath.indexOf('https://')) return resourceFullPath;
|
|
|
|
return `file://${toForwardSlashes(resourceFullPath)}`;
|
|
|
|
}
|
|
|
|
|
2020-12-09 23:30:51 +02:00
|
|
|
export default function(link: Link, options: Options) {
|
|
|
|
const resource = link.resource;
|
|
|
|
|
|
|
|
if (!link.resourceReady || !resource || !resource.mime) return '';
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
const escapedResourcePath = htmlentities(resourceUrl(link.resourceFullPath));
|
2020-12-09 23:30:51 +02:00
|
|
|
const escapedMime = htmlentities(resource.mime);
|
|
|
|
|
|
|
|
if (options.videoPlayerEnabled && resource.mime.indexOf('video/') === 0) {
|
|
|
|
return `
|
|
|
|
<video class="media-player media-video" controls>
|
|
|
|
<source src="${escapedResourcePath}" type="${escapedMime}">
|
|
|
|
</video>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.audioPlayerEnabled && resource.mime.indexOf('audio/') === 0) {
|
2022-07-30 14:11:21 +02:00
|
|
|
// We want to support both audio/x-flac and audio/flac MIME types, but chromium only supports audio/flac
|
|
|
|
// https://github.com/laurent22/joplin/issues/6434
|
|
|
|
const escapedAudioMime = escapedMime === 'audio/x-flac' ? 'audio/flac' : escapedMime;
|
2020-12-09 23:30:51 +02:00
|
|
|
return `
|
|
|
|
<audio class="media-player media-audio" controls>
|
2022-07-30 14:11:21 +02:00
|
|
|
<source src="${escapedResourcePath}" type="${escapedAudioMime}">
|
2020-12-09 23:30:51 +02:00
|
|
|
</audio>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.pdfViewerEnabled && resource.mime === 'application/pdf') {
|
|
|
|
return `<object data="${escapedResourcePath}" class="media-player media-pdf" type="${escapedMime}"></object>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|