From 28f97f71c271b745624082c0e5827bc7377fdca6 Mon Sep 17 00:00:00 2001 From: Nathan Kennedy Date: Thu, 11 Apr 2024 13:50:09 +1200 Subject: [PATCH] Another fix for baseURLs --- src/config.js | 4 ++-- src/generators/info.js | 15 +++++++++++++-- src/helpers/hugo-config.js | 2 +- test/helpers/hugo-config.test.js | 2 +- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/config.js b/src/config.js index 6f635ec..827b682 100644 --- a/src/config.js +++ b/src/config.js @@ -113,12 +113,12 @@ export async function getConfig(hugoConfig) { const paths = pathHelper.getPaths(); const file = await readFile(process.env.CLOUDCANNON_CONFIG_PATH) || {}; const legacy = getLegacyConfig(hugoConfig); - const baseUrl = file.base_url || getUrlPathname(hugoConfig.baseURL) || ''; + const baseURL = file.base_url || getUrlPathname(hugoConfig.baseURL) || ''; const config = { ...legacy, ...file, - base_url: baseUrl === '/' ? '' : baseUrl, + base_url: baseURL === '/' ? '' : baseURL, source: file.source || paths.source || '', multilingual: { languages: hugoConfig.languages || [], diff --git a/src/generators/info.js b/src/generators/info.js index 71d4854..58f684f 100644 --- a/src/generators/info.js +++ b/src/generators/info.js @@ -1,5 +1,5 @@ import Papa from 'papaparse'; -import { runProcess } from '../helpers/helpers.js'; +import { runProcess, getUrlPathname } from '../helpers/helpers.js'; import pathHelper from '../helpers/paths.js'; import chalk from 'chalk'; import log from '../helpers/logger.js'; @@ -26,7 +26,18 @@ async function getHugoUrls(hugoConfig) { const fileList = Papa.parse(fileCsv, { header: true }); return fileList.data.reduce((memo, file) => { - memo[file.path] = ('/' + file.permalink.replace(hugoConfig.baseURL ?? '', '')).replace(/^\/\//, '/'); + const baseURLPathname = getUrlPathname(hugoConfig.baseURL || '') + .replace(/^\/*/g, '') + .replace(/\/*$/g, '/'); + + let filePathname = getUrlPathname(file.permalink).replace(/^\/*/g, ''); + + if (filePathname.startsWith(baseURLPathname)) { + filePathname = filePathname.substring(baseURLPathname.length); + } + const url = ('/' + filePathname).replace(/^\/\//, '/'); + + memo[file.path] = url; return memo; }, {}); } diff --git a/src/helpers/hugo-config.js b/src/helpers/hugo-config.js index dfc685d..4fbaca2 100644 --- a/src/helpers/hugo-config.js +++ b/src/helpers/hugo-config.js @@ -116,7 +116,7 @@ export async function generateConfigObject(flags = {}, options) { export async function getHugoConfig(flags = {}) { const configObject = await generateConfigObject(flags); - configObject.baseURL = flags.baseUrl || configObject.baseURL || '/'; + configObject.baseURL = flags.baseURL || configObject.baseURL || '/'; if (flags.source) { configObject.source = flags.source; diff --git a/test/helpers/hugo-config.test.js b/test/helpers/hugo-config.test.js index 184a423..56f0615 100644 --- a/test/helpers/hugo-config.test.js +++ b/test/helpers/hugo-config.test.js @@ -299,7 +299,7 @@ describe('hugo-config', function () { baseURL: 'http://build-arg.org/' }; - const flags = { baseUrl: 'http://build-arg.org/' }; + const flags = { baseURL: 'http://build-arg.org/' }; const obj = await getHugoConfig(flags); expect(obj).to.deep.equal(expected); });