Skip to content

Commit

Permalink
fix: Avoid truncating in the middle of markdown (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
BeyondEvil authored and juliuscc committed Nov 3, 2019
1 parent acec2b5 commit bc373f9
Show file tree
Hide file tree
Showing 6 changed files with 583 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ jobs:
- run:
name: Lint test
command: npm run lint
- run:
name: Run tests
command: npm test
deploy:
docker:
- image: circleci/node:10.5.0
Expand Down
6 changes: 2 additions & 4 deletions lib/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const slackifyMarkdown = require('slackify-markdown')
const postMessage = require('./postMessage')
const template = require('./template')
const truncate = require('./truncate')

// Max lenght of message to the slack api is 3000 characters. Make sure we are below that.
const MAX_LENGTH = 2900
Expand Down Expand Up @@ -57,10 +58,7 @@ module.exports = async (pluginConfig, context) => {

if (nextRelease.notes !== '') {
// truncate long messages
let messageText =
nextRelease.notes.length > MAX_LENGTH
? nextRelease.notes.substring(0, MAX_LENGTH) + '*[...]*'
: nextRelease.notes
let messageText = truncate(nextRelease.notes, MAX_LENGTH)

if (pluginConfig.markdownReleaseNotes)
messageText = slackifyMarkdown(messageText)
Expand Down
14 changes: 14 additions & 0 deletions lib/truncate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = (messageText, maxLength) => {
const delimiter = '\n'
if (messageText.length > maxLength) {
messageText = messageText.substring(0, maxLength).split(delimiter)
// if no newlines, we don't remove anything, we keep the truncated message as is
if (messageText.length > 1) {
// remove all text after the last newline in the truncated message
// this avoids truncating in the middle of markdown
messageText.splice(-1, 1)
}
messageText = messageText.join(delimiter) + '*[...]*'
}
return messageText
}
Loading

0 comments on commit bc373f9

Please sign in to comment.