Skip to content

Commit

Permalink
🔧 Frontmatter edit -> edit_url
Browse files Browse the repository at this point in the history
  • Loading branch information
fwkoch committed Jan 23, 2025
1 parent 38f1af5 commit 59f7c86
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
4 changes: 2 additions & 2 deletions docs/frontmatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ The following table lists the available frontmatter fields, a brief description
* - `github`
- a valid GitHub URL or `owner/reponame`
- page can override project
* - `edit`
- URL to edit the page source. If this value is unset but `github` is specified, MyST will attempt to compute the specific github URL for the page. You may disable this behavior by explicitly setting `edit` to `null`.
* - `edit_url`
- URL to edit the page source. If this value is unset but `github` is specified, MyST will attempt to compute the specific github URL for the page. You may disable this behavior by explicitly setting `edit_url` to `null`.
- page can override project
* - `binder`
- any valid URL
Expand Down
20 changes: 12 additions & 8 deletions packages/myst-cli/src/utils/addEditUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@ function gitCommandAvailable(): boolean {
return !!which.sync('git', { nothrow: true });
}

/**
* Compute edit_url and add to frontmatter
*
* If edit_url is already defined on the page it will remain unchanged.
* If edit_url is explicitly null or if github url is not defined, edit_url will not be set.
* If git is not available to determine branch and top-level folder, edit_url will not be set.
*/
export async function addEditUrl(session: ISession, frontmatter: PageFrontmatter, file: string) {
if (frontmatter.edit || frontmatter.edit === null) return;
if (!frontmatter.github) return
if (frontmatter.edit_url || frontmatter.edit_url === null) return;
if (!gitCommandAvailable()) return;
try {
const gitLog = silentLogger();
const getGitOrigin = makeExecutable('git config --get remote.origin.url', gitLog);
const gitOrigin =
frontmatter.github ??
(await getGitOrigin()).trim().replace('[email protected]:', 'https://github.com/');
const getGitBranch = makeExecutable('git rev-parse --abbrev-ref HEAD', gitLog);
const gitBranch = (await getGitBranch()).trim();
const getGitRoot = makeExecutable('git rev-parse --show-toplevel', gitLog);
const gitRoot = (await getGitRoot()).trim();
if (gitOrigin && gitBranch && gitRoot && file.startsWith(gitRoot)) {
frontmatter.edit = `${gitOrigin}/blob/${gitBranch}${file.replace(gitRoot, '')}`;
session.log.debug(`Added edit URL ${frontmatter.edit} to ${file}`);
if (gitBranch && gitRoot && file.startsWith(gitRoot)) {
frontmatter.edit_url = `${frontmatter.github}/blob/${gitBranch}${file.replace(gitRoot, '')}`;
session.log.debug(`Added edit URL ${frontmatter.edit_url} to ${file}`);
}
} catch {
session.log.debug(`Unable to add edit URL to ${file}`);
Expand Down
4 changes: 2 additions & 2 deletions packages/myst-frontmatter/src/project/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const PROJECT_AND_PAGE_FRONTMATTER_KEYS = [
'exports',
'downloads',
'settings', // We maybe want to move this into site frontmatter in the future
'edit',
'edit_url',
...KNOWN_EXTERNAL_IDENTIFIERS,
// Do not add any project specific keys here!
...SITE_FRONTMATTER_KEYS,
Expand Down Expand Up @@ -74,7 +74,7 @@ export type ProjectAndPageFrontmatter = SiteFrontmatter & {
exports?: Export[];
downloads?: Download[];
settings?: ProjectSettings;
edit?: string | null;
edit_url?: string | null;
};

export type ProjectFrontmatter = ProjectAndPageFrontmatter & {
Expand Down
8 changes: 4 additions & 4 deletions packages/myst-frontmatter/src/project/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ export function validateProjectAndPageFrontmatterKeys(
);
if (settings) output.settings = settings;
}
if (value.edit === null) {
output.edit = null;
} else if (defined(value.edit)) {
output.edit = validateUrl(value.edit, incrementOptions('edit', opts));
if (value.edit_url === null) {
output.edit_url = null;
} else if (defined(value.edit_url)) {
output.edit_url = validateUrl(value.edit_url, incrementOptions('edit_url', opts));
}
return output;
}
Expand Down

0 comments on commit 59f7c86

Please sign in to comment.