Skip to content

Commit 2f16deb

Browse files
committed
Add fallback for missing branchName
1 parent 5450dcb commit 2f16deb

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/markdown_renderers.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import gitUrlParse from 'git-url-parse';
22
import * as utils from './utilities.client';
33

4+
const regex = require('./regexs');
5+
46
/**
57
* @param {string} headingId
68
*/
@@ -78,12 +80,15 @@ export const parseRepoName = (repoUrl) => {
7880
* Generates URLs for RAW content such as images
7981
*
8082
* @param {string} repoUrl
81-
* @param {string} branchName
83+
* @param {string} gitBranchName
8284
*/
83-
export const generateRawGitRepoUrlPrefix = (repoUrl, branchName) => {
85+
export const generateRawGitRepoUrlPrefix = (repoUrl, gitBranchName) => {
8486
let urlPrefix;
8587
const repoFullName = parseRepoName(repoUrl);
8688

89+
// Avoid errors created by missing branch name / badly formed URLs
90+
const branchName = gitBranchName || 'main';
91+
8792
if (repoUrl.includes('github.com')) {
8893
urlPrefix = `https://raw.githubusercontent.com/${repoFullName}/${branchName}`;
8994
} else if (repoUrl.includes('gitlab.com')) {
@@ -99,10 +104,10 @@ export const generateRawGitRepoUrlPrefix = (repoUrl, branchName) => {
99104
* Generates URLs for files and folders
100105
*
101106
* @param {string} repoUrl
102-
* @param {string} branchName
107+
* @param {string} gitBranchName
103108
* @param {string} href
104109
*/
105-
export const generateGitRepoUrlPrefix = (repoUrl, branchName, href) => {
110+
export const generateGitRepoUrlPrefix = (repoUrl, gitBranchName, href) => {
106111
let urlPrefix;
107112
const repoFullName = parseRepoName(repoUrl);
108113

@@ -113,6 +118,9 @@ export const generateGitRepoUrlPrefix = (repoUrl, branchName, href) => {
113118
// otherwise we assume the link is for a directory (tree)
114119
const isTreeOrBlob = lastHrefPart.includes('.') ? 'blob' : 'tree';
115120

121+
// Avoid errors created by missing branch name / badly formed URLs
122+
const branchName = gitBranchName || 'main';
123+
116124
if (repoUrl.includes('github.com')) {
117125
urlPrefix = `https://github.com/${repoFullName}/${isTreeOrBlob}/${branchName}`;
118126
} else if (repoUrl.includes('gitlab.com')) {
@@ -127,25 +135,27 @@ export const generateGitRepoUrlPrefix = (repoUrl, branchName, href) => {
127135
/**
128136
* Replaces relative links with absolute ones that point to the actor's git repo.
129137
* Mainly for use in actor READMES
130-
* Parses the actor's repo URL to extract the repo name and owner name.
138+
* The flow:
139+
* 1) handle anchors, Apify links, and contact links (these don't point to a git repo and shouldn't have rel=nofollow).
140+
* 2) handle relative links for the Git repo and convert them to absolute
141+
* 3) handle absolute links
131142
* @param {string} href
132143
* @param {string} text
133144
* @param {string} repoUrl
134-
* @param {string} branchName
145+
* @param {string} gitBranchName
135146
* @return {string}
136147
*/
137-
export const customLinkRenderer = (href, text, repoUrl, branchName) => {
148+
export const customLinkRenderer = (href, text, repoUrl, gitBranchName) => {
138149
// Handle anchor links, local Apify links, and mailto
139150
// Return Apify domain links without rel="nofollow" for SEO
140-
const contactLinkRegex = new RegExp('^(mailto|tel|sms):.*$', 'i');
141-
if (href.startsWith('#') || href.includes('apify.com') || contactLinkRegex.test(href)) {
151+
if (href.startsWith('#') || href.includes('apify.com') || regex.CONTACT_LINK_REGEX.test(href)) {
142152
// Ensure that anchors have lowercase href
143153
return `<a href="${href.toLowerCase()}">${text}</a>`;
144154
}
145155
// Only target relative URLs, which are used to refer to the git repo, and not anchors or absolute URLs
146156
const urlIsRelative = utils.isUrlRelative(href);
147157
if (urlIsRelative) {
148-
const urlPrefix = generateGitRepoUrlPrefix(repoUrl, branchName, href);
158+
const urlPrefix = generateGitRepoUrlPrefix(repoUrl, gitBranchName, href);
149159
// Since the README will always be in the root, the hrefs will have the same prefix, which needs to be taken off for the URL
150160
const cleanedHref = href.startsWith('./') ? href.replace('./', '') : href;
151161
href = `${urlPrefix}/${cleanedHref}`;

src/regexs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,6 @@ export const SPLIT_PATH_REGEX = /[^/]+/g;
110110

111111
// Check if a URL is relative, i.e. does not start with a protocol
112112
export const RELATIVE_URL_REGEX = new RegExp('^(?!www.|(?:http|ftp)s?://|[A-Za-z]:\\|//).*', 'i');
113+
114+
// Check if a link is a mailto/tel/sms type
115+
export const CONTACT_LINK_REGEX = new RegExp('^(mailto|tel|sms):.*$', 'i');

0 commit comments

Comments
 (0)