Skip to content

Commit

Permalink
fix: correct conversion of standalone PR, issue, and changelog URLs t…
Browse files Browse the repository at this point in the history
…o markdown format

resolves #38
  • Loading branch information
SethCohen committed Oct 18, 2024
1 parent 9737dc9 commit 4786949
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,29 @@ const reduceHeadings = (text) => text
.replace(/^##\s+(.+)$/gm, '**$1**'); // Convert H2 to bold

/**
* Converts PR links, issue links, and changelog links to markdown format.
* Converts PR, issue, and changelog links to markdown format, ignoring existing markdown links.
* - 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;
// Extract existing markdown links and replace them with placeholders
const markdownLinks = [];
const textWithoutMarkdownLinks = text.replace(/\[.*?\]\(.*?\)/g, (link) => {
markdownLinks.push(link);
return `__MARKDOWN_LINK_PLACEHOLDER_${markdownLinks.length - 1}__`;
});

// Convert standalone PR, issue, and changelog URLs to markdown format
let processedText = textWithoutMarkdownLinks
.replace(/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/pull\/(\d+)/g, (match, owner, repo, prNumber) => `[PR #${prNumber}](${match})`)
.replace(/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/issues\/(\d+)/g, (match, owner, repo, issueNumber) => `[Issue #${issueNumber}](${match})`)
.replace(/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/compare\/([v\w.-]+)\.\.\.([v\w.-]+)/g, (match, owner, repo, fromVersion, toVersion) => `[${fromVersion}...${toVersion}](${match})`);

// Reinsert the original markdown links
return processedText.replace(/__MARKDOWN_LINK_PLACEHOLDER_(\d+)__/g, (match, index) => markdownLinks[parseInt(index, 10)]);
};

/**
Expand Down

0 comments on commit 4786949

Please sign in to comment.