UNPKG

@mdfriday/foundry

Version:

The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.

98 lines 6.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const url_1 = require("./url"); describe('URL', () => { describe('relURL', () => { describe('with relative BaseURL', () => { it('should handle relative BaseURL /public/', () => { const url = new url_1.URL('/public/'); // 相对文件路径 expect(url.relURL('fuse.min.js')).toBe('/public/fuse.min.js'); expect(url.relURL('css/style.css')).toBe('/public/css/style.css'); expect(url.relURL('images/logo.png')).toBe('/public/images/logo.png'); // 绝对路径(相对于服务器根目录) expect(url.relURL('/assets/script.js')).toBe('/assets/script.js'); expect(url.relURL('/favicon.ico')).toBe('/favicon.ico'); }); it('should handle relative BaseURL /public (without trailing slash)', () => { const url = new url_1.URL('/public'); expect(url.relURL('fuse.min.js')).toBe('/public/fuse.min.js'); expect(url.relURL('css/style.css')).toBe('/public/css/style.css'); }); it('should handle root BaseURL /', () => { const url = new url_1.URL('/'); expect(url.relURL('fuse.min.js')).toBe('/fuse.min.js'); expect(url.relURL('css/style.css')).toBe('/css/style.css'); expect(url.relURL('/assets/script.js')).toBe('/assets/script.js'); }); }); describe('with absolute BaseURL', () => { it('should handle absolute BaseURL https://example.com/', () => { const url = new url_1.URL('https://example.com/'); // 相对文件路径 expect(url.relURL('fuse.min.js')).toBe('https://example.com/fuse.min.js'); expect(url.relURL('css/style.css')).toBe('https://example.com/css/style.css'); expect(url.relURL('images/logo.png')).toBe('https://example.com/images/logo.png'); // 绝对路径(相对于服务器根目录) expect(url.relURL('/assets/script.js')).toBe('https://example.com/assets/script.js'); expect(url.relURL('/favicon.ico')).toBe('https://example.com/favicon.ico'); }); it('should handle absolute BaseURL with path https://example.com/public/', () => { const url = new url_1.URL('https://example.com/public/'); // 相对文件路径 expect(url.relURL('fuse.min.js')).toBe('https://example.com/public/fuse.min.js'); expect(url.relURL('css/style.css')).toBe('https://example.com/public/css/style.css'); // 绝对路径(相对于服务器根目录) expect(url.relURL('/assets/script.js')).toBe('https://example.com/assets/script.js'); expect(url.relURL('/favicon.ico')).toBe('https://example.com/favicon.ico'); }); it('should handle HTTP BaseURL', () => { const url = new url_1.URL('http://localhost:3000/'); expect(url.relURL('fuse.min.js')).toBe('http://localhost:3000/fuse.min.js'); expect(url.relURL('/api/data')).toBe('http://localhost:3000/api/data'); }); }); describe('edge cases', () => { it('should handle empty input', () => { const url = new url_1.URL('/public/'); expect(url.relURL('')).toBe(''); const absoluteUrl = new url_1.URL('https://example.com/'); expect(absoluteUrl.relURL('')).toBe(''); }); it('should handle absolute URLs in input', () => { const url = new url_1.URL('/public/'); expect(url.relURL('https://external.com/file.js')).toBe('https://external.com/file.js'); expect(url.relURL('http://external.com/file.js')).toBe('http://external.com/file.js'); }); it('should handle protocol-relative URLs', () => { const url = new url_1.URL('/public/'); expect(url.relURL('//cdn.example.com/file.js')).toBe('//cdn.example.com/file.js'); }); it('should normalize multiple slashes', () => { const url = new url_1.URL('/public/'); expect(url.relURL('path//to//file.js')).toBe('/public/path/to/file.js'); const absoluteUrl = new url_1.URL('https://example.com/'); expect(absoluteUrl.relURL('path//to//file.js')).toBe('https://example.com/path/to/file.js'); }); }); describe('real-world scenarios', () => { it('should work like Hugo template relURL function', () => { // 场景1: 相对路径 BaseURL const relativeUrl = new url_1.URL('/public/'); expect(relativeUrl.relURL('fuse.min.js')).toBe('/public/fuse.min.js'); expect(relativeUrl.relURL('css/main.css')).toBe('/public/css/main.css'); expect(relativeUrl.relURL('js/app.js')).toBe('/public/js/app.js'); // 场景2: 绝对 URL BaseURL const absoluteUrl = new url_1.URL('https://example.com/'); expect(absoluteUrl.relURL('fuse.min.js')).toBe('https://example.com/fuse.min.js'); expect(absoluteUrl.relURL('css/main.css')).toBe('https://example.com/css/main.css'); expect(absoluteUrl.relURL('js/app.js')).toBe('https://example.com/js/app.js'); // 场景3: 带路径的绝对 URL BaseURL const pathUrl = new url_1.URL('https://example.com/blog/'); expect(pathUrl.relURL('fuse.min.js')).toBe('https://example.com/blog/fuse.min.js'); expect(pathUrl.relURL('css/main.css')).toBe('https://example.com/blog/css/main.css'); }); }); }); }); //# sourceMappingURL=url.test.js.map