diff --git a/lib/SymMessageParser/index.js b/lib/SymMessageParser/index.js index 76078b0..1519a75 100644 --- a/lib/SymMessageParser/index.js +++ b/lib/SymMessageParser/index.js @@ -2,14 +2,12 @@ var SymBotAuth = require('../SymBotAuth') var SymMessageParser = {} -const regex = /()(.*)(<\/div>)/g const regexTagStart = /<\w+>/g const regexTagEnd = /<\/\w+>/g const HASHTAG_TYPE = 'org.symphonyoss.taxonomy.hashtag' const CASHTAG_TYPE = 'org.symphonyoss.fin.security.id.ticker' const MENTION_TYPE = 'com.symphony.user.userId' -const EntityRegex = 'id=\"\\d+\">(.*?)<' SymMessageParser.parse = events => { const arrParsedMessages = [] @@ -17,16 +15,10 @@ SymMessageParser.parse = events => { try { events.forEach(event => { if (event.type === 'MESSAGESENT' && event.initiator.user.userId !== SymBotAuth.botUser.id) { - let match const message = event.payload.messageSent.message - while ((match = regex.exec(message.message)) !== null) { - if (match.index === regex.lastIndex) { - regex.lastIndex++ - } - let textMessage = match[2].replace(regexTagStart, '') - textMessage = textMessage.replace(regexTagEnd, '') - message.messageText = textMessage - } + + message.messageText = extractTextFromPresentationML(message.message) + arrParsedMessages.push(message) } }) @@ -43,6 +35,25 @@ SymMessageParser.parse = events => { return arrParsedMessages } +function extractTextFromPresentationML(presentationMl) { + const trimmedPresentationMl = presentationMl.trim() + + let indexOfFirstDivTag = trimmedPresentationMl.indexOf('', indexOfFirstDivTag) // index of first
tag + + let lastIndex = trimmedPresentationMl.lastIndexOf('
') // index of last tag + if(lastIndex == -1) { + return undefined + } + + let insideDiv = trimmedPresentationMl.substring(firstIndex + 1, lastIndex) // extract what is inside
+ return insideDiv.replace(regexTagStart, '').replace(regexTagEnd, '') //remove inside tags and extract text +} + function getTags (message, type) { const arrTags = [] diff --git a/tests/SymMessageParser/index.test.js b/tests/SymMessageParser/index.test.js new file mode 100644 index 0000000..af7fa98 --- /dev/null +++ b/tests/SymMessageParser/index.test.js @@ -0,0 +1,96 @@ +const SymMessageParser = require('../../lib/SymMessageParser'); + +describe('Message parsing', () => { + beforeAll(() => { + const SymBotAuth = require('../../lib/SymBotAuth') + SymBotAuth.botUser = { id: -1}; + }); + + test('invalid opening tag', () => { + assertMessageParsed('Hello, World', + 'Hello, World'); + }); + + test('no opening tag', () => { + assertMessageParsed(' data-format="PresentationML" data-version="2.0">Hello, World', + undefined); + }); + + test('no closing tag', () => { + assertMessageParsed('
Hello, World', + undefined); + }); + + test('simple message', () => { + assertMessageParsed('
Hello, World
', + 'Hello, World'); + }); + + test('simple message with spaces around div', () => { + assertMessageParsed('
Hello, World
', + 'Hello, World'); + }); + + test('message enclosed in markups', () => { + assertMessageParsed('
Hello, World
', + 'Hello, World'); + }); + + test('message enclosed in div markups', () => { + assertMessageParsed('
Hello, World
', + 'Hello, World'); + }); + + test('message enclosed in markups with spaces around div', () => { + assertMessageParsed('
Hello, World
', + 'Hello, World'); + }); + + test('message partially enclosed in markups', () => { + assertMessageParsed('
Hello wonderful world
', + 'Hello wonderful world'); + }); + + test('message partially enclosed in div markups', () => { + assertMessageParsed('
Hello
wonderful
world
', + 'Hello wonderful world'); + }); + + test('message partially enclosed in markups with spaces around div', () => { + assertMessageParsed('
Hello wonderful world
', + 'Hello wonderful world'); + }); + + test('message with several levels of markups', () => { + assertMessageParsed( + '
Hello wonderful and beautiful world
', + 'Hello wonderful and beautiful world'); + }); + + test('message with several levels of markups, innermost div', () => { + assertMessageParsed( + '
Hello wonderful
and
beautiful
world
', + 'Hello wonderful and beautiful world'); + }); + + test('message with several levels of markups with spaces around div', () => { + assertMessageParsed( + '
Hello wonderful and beautiful world
', + 'Hello wonderful and beautiful world'); + }); +}); + +function assertMessageParsed(incomingMessage, expectedParsedMessage) { + const event = { + type: 'MESSAGESENT', + initiator: {user: {id: 1234}}, + payload: {messageSent: {message: {message: incomingMessage}}} + }; + + let parsedMessages = SymMessageParser.parse([event]); + + expect(Array.isArray(parsedMessages)).toBeTruthy(); + expect(parsedMessages.length).toBe(1); + + expect(parsedMessages[0].messageText).toBe(expectedParsedMessage); +}