fix: correct conversion of standalone PR, issue, and changelog URLs to markdown format

resolves #38
This commit is contained in:
SethCohen
2024-10-18 00:02:43 -04:00
parent 9737dc9002
commit 47869497ed
+14 -18
View File
@@ -48,7 +48,7 @@ const reduceHeadings = (text) => text
.replace(/^##\s+(.+)$/gm, '**$1**'); // Convert H2 to bold .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)` * - 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)` * - 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)` * - 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)`
@@ -56,25 +56,21 @@ const reduceHeadings = (text) => text
* @returns {string} The text with links converted to markdown format. * @returns {string} The text with links converted to markdown format.
*/ */
const convertLinksToMarkdown = (text) => { const convertLinksToMarkdown = (text) => {
// Convert PR links // Extract existing markdown links and replace them with placeholders
text = text.replace( const markdownLinks = [];
/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/pull\/(\d+)/g, const textWithoutMarkdownLinks = text.replace(/\[.*?\]\(.*?\)/g, (link) => {
(match, owner, repo, prNumber) => `[PR #${prNumber}](${match})` markdownLinks.push(link);
); return `__MARKDOWN_LINK_PLACEHOLDER_${markdownLinks.length - 1}__`;
});
// Convert issue links // Convert standalone PR, issue, and changelog URLs to markdown format
text = text.replace( let processedText = textWithoutMarkdownLinks
/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/issues\/(\d+)/g, .replace(/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/pull\/(\d+)/g, (match, owner, repo, prNumber) => `[PR #${prNumber}](${match})`)
(match, owner, repo, issueNumber) => `[Issue #${issueNumber}](${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})`);
// Convert changelog comparison links // Reinsert the original markdown links
text = text.replace( return processedText.replace(/__MARKDOWN_LINK_PLACEHOLDER_(\d+)__/g, (match, index) => markdownLinks[parseInt(index, 10)]);
/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/compare\/([v\w.-]+)\.\.\.([v\w.-]+)/g,
(match, owner, repo, fromVersion, toVersion) => `[${fromVersion}...${toVersion}](${match})`
);
return text;
}; };
/** /**