Skip to content

Commit ef7b78f

Browse files
authored
Merge pull request #45 from primer/update-repo-root-path
Simplify repoRootPath option
2 parents 7a86447 + 28684a5 commit ef7b78f

4 files changed

Lines changed: 15 additions & 15 deletions

File tree

docs/content/usage/customization.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ _Note: Doctocat only supports one level of nesting._
4141
4242
## Edit on GitHub link
4343
44-
To add an "Edit this page on GitHub" link to the bottom of every page, add a [`repository`](https://docs.npmjs.com/files/package.json#repository) field in `package.json` and a `repoRootPath` theme option in `gatsby-config.js`:
44+
To add an "Edit this page on GitHub" link to the bottom of every page, you'll need to specify your site's [`repository`](https://docs.npmjs.com/files/package.json#repository) in `package.json`:
4545

4646
```json
4747
// package.json
@@ -50,22 +50,22 @@ To add an "Edit this page on GitHub" link to the bottom of every page, add a [`r
5050
}
5151
```
5252

53+
If your site is located in a subdirectory of a repository, you'll also need to provide the relative path to the root of your git respository via the `repoRootPath` option in `gatsby-config.js`:
54+
5355
```js
5456
// gatsby-config.js
5557
module.exports = {
5658
plugins: [
5759
{
5860
resolve: '@primer/gatsby-theme-doctocat',
5961
options: {
60-
repoRootPath: process.cwd(),
62+
repoRootPath: '..', // defaults to '.'
6163
},
6264
},
6365
],
6466
}
6567
```
6668

67-
The `repoRootPath` option specifies the absolute path to the root of your git repository. If your `gatsby-config.js` is located at the root of your git respository, set `repoRootPath` to [`process.cwd()`](https://nodejs.org/api/process.html#process_process_cwd). If your `gatsby-config.js` is located in a subdirectory, you can use [`path.resolve()`](https://nodejs.org/docs/latest/api/path.html#path_path_resolve_paths), `process.cwd()`, and the relative path to the root of your git repository to compute the absolute path. For example, if your `gatsby-config.js` is located in a `docs` directory set `repoRootpath` to `path.resolve(process.cwd(), '..')`.
68-
6969
## Hero
7070

7171
To add a hero section to a page, you'll need to override the default layout component with Doctocat's `HeroLayout` component by exporting it from your MDX file:

docs/gatsby-config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const path = require('path')
2-
31
module.exports = {
42
siteMetadata: {
53
title: 'Doctocat',
@@ -10,7 +8,7 @@ module.exports = {
108
{
119
resolve: '@primer/gatsby-theme-doctocat',
1210
options: {
13-
repoRootPath: path.resolve(process.cwd(), '../..'),
11+
repoRootPath: '..',
1412
},
1513
},
1614
],

theme/gatsby-node.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ const getRepo = memoize(() => {
77
return getPkgRepo(readPkgUp.sync().package)
88
})
99

10-
function generateEditUrl(rootPath, absolutePath) {
10+
function generateEditUrl(rootAbsolutePath, fileAbsolutePath) {
1111
try {
1212
const {domain, user, project} = getRepo()
13-
const relativePath = path.relative(rootPath, absolutePath)
14-
return `https://${domain}/${user}/${project}/edit/master/${relativePath}`
13+
const fileRelativePath = path.relative(rootAbsolutePath, fileAbsolutePath)
14+
return `https://${domain}/${user}/${project}/edit/master/${fileRelativePath}`
1515
} catch (error) {
1616
console.warn(
17-
`[warning] An edit url could not be generated for ${absolutePath}`,
17+
`[warning] An edit url could not be generated for ${fileAbsolutePath}`,
1818
)
1919
return null
2020
}
@@ -44,11 +44,13 @@ exports.createPages = async ({graphql, actions}, themeOptions) => {
4444
node.parent.name === 'index' ? '/' : node.parent.name,
4545
)
4646

47-
const editUrl = generateEditUrl(
48-
themeOptions.repoRootPath,
49-
node.fileAbsolutePath,
47+
const rootAbsolutePath = path.resolve(
48+
process.cwd(),
49+
themeOptions.repoRootPath || '.',
5050
)
5151

52+
const editUrl = generateEditUrl(rootAbsolutePath, node.fileAbsolutePath)
53+
5254
actions.createPage({
5355
path: pagePath,
5456
component: node.fileAbsolutePath,

theme/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@primer/gatsby-theme-doctocat",
3-
"version": "0.8.0",
3+
"version": "0.9.0",
44
"main": "index.js",
55
"license": "MIT",
66
"devDependencies": {

0 commit comments

Comments
 (0)