diff --git a/simple-index.html b/simple-index.html index e74e1434..cd6140b6 100644 --- a/simple-index.html +++ b/simple-index.html @@ -60,7 +60,7 @@ class="h-8 w-8" /> - {{#if brandName}}{{brandName}}{{else}}BentoPDF{{/if}} diff --git a/src/js/logic/deskew-pdf-page.ts b/src/js/logic/deskew-pdf-page.ts index 0428d704..f5467770 100644 --- a/src/js/logic/deskew-pdf-page.ts +++ b/src/js/logic/deskew-pdf-page.ts @@ -273,7 +273,7 @@ function initPage(): void { if (backBtn) { backBtn.addEventListener('click', () => { - window.location.href = '/'; + window.location.href = import.meta.env.BASE_URL; }); } diff --git a/src/js/logic/form-filler-page.ts b/src/js/logic/form-filler-page.ts index ae49b487..47772bdc 100644 --- a/src/js/logic/form-filler-page.ts +++ b/src/js/logic/form-filler-page.ts @@ -311,6 +311,6 @@ document.addEventListener('DOMContentLoaded', () => { processBtn?.addEventListener('click', processAndDownloadForm); backBtn?.addEventListener('click', () => { - window.location.href = '../../index.html'; + window.location.href = import.meta.env.BASE_URL; }); }); diff --git a/src/js/logic/markdown-to-pdf-page.ts b/src/js/logic/markdown-to-pdf-page.ts index e104c983..3282f4cd 100644 --- a/src/js/logic/markdown-to-pdf-page.ts +++ b/src/js/logic/markdown-to-pdf-page.ts @@ -1,21 +1,21 @@ import { MarkdownEditor } from '../utils/markdown-editor.js'; document.addEventListener('DOMContentLoaded', () => { - const container = document.getElementById('markdown-editor-container'); + const container = document.getElementById('markdown-editor-container'); - if (!container) { - console.error('Markdown editor container not found'); - return; - } + if (!container) { + console.error('Markdown editor container not found'); + return; + } - const editor = new MarkdownEditor(container, {}); + const editor = new MarkdownEditor(container, {}); - console.log('Markdown editor initialized'); + console.log('Markdown editor initialized'); - const backButton = document.getElementById('back-to-tools'); - if (backButton) { - backButton.addEventListener('click', () => { - window.location.href = '/'; - }); - } + const backButton = document.getElementById('back-to-tools'); + if (backButton) { + backButton.addEventListener('click', () => { + window.location.href = import.meta.env.BASE_URL; + }); + } }); diff --git a/src/js/logic/remove-annotations-page.ts b/src/js/logic/remove-annotations-page.ts index bea92451..0ff3fda0 100644 --- a/src/js/logic/remove-annotations-page.ts +++ b/src/js/logic/remove-annotations-page.ts @@ -189,6 +189,6 @@ document.addEventListener('DOMContentLoaded', () => { processBtn?.addEventListener('click', processRemoveAnnotations); backBtn?.addEventListener('click', () => { - window.location.href = '../../index.html'; + window.location.href = import.meta.env.BASE_URL; }); }); diff --git a/src/js/logic/remove-blank-pages-page.ts b/src/js/logic/remove-blank-pages-page.ts index 5b70b371..540e37c6 100644 --- a/src/js/logic/remove-blank-pages-page.ts +++ b/src/js/logic/remove-blank-pages-page.ts @@ -366,6 +366,6 @@ document.addEventListener('DOMContentLoaded', () => { processBtn?.addEventListener('click', processRemoveBlankPages); document.getElementById('back-to-tools')?.addEventListener('click', () => { - window.location.href = '../../index.html'; + window.location.href = import.meta.env.BASE_URL; }); }); diff --git a/src/tests/navigation-base-url.test.ts b/src/tests/navigation-base-url.test.ts new file mode 100644 index 00000000..282e3559 --- /dev/null +++ b/src/tests/navigation-base-url.test.ts @@ -0,0 +1,95 @@ +import { describe, it, expect } from 'vitest'; +import * as fs from 'fs'; +import * as path from 'path'; + +describe('Navigation BASE_URL Consistency', () => { + const logicDir = path.resolve(__dirname, '../js/logic'); + const pagesDir = path.resolve(__dirname, '../pages'); + const rootDir = path.resolve(__dirname, '../../'); + + it('ensures no tool logic files hardcode root "/" or relative paths for navigation', () => { + const files = fs + .readdirSync(logicDir) + .filter((file) => file.endsWith('.ts') || file.endsWith('.js')); + + const offendingFiles: string[] = []; + + for (const file of files) { + const content = fs.readFileSync(path.join(logicDir, file), 'utf-8'); + + // Check for hardcoded root redirects or relative path escapes + if ( + /window\.location\.href\s*=\s*['"`]\/['"`]/.test(content) || + /window\.location\.href\s*=\s*['"`]\.\.\//.test(content) + ) { + offendingFiles.push(file); + } + } + + expect( + offendingFiles, + `The following files hardcode root or relative navigation instead of import.meta.env.BASE_URL: ${offendingFiles.join( + ', ' + )}` + ).toEqual([]); + }); + + it('ensures tool logic files with back-to-tools button use import.meta.env.BASE_URL', () => { + const files = fs + .readdirSync(logicDir) + .filter((file) => file.endsWith('.ts') || file.endsWith('.js')); + + const filesMissingBaseUrl: string[] = []; + + for (const file of files) { + const content = fs.readFileSync(path.join(logicDir, file), 'utf-8'); + + // If the file handles 'back-to-tools' click navigation + if ( + content.includes('back-to-tools') && + content.includes('window.location.href') && + !content.includes('import.meta.env.BASE_URL') + ) { + filesMissingBaseUrl.push(file); + } + } + + expect( + filesMissingBaseUrl, + `The following files handle back-to-tools navigation without import.meta.env.BASE_URL: ${filesMissingBaseUrl.join( + ', ' + )}` + ).toEqual([]); + }); + + it('ensures HTML templates do not hardcode href="/" for brand links', () => { + const htmlFiles = fs + .readdirSync(pagesDir) + .filter((file) => file.endsWith('.html')) + .map((file) => path.join(pagesDir, file)); + + const simpleIndex = path.join(rootDir, 'simple-index.html'); + if (fs.existsSync(simpleIndex)) { + htmlFiles.push(simpleIndex); + } + + const offendingFiles: string[] = []; + + for (const filePath of htmlFiles) { + const content = fs.readFileSync(filePath, 'utf-8'); + + // Brand link / logo anchor should not be hardcoded to href="/" + // e.g. should be + if (/]*href=["']\/["'][^>]*>.*BentoPDF/i.test(content)) { + offendingFiles.push(path.basename(filePath)); + } + } + + expect( + offendingFiles, + `The following HTML templates hardcode href="/" for the brand link: ${offendingFiles.join( + ', ' + )}` + ).toEqual([]); + }); +});