Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔗 Use READTHEDOCS_CANONICAL_URL as baseurl if defined #996

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading