Skip to content

Commit

Permalink
🔗 Use READTHEDOCS_CANONICAL_URL as baseurl if defined (#996)
Browse files Browse the repository at this point in the history
Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
Co-authored-by: Rowan Cockett <rowanc1@gmail.com>
  • Loading branch information
LecrisUT and rowanc1 authored Apr 17, 2024
1 parent 74091e3 commit 882eb90
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-buttons-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"myst-cli": patch
---

Use READTHEDOCS_CANONICAL_URL as baseurl if defined
34 changes: 28 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,44 @@ 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)) {
// Get only the path part of the RTD url, without trailing `/`
baseurl = new URL(baseurl).pathname.replace(/\/$/, '');
session.log.info(
`Building inside a ReadTheDocs environment for ${process.env.READTHEDOCS_CANONICAL_URL}`,
);
}
// 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 882eb90

Please sign in to comment.