|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { posix: path } = require('path') |
| 4 | + |
| 5 | +module.exports = (page) => { |
| 6 | + const HOSTED_GIT_REPO_RX = /^(?:https?:\/\/|.+@)(git(?:hub|lab)\.com|bitbucket\.org|pagure\.io)[/:](.+?)(?:\.git)?$/ |
| 7 | + const repoPage = page.attributes['edit-url-repo-action'] || 'issues' |
| 8 | + const repoExtra = repoPage === 'issues' ? 'new' : '' |
| 9 | + const feedbackTitle = page.attributes['edit-url-title'] || 'Docs Feedback' |
| 10 | + const feedbackBody = page.attributes['edit-url-body'] || '> Do not include confidential information, personal data, sensitive data, or other regulated data.' |
| 11 | + const feedbackLabels = page.attributes['edit-url-labels'] || '' |
| 12 | + let url = page.attributes['edit-url-uri'] || page.editUrl |
| 13 | + |
| 14 | + // text for the link is based on the edit-url-text attribute, or page theme |
| 15 | + const text = (page.attributes && page.attributes['edit-url-text']) |
| 16 | + ? page.attributes['edit-url-text'] |
| 17 | + : (page.attributes && page.attributes.theme === 'docs') |
| 18 | + ? 'Raise an issue' |
| 19 | + : 'Edit this page' |
| 20 | + |
| 21 | + // url for docs can be derived from page.editUrl |
| 22 | + // and updated to link to the repo issues page |
| 23 | + // for other themes, page.editUrl is used |
| 24 | + const match = url.match(HOSTED_GIT_REPO_RX) |
| 25 | + if (page.attributes && page.attributes.theme === 'docs' && match) { |
| 26 | + const editDetails = match[2].split('/') |
| 27 | + const issueParts = { |
| 28 | + title: { |
| 29 | + text: feedbackTitle, |
| 30 | + path: editDetails.slice(4) ? path.join(...editDetails.slice(4)) : '', |
| 31 | + ref: editDetails[3] ? `(ref: ${editDetails[3]})` : '', |
| 32 | + }, |
| 33 | + query: { |
| 34 | + body: feedbackBody !== '' ? feedbackBody : '', |
| 35 | + labels: feedbackLabels !== '' ? feedbackLabels : '', |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + // construct issue content |
| 40 | + const issueTitle = '?title=' + Object.values(issueParts.title).join(' ') |
| 41 | + |
| 42 | + // construct query params |
| 43 | + const issueQuery = Object.keys(issueParts.query).map((key) => { |
| 44 | + if (issueParts.query[key] !== '') return `&${key}=${issueParts.query[key]}` |
| 45 | + }).join('') |
| 46 | + |
| 47 | + url = 'https://' + path.join(match[1], editDetails[0], editDetails[1], repoPage, repoExtra, issueTitle + issueQuery) |
| 48 | + } |
| 49 | + |
| 50 | + return { |
| 51 | + text: text, |
| 52 | + url: encodeURI(url), |
| 53 | + } |
| 54 | +} |
0 commit comments