Skip to content

Commit 4786949

Browse files
committedOct 18, 2024·
fix: correct conversion of standalone PR, issue, and changelog URLs to markdown format
resolves #38
1 parent 9737dc9 commit 4786949

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed
 

‎index.js

+16-20
Original file line numberDiff line numberDiff line change
@@ -48,33 +48,29 @@ const reduceHeadings = (text) => text
4848
.replace(/^##\s+(.+)$/gm, '**$1**'); // Convert H2 to bold
4949

5050
/**
51-
* Converts PR links, issue links, and changelog links to markdown format.
51+
* Converts PR, issue, and changelog links to markdown format, ignoring existing markdown links.
5252
* - PR links: `https://github.com/OWNER/REPO/pull/1` -> `[PR #1](https://github.com/OWNER/REPO/pull/1)`
5353
* - Issue links: `https://github.com/OWNER/REPO/issues/1` -> `[Issue #30](https://github.com/OWNER/REPO/issues/1)`
5454
* - 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)`
5555
* @param {string} text The input text.
5656
* @returns {string} The text with links converted to markdown format.
5757
*/
5858
const convertLinksToMarkdown = (text) => {
59-
// Convert PR links
60-
text = text.replace(
61-
/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/pull\/(\d+)/g,
62-
(match, owner, repo, prNumber) => `[PR #${prNumber}](${match})`
63-
);
64-
65-
// Convert issue links
66-
text = text.replace(
67-
/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/issues\/(\d+)/g,
68-
(match, owner, repo, issueNumber) => `[Issue #${issueNumber}](${match})`
69-
);
70-
71-
// Convert changelog comparison links
72-
text = text.replace(
73-
/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/compare\/([v\w.-]+)\.\.\.([v\w.-]+)/g,
74-
(match, owner, repo, fromVersion, toVersion) => `[${fromVersion}...${toVersion}](${match})`
75-
);
76-
77-
return text;
59+
// Extract existing markdown links and replace them with placeholders
60+
const markdownLinks = [];
61+
const textWithoutMarkdownLinks = text.replace(/\[.*?\]\(.*?\)/g, (link) => {
62+
markdownLinks.push(link);
63+
return `__MARKDOWN_LINK_PLACEHOLDER_${markdownLinks.length - 1}__`;
64+
});
65+
66+
// Convert standalone PR, issue, and changelog URLs to markdown format
67+
let processedText = textWithoutMarkdownLinks
68+
.replace(/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/pull\/(\d+)/g, (match, owner, repo, prNumber) => `[PR #${prNumber}](${match})`)
69+
.replace(/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/issues\/(\d+)/g, (match, owner, repo, issueNumber) => `[Issue #${issueNumber}](${match})`)
70+
.replace(/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/compare\/([v\w.-]+)\.\.\.([v\w.-]+)/g, (match, owner, repo, fromVersion, toVersion) => `[${fromVersion}...${toVersion}](${match})`);
71+
72+
// Reinsert the original markdown links
73+
return processedText.replace(/__MARKDOWN_LINK_PLACEHOLDER_(\d+)__/g, (match, index) => markdownLinks[parseInt(index, 10)]);
7874
};
7975

8076
/**

0 commit comments

Comments
 (0)
Please sign in to comment.