Skip to content

Commit

Permalink
fix: add better pdf viewer building function (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
spaenleh authored Apr 3, 2024
1 parent 62305ae commit 636109b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ export {
redirectToSavedUrl,
buildSignInPath,
buildPdfViewerLink,
buildPdfViewerURL,
buildItemLinkForBuilder,
} from './navigation/navigation.js';

Expand Down
30 changes: 30 additions & 0 deletions src/navigation/navigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DEFAULT_PROTOCOL } from './constants.js';
import {
buildItemLinkForBuilder,
buildPdfViewerLink,
buildPdfViewerURL,
buildSignInPath,
redirect,
redirectToSavedUrl,
Expand Down Expand Up @@ -180,4 +181,33 @@ describe('Navigation Util Tests', () => {
expect(res).toContain(assetsUrl);
});
});

describe('buildPdfViewerURL', () => {
const assetsUrl = 'localhost';
const assetsUrlWithProtocol = 'http://localhost';
const assetsUrlWithProtocolAndPath = 'http://localhost/assets/';

it('undefined if url is not provided', () => {
const res = buildPdfViewerURL();
expect(res).toBeUndefined();
});
it('undefined if url is empty', () => {
const res = buildPdfViewerURL('');
expect(res).toBeUndefined();
});
it('build url with asset url being just a domain', () => {
const res = buildPdfViewerURL(assetsUrl);
expect(res?.toString()).toMatch(/https:\/\/localhost\/pdf-viewer*/i);
});
it('build url with asset url containing protocol', () => {
const res = buildPdfViewerURL(assetsUrlWithProtocol);
expect(res?.toString()).toMatch(/http:\/\/localhost\/pdf-viewer*/i);
});
it('build url with asset url containing protocol and path', () => {
const res = buildPdfViewerURL(assetsUrlWithProtocolAndPath);
expect(res?.toString()).toMatch(
/http:\/\/localhost\/assets\/pdf-viewer*/i,
);
});
});
});
25 changes: 25 additions & 0 deletions src/navigation/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,34 @@ export const buildItemLinkForBuilder = (args: {
};

/**
* @deprecated Use `buildPdfViewerURL` instead
* Return a link to display a pdf, embedded in a custom pdf viewer if provided
* @param assetsUrl assets url where the pdf viewer is hosted
* @returns embedded link to display a pdf
*/
export const buildPdfViewerLink = (assetsUrl?: string) =>
assetsUrl ? `https://${assetsUrl}/pdf-viewer/web/viewer.html?file=` : '';

/**
* Returns the URL object of the resource that hosts a pdf viewer
* If url contains protocol and path, it needs to end with a trailing
* slash otherwise the last segment will be dropped
* @param assetsUrl assets url where the pdf viewer is hosted
* @returns embedded link to display a pdf
*/
export const buildPdfViewerURL = (assetsUrl?: string): URL | undefined => {
if (!assetsUrl) {
console.debug('No assets url provided');
return undefined;
}

let baseUrl = '';
// check if input contains protocol
try {
baseUrl = new URL(assetsUrl).href;
} catch {
// is not a valid url, so we should append the protocol
baseUrl = `https://${assetsUrl}`;
}
return new URL('pdf-viewer/web/viewer.html', baseUrl);
};

0 comments on commit 636109b

Please sign in to comment.