Skip to content

Commit

Permalink
feat: add function to convert PR, issue, and changelog links to markd…
Browse files Browse the repository at this point in the history
…own format

resolves #32
  • Loading branch information
SethCohen committed Oct 18, 2024
1 parent f184bc5 commit 07c2e1c
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,36 @@ const reduceHeadings = (text) => text
.replace(/^###\s+(.+)$/gm, '**__$1__**') // Convert H3 to bold + underline
.replace(/^##\s+(.+)$/gm, '**$1**'); // Convert H2 to bold

/**
* Converts PR links, issue links, and changelog links to markdown format.
* - PR links: `https://github.com/OWNER/REPO/pull/1` -> `[PR #1](https://github.com/OWNER/REPO/pull/1)`
* - Issue links: `https://github.com/OWNER/REPO/issues/1` -> `[Issue #30](https://github.com/OWNER/REPO/issues/1)`
* - Changelog links: `https://github.com/OWNER/REPO/compare/v1.0.0...v1.1.0` -> `[v1.0.0...v1.1.0](https://github.com/OWNER/REPO/compare/v1.0.0...v1.1.0)`
* @param {string} text The input text.
* @returns {string} The text with links converted to markdown format.
*/
const convertLinksToMarkdown = (text) => {
// Convert PR links
text = text.replace(
/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/pull\/(\d+)/g,
(match, owner, repo, prNumber) => `[PR #${prNumber}](${match})`
);

// Convert issue links
text = text.replace(
/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/issues\/(\d+)/g,
(match, owner, repo, issueNumber) => `[Issue #${issueNumber}](${match})`
);

// Convert changelog comparison links
text = text.replace(
/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/compare\/([v\w.-]+)\.\.\.([v\w.-]+)/g,
(match, owner, repo, fromVersion, toVersion) => `[${fromVersion}...${toVersion}](${match})`
);

return text;
};

/**
* Stylizes a markdown body into an appropriate embed message style.
* @param {string} description The description to format.
Expand All @@ -57,6 +87,7 @@ const formatDescription = (description) => {
edit = removeHTMLComments(edit);
edit = reduceNewlines(edit);
edit = convertMentionsToLinks(edit);
edit = convertLinksToMarkdown(edit);
edit = edit.trim();

if (core.getBooleanInput('reduce_headings')) {
Expand Down

0 comments on commit 07c2e1c

Please sign in to comment.