Skip to content

Commit

Permalink
Merge pull request #10 from CloudCannon/fix/baseurl
Browse files Browse the repository at this point in the history
Another fix for baseURLs
  • Loading branch information
rycoll authored Apr 11, 2024
2 parents b8b3344 + 28f97f7 commit 74f35a1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || [],
Expand Down
15 changes: 13 additions & 2 deletions src/generators/info.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
}, {});
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/hugo-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/hugo-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit 74f35a1

Please sign in to comment.