UNPKG

myst-cli

Version:
35 lines (34 loc) 1.41 kB
import which from 'which'; import { makeExecutable, silentLogger } from 'myst-cli-utils'; function gitCommandAvailable() { 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, frontmatter, file) { if (!frontmatter.github) return; if (frontmatter.edit_url || frontmatter.edit_url === null) return; if (!gitCommandAvailable()) return; try { const gitLog = silentLogger(); 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 (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}`); } }