Skip to content

Commit

Permalink
Moved JIRA formatting to external library
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunburdick committed May 22, 2016
1 parent 68eb770 commit ba9f37e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 85 deletions.
86 changes: 2 additions & 84 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const JiraApi = require('jira-client');
const Botkit = require('botkit');
const moment = require('moment');
const J2S = require('jira2slack');
const logger = require('./logger')();

const RESPONSE_FULL = 'full';
Expand Down Expand Up @@ -149,90 +150,7 @@ class Bot {
*/
formatIssueDescription (description) {
const desc = description || 'Ticket does not contain a description';
let depths = [];
let lastDepth = 0;
return desc
// Quotes
.replace(/\{(quote)\}/g, '```')

// Stolen from: https://github.com/kylefarris/J2M
// Un-ordered Lists
.replace(/^[ \t]*(\*+)\s+/gm, (match, stars) => `${Array(stars.length).join(' ')}• `)
.replace(/^\-\s+/gm, () => '• ')

// Ordered lists
.replace(/^[ \t]*(#+)\s+/gm, (match, nums) => {
const curDepth = nums.length - 1;
if (curDepth === lastDepth) {
depths[curDepth] = depths[curDepth] !== undefined ? depths[curDepth] + 1 : 1;
} else if (curDepth > lastDepth) {
depths[curDepth] = 1;
} else if (curDepth < lastDepth) {
depths = depths.slice(0, curDepth + 1);
depths[curDepth]++;
}

lastDepth = curDepth;
return `${Array(curDepth + 1).join(' ')}${depths[curDepth]}. `;
})

// Headers 1-6
.replace(/^h([0-6])\.(\s*)(.*)$/gm,
(match, level, spacing, content) => `\n${spacing}*${content}*\n`)

// Bold
.replace(/\*(\s*)(\S.*?\S)(\s*)\*/g, '$1*$2*$3')

// Italic
.replace(/_(\s*)(\S.*?\S)(\s*)_/g, '$1_$2_$3')

// Monospaced text
.replace(/\{\{([^}]+)\}\}/g, '`$1`')

// Citations
.replace(/\?\?((?:.[^?]|[^?].)+)\?\?/g, '_-- $1_')

// Inserts (ignored as unsupported)
// .replace(/\+([^+]*)\+/g, '<ins>$1</ins>')

// Superscript
.replace(/\^([^^]*)\^/g, '^$1')

// Subscript
.replace(/~([^~]*)~/g, '_$1')

// Strikethrough
.replace(/((\W)\-|(^)\-)( *)(\S.*?\S)( *)(\-(\W)|\-($))/gm, '$2$3$4~$5~$6$8')

// Code Block
.replace(/\{code(:([a-z]+))?\}([^]*)\{code\}/gm, '```$2$3```')

// Pre-formatted text
.replace(/{noformat}/g, '```')

// Un-named Links
.replace(/\[([^|]+)\]/g, '<$1>')

// Named Links
.replace(/\[(.+?)\|(.+)\]/g, '<$2|$1>')

// Single Paragraph Blockquote
.replace(/^bq\.\s+/gm, '> ')

// Remove color: unsupported in md
.replace(/\{color:[^}]+\}([^]*)\{color\}/gm, '$1')

// panel into table
.replace(/\{panel:title=([^}]*)\}\n?([^]*?)\n?\{panel\}/gm, '\n| $1 |\n| --- |\n| $2 |')

// table header
.replace(/^[ \t]*((?:\|\|.*?)+\|\|)[ \t]*$/gm, (match, headers) => {
const singleBarred = headers.replace(/\|\|/g, '|');
return `\n${singleBarred}\n${singleBarred.replace(/\|[^|]+/g, '| --- ')}`;
})

// remove leading-space of table headers and rows
.replace(/^[ \t]*\|/gm, '|');
return J2S.toSlack(desc);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "slack-jirabot",
"version": "2.3.0",
"version": "2.3.1",
"description": "Slackbot for interacting with JIRA",
"main": "app.js",
"private": true,
Expand All @@ -24,6 +24,7 @@
"dependencies": {
"botkit": "^0.1.1",
"jira-client": "^3.0.2",
"jira2slack": "^0.1.0",
"moment": "^2.10.3",
"redact-object": "^1.0.1",
"winston": "^2.1.1"
Expand Down

0 comments on commit ba9f37e

Please sign in to comment.