2020-12-09 23:30:51 +02:00
|
|
|
import { Link } from '../MdToHtml';
|
2024-01-18 13:20:10 +02:00
|
|
|
import { toForwardSlashes } from '@joplin/utils/path';
|
2022-08-27 14:32:20 +02:00
|
|
|
import { LinkIndexes } from './rules/link_close';
|
2020-12-09 23:30:51 +02:00
|
|
|
const Entities = require('html-entities').AllHtmlEntities;
|
|
|
|
const htmlentities = new Entities().encode;
|
|
|
|
|
|
|
|
export interface Options {
|
|
|
|
audioPlayerEnabled: boolean;
|
|
|
|
videoPlayerEnabled: boolean;
|
|
|
|
pdfViewerEnabled: boolean;
|
2022-08-04 11:12:22 +02:00
|
|
|
useCustomPdfViewer: boolean;
|
2022-08-27 14:32:20 +02:00
|
|
|
noteId: string;
|
|
|
|
vendorDir: string;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2022-08-04 11:12:22 +02:00
|
|
|
theme: any;
|
2020-12-09 23:30:51 +02:00
|
|
|
}
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
function resourceUrl(resourceFullPath: string): string {
|
2024-08-17 13:22:03 +02:00
|
|
|
if (
|
|
|
|
resourceFullPath.indexOf('http://') === 0 || resourceFullPath.indexOf('https://') === 0 || resourceFullPath.indexOf('joplin-content://') === 0 ||
|
|
|
|
resourceFullPath.indexOf('file://') === 0
|
|
|
|
) {
|
|
|
|
return resourceFullPath;
|
|
|
|
}
|
2021-01-29 20:45:11 +02:00
|
|
|
return `file://${toForwardSlashes(resourceFullPath)}`;
|
|
|
|
}
|
|
|
|
|
2022-08-27 14:32:20 +02:00
|
|
|
export default function(link: Link, options: Options, linkIndexes: LinkIndexes) {
|
2020-12-09 23:30:51 +02:00
|
|
|
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') {
|
2022-08-27 14:32:20 +02:00
|
|
|
|
2022-08-04 11:12:22 +02:00
|
|
|
if (options.useCustomPdfViewer) {
|
2022-08-27 14:32:20 +02:00
|
|
|
const resourceId = resource.id;
|
2022-08-04 11:12:22 +02:00
|
|
|
let anchorPageNo = null;
|
2022-08-27 14:32:20 +02:00
|
|
|
|
|
|
|
let id = `${options.noteId}.${resourceId}`;
|
|
|
|
if (linkIndexes && linkIndexes[resourceId]) {
|
|
|
|
linkIndexes[resourceId]++;
|
|
|
|
} else {
|
|
|
|
linkIndexes[resourceId] = 1;
|
|
|
|
}
|
|
|
|
id += `.${linkIndexes[resourceId]}`;
|
|
|
|
|
2022-08-04 11:12:22 +02:00
|
|
|
if (link.href.indexOf('#') > 0) {
|
|
|
|
anchorPageNo = Number(link.href.split('#').pop());
|
|
|
|
if (anchorPageNo < 1) anchorPageNo = null;
|
|
|
|
}
|
2022-08-27 14:32:20 +02:00
|
|
|
|
|
|
|
const src = `${options.vendorDir}/lib/@joplin/pdf-viewer/index.html`;
|
|
|
|
|
|
|
|
return `<iframe src="${src}" x-url="${escapedResourcePath}"
|
|
|
|
x-appearance="${options.theme.appearance}" ${anchorPageNo ? `x-anchorPage="${anchorPageNo}"` : ''} id="${id}"
|
2022-09-11 15:58:32 +02:00
|
|
|
x-type="mini" x-resourceid="${resourceId}"
|
2022-08-04 11:12:22 +02:00
|
|
|
class="media-player media-pdf"></iframe>`;
|
|
|
|
}
|
2022-08-27 14:32:20 +02:00
|
|
|
|
2020-12-09 23:30:51 +02:00
|
|
|
return `<object data="${escapedResourcePath}" class="media-player media-pdf" type="${escapedMime}"></object>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|