diff --git a/steps/slack-notify/slackNotify.js b/steps/slack-notify/slackNotify.js index 05a66af..b618a9a 100644 --- a/steps/slack-notify/slackNotify.js +++ b/steps/slack-notify/slackNotify.js @@ -13,6 +13,8 @@ const markdownToMrkdwn = (text) => { text // Convert links: [text](url) -> .replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<$2|$1>') + // Convert bold: **text** -> *text* (mrkdwn renders ** literally) + .replace(/\*\*(?=\S)([^*\n]+?)(?<=\S)\*\*/g, '*$1*') // Convert headers: ## Header -> *Header* .replace(/^#{1,6}\s+(.+)$/gm, '*$1*') // Convert list items: - item -> • item @@ -77,6 +79,8 @@ const sendMessage = (token, channel, text, threadTs, updateTs) => req.end(); }); +module.exports = { isValidSlackTs, markdownToMrkdwn }; + // Standalone runner if (require.main === module) { const token = process.env.SLACK_BOT_TOKEN; diff --git a/steps/slack-notify/slackNotify.test.js b/steps/slack-notify/slackNotify.test.js new file mode 100644 index 0000000..c7b4dec --- /dev/null +++ b/steps/slack-notify/slackNotify.test.js @@ -0,0 +1,19 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { markdownToMrkdwn } = require('./slackNotify'); + +test('converts markdown bold to mrkdwn and leaves mrkdwn bold alone', () => { + assert.equal( + markdownToMrkdwn('⚠️ **Contains DB migrations** — roll-forward only'), + '⚠️ *Contains DB migrations* — roll-forward only', + ); + assert.equal(markdownToMrkdwn('*Release — Started*'), '*Release — Started*'); + assert.equal(markdownToMrkdwn('a ** b ** c'), 'a ** b ** c'); +}); + +test('converts links, headers and list items', () => { + assert.equal( + markdownToMrkdwn('## Features\n- [APP-1: title](https://x.y/z)'), + '*Features*\n• ', + ); +});