Skip to content

Commit

Permalink
Use READTHEDOCS_CANONICAL_URL as baseurl if defined
Browse files Browse the repository at this point in the history
Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
  • Loading branch information
LecrisUT committed Mar 18, 2024
1 parent e07d55a commit d8063b3
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions packages/myst-cli/src/build/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,40 @@ function rewriteAssetsFolder(directory: string, baseurl?: string): void {
}

/**
* Build a MyST project as a static HTML deployment
* Get the baseurl from BASE_URL or common deployment environments
*
* @param session session with logging
* @param opts configuration options
*/
export async function buildHtml(session: ISession, opts: any) {
const template = await getMystTemplate(session, opts);
// The BASE_URL env variable allows for mounting the site in a folder, e.g., github pages
const baseurl = process.env.BASE_URL;
function get_baseurl(session: ISession): string | undefined {
let baseurl;
// BASE_URL always takes precedence. If it's not defined, check common deployment environments.
if ((baseurl = process.env.BASE_URL)) {
session.log.info('BASE_URL environment overwrite is set');
} else if ((baseurl = process.env.READTHEDOCS_CANONICAL_URL)) {
session.log.info('Building inside a ReadTheDocs environment');
}
// Check if baseurl was set to any value, otherwise print a hint on how to set it manually.
if (baseurl) {
session.log.info(`Building the site with a baseurl of "${baseurl}"`);
} else {
// The user should only use `BASE_URL` to set the value manually.
session.log.info(
'Building the base site.\nTo set a baseurl (e.g. GitHub pages) use "BASE_URL" environment variable.',
);
}
return baseurl;
}

/**
* Build a MyST project as a static HTML deployment
*
* @param session session with logging
* @param opts configuration options
*/
export async function buildHtml(session: ISession, opts: any) {
const template = await getMystTemplate(session, opts);
// The BASE_URL env variable allows for mounting the site in a folder, e.g., github pages
const baseurl = get_baseurl(session);
// Note, this process is really only for Remix templates
// We could add a flag in the future for other templates
const htmlDir = path.join(session.buildPath(), 'html');
Expand Down

0 comments on commit d8063b3

Please sign in to comment.