Skip to content

Commit a01426a

Browse files
mfisher87rowanc1agoose77fwkoch
authored
🚸 Take users directly to the GitHub editing interface when clicking "Edit this page" (#2138)
Co-authored-by: Rowan Cockett <[email protected]> Co-authored-by: Angus Hollands <[email protected]> Co-authored-by: Franklin Koch <[email protected]>
1 parent e661f85 commit a01426a

File tree

7 files changed

+21
-10
lines changed

7 files changed

+21
-10
lines changed

.changeset/new-hounds-appear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"myst-cli": patch
3+
---
4+
5+
When clicking "edit this page" take the user directly to the GitHub edit interface

docs/frontmatter.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@ The following table lists the available frontmatter fields, a brief description
206206
- a valid GitHub URL or `owner/reponame`
207207
- page can override project
208208
* - `edit_url`
209-
- 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`.
209+
- 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`.
210+
- page can override project
211+
* - `source_url`
212+
- URL to view 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 `source_url` to `null`.
210213
- page can override project
211214
* - `binder`
212215
- any valid URL

docs/website-navigation.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,7 @@ See [](./frontmatter.md) for many kinds of metadata that configure this section.
144144

145145
### Use the Edit this Page button
146146

147-
If you've added [`github` MyST frontmatter](#table-frontmatter), the MyST themes will display an "Edit this page" link for your page. This link will point to the source file for your page in GitHub, allowing a reader to quickly view the source and make an edit using GitHub's UI.
148-
149-
:::{tip} How to use GitHub's UI to edit a page
150-
There are two ways to edit a URL in GitHub:
151-
152-
1. Click the pencil icon {kbd}`✏️` to open a lightweight editor on the page.
153-
2. Replace `.com` with `.dev` in the URL (or, simply press the period button {kbd}`.`). This will open a VSCode editor session you can use to make more extensive edits.
154-
:::
147+
If you've added [`github` MyST frontmatter](#table-frontmatter), the MyST themes will display an "Edit this page" link for your page. This link will take the user directly to GitHub's editing interface for the given page.
155148

156149
To override this behavior and set a manual edit URL, use the `edit_url` field in [MyST frontmatter](#table-frontmatter).
157150

packages/myst-cli/src/utils/addEditUrl.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export async function addEditUrl(session: ISession, frontmatter: PageFrontmatter
2525
const getGitRoot = makeExecutable('git rev-parse --show-toplevel', gitLog);
2626
const gitRoot = (await getGitRoot()).trim();
2727
if (gitBranch && gitRoot && file.startsWith(gitRoot)) {
28-
frontmatter.edit_url = `${frontmatter.github}/blob/${gitBranch}${file.replace(gitRoot, '')}`;
28+
frontmatter.source_url = `${frontmatter.github}/blob/${gitBranch}${file.replace(gitRoot, '')}`;
29+
frontmatter.edit_url = `${frontmatter.github}/edit/${gitBranch}${file.replace(gitRoot, '')}`;
2930
session.log.debug(`Added edit URL ${frontmatter.edit_url} to ${file}`);
3031
}
3132
} catch {

packages/myst-frontmatter/src/page/validators.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
validateString,
77
validateBoolean,
88
validateObject,
9+
validateUrl,
910
} from 'simple-validators';
1011
import { validateProjectAndPageFrontmatterKeys } from '../project/validators.js';
1112
import { PAGE_FRONTMATTER_KEYS, type PageFrontmatter } from './types.js';

packages/myst-frontmatter/src/project/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const PROJECT_AND_PAGE_FRONTMATTER_KEYS = [
3535
'downloads',
3636
'settings', // We maybe want to move this into site frontmatter in the future
3737
'edit_url',
38+
'source_url',
3839
...KNOWN_EXTERNAL_IDENTIFIERS,
3940
// Do not add any project specific keys here!
4041
...SITE_FRONTMATTER_KEYS,
@@ -75,7 +76,9 @@ export type ProjectAndPageFrontmatter = SiteFrontmatter & {
7576
exports?: Export[];
7677
downloads?: Download[];
7778
settings?: ProjectSettings;
79+
/** URLs to edit or view the current page source - may be disabled by setting to null at project level */
7880
edit_url?: string | null;
81+
source_url?: string | null;
7982
};
8083

8184
export type ProjectFrontmatter = ProjectAndPageFrontmatter & {

packages/myst-frontmatter/src/project/validators.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ export function validateProjectAndPageFrontmatterKeys(
241241
} else if (defined(value.edit_url)) {
242242
output.edit_url = validateUrl(value.edit_url, incrementOptions('edit_url', opts));
243243
}
244+
if (value.source_url === null) {
245+
output.source_url = null;
246+
} else if (defined(value.source_url)) {
247+
output.source_url = validateUrl(value.source_url, incrementOptions('source_url', opts));
248+
}
244249
return output;
245250
}
246251

0 commit comments

Comments
 (0)