@canopy-iiif/app 1.4.10 → 1.4.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,67 @@
1
+ 'use strict';
2
+
3
+ function normalizeBasePath(value) {
4
+ if (value == null) return '';
5
+ const raw = String(value).trim();
6
+ if (!raw) return '';
7
+ const prefixed = raw.startsWith('/') ? raw : `/${raw}`;
8
+ const cleaned = prefixed.replace(/\/+$/, '');
9
+ return cleaned === '/' ? '' : cleaned;
10
+ }
11
+
12
+ let cachedBasePath = null;
13
+
14
+ function readBasePath(options = {}) {
15
+ const {force = false} = options;
16
+ if (!force && cachedBasePath !== null) return cachedBasePath;
17
+ const candidates = [];
18
+ try {
19
+ if (typeof window !== 'undefined' && window.CANOPY_BASE_PATH != null) {
20
+ candidates.push(window.CANOPY_BASE_PATH);
21
+ }
22
+ } catch (_) {}
23
+ try {
24
+ if (typeof globalThis !== 'undefined' && globalThis.CANOPY_BASE_PATH != null) {
25
+ candidates.push(globalThis.CANOPY_BASE_PATH);
26
+ }
27
+ } catch (_) {}
28
+ try {
29
+ if (typeof process !== 'undefined' && process.env && process.env.CANOPY_BASE_PATH) {
30
+ candidates.push(process.env.CANOPY_BASE_PATH);
31
+ }
32
+ } catch (_) {}
33
+
34
+ for (const candidate of candidates) {
35
+ const normalized = normalizeBasePath(candidate);
36
+ if (normalized || normalized === '') {
37
+ cachedBasePath = normalized;
38
+ return cachedBasePath;
39
+ }
40
+ }
41
+
42
+ cachedBasePath = '';
43
+ return cachedBasePath;
44
+ }
45
+
46
+ function withBasePath(href) {
47
+ if (typeof href !== 'string') return href;
48
+ const raw = href.trim();
49
+ if (!raw) return href;
50
+ if (/^(?:[a-z][a-z0-9+.-]*:|\/\/|#)/i.test(raw)) return raw;
51
+ if (!raw.startsWith('/')) return raw;
52
+ const base = readBasePath();
53
+ if (!base || base === '/') return raw;
54
+ if (raw === base || raw.startsWith(`${base}/`)) return raw;
55
+ return `${base}${raw}`;
56
+ }
57
+
58
+ function clearBasePathCache() {
59
+ cachedBasePath = null;
60
+ }
61
+
62
+ module.exports = {
63
+ normalizeBasePath,
64
+ readBasePath,
65
+ withBasePath,
66
+ clearBasePathCache,
67
+ };
package/lib/common.js CHANGED
@@ -8,7 +8,9 @@ const OUT_DIR = path.resolve('site');
8
8
  const CACHE_DIR = path.resolve('.cache/mdx');
9
9
  const ASSETS_DIR = path.resolve('assets');
10
10
 
11
- const BASE_PATH = String(process.env.CANOPY_BASE_PATH || '').replace(/\/$/, '');
11
+ const { readBasePath, withBasePath } = require('./base-path');
12
+
13
+ const BASE_PATH = readBasePath();
12
14
  let cachedAppearance = null;
13
15
 
14
16
  function resolveThemeAppearance() {
@@ -107,10 +109,7 @@ function htmlShell({ title, body, cssHref, scriptHref, headExtra, bodyClass }) {
107
109
  }
108
110
 
109
111
  function withBase(href) {
110
- if (!href) return href;
111
- if (!BASE_PATH) return href;
112
- if (typeof href === 'string' && href.startsWith('/')) return `${BASE_PATH}${href}`;
113
- return href;
112
+ return withBasePath(href);
114
113
  }
115
114
 
116
115
  function rootRelativeHref(href) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canopy-iiif/app",
3
- "version": "1.4.10",
3
+ "version": "1.4.11",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "author": "Mat Jordan <mat@northwestern.edu>",
@@ -33,6 +33,11 @@
33
33
  "./ui/theme": "./ui/theme.js",
34
34
  "./lib/components/*": "./lib/components/*",
35
35
  "./head": "./lib/head.js",
36
+ "./base-path": {
37
+ "types": "./types/base-path.d.ts",
38
+ "default": "./lib/base-path.js",
39
+ "require": "./lib/base-path.js"
40
+ },
36
41
  "./orchestrator": {
37
42
  "types": "./types/orchestrator.d.ts",
38
43
  "import": "./lib/orchestrator.js",
@@ -0,0 +1,4 @@
1
+ export function normalizeBasePath(value: unknown): string;
2
+ export function readBasePath(options?: {force?: boolean}): string;
3
+ export function withBasePath(href: string): string;
4
+ export function clearBasePathCache(): void;