-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
20 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}`); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters