-
Notifications
You must be signed in to change notification settings - Fork 290
Convert text styles between IRC/Discord #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ekmartin
merged 8 commits into
reactiflux:master
from
rahatarmanahmed:convert-formatting
Mar 29, 2017
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
31042c1
Added formatting conversions to messages. Text formatting like bold, …
Kurocon 43d5e27
Remove colors from IRC messages sent to Discord, as Discord does not …
Kurocon 0684668
Make the leading 0 in the color optional in the regex.
Kurocon cb35b49
Use irc-formatting for irc to discord formatting
rahatarmanahmed 336e87e
Use simple-markdown to format discord to irc
rahatarmanahmed bc71228
Add tests & catch reverse irc format
rahatarmanahmed 86fadd5
Fix IRC to discord formatting nested styles
rahatarmanahmed 200a0b9
Use es6+ features instead of lodash
rahatarmanahmed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import ircFormatting from 'irc-formatting'; | ||
import SimpleMarkdown from 'simple-markdown'; | ||
import colors from 'irc-colors'; | ||
|
||
function mdNodeToIRC(node) { | ||
let content = node.content; | ||
if (Array.isArray(content)) content = content.map(mdNodeToIRC).join(''); | ||
if (node.type === 'em') return colors.italic(content); | ||
if (node.type === 'strong') return colors.bold(content); | ||
if (node.type === 'u') return colors.underline(content); | ||
return content; | ||
} | ||
|
||
export function formatFromDiscordToIRC(text) { | ||
const markdownAST = SimpleMarkdown.defaultInlineParse(text); | ||
return markdownAST.map(mdNodeToIRC).join(''); | ||
} | ||
|
||
export function formatFromIRCToDiscord(text) { | ||
const blocks = ircFormatting.parse(text).map(block => ({ | ||
// Consider reverse as italic, some IRC clients use that | ||
...block, | ||
italic: block.italic || block.reverse | ||
})); | ||
let mdText = ''; | ||
|
||
for (let i = 0; i <= blocks.length; i += 1) { | ||
// Default to unstyled blocks when index out of range | ||
const block = blocks[i] || {}; | ||
const prevBlock = blocks[i - 1] || {}; | ||
|
||
// Add start markers when style turns from false to true | ||
if (!prevBlock.italic && block.italic) mdText += '*'; | ||
if (!prevBlock.bold && block.bold) mdText += '**'; | ||
if (!prevBlock.underline && block.underline) mdText += '__'; | ||
|
||
// Add end markers when style turns from true to false | ||
// (and apply in reverse order to maintain nesting) | ||
if (prevBlock.underline && !block.underline) mdText += '__'; | ||
if (prevBlock.bold && !block.bold) mdText += '**'; | ||
if (prevBlock.italic && !block.italic) mdText += '*'; | ||
|
||
mdText += block.text || ''; | ||
} | ||
|
||
return mdText; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* eslint-disable prefer-arrow-callback */ | ||
|
||
import chai from 'chai'; | ||
import { formatFromDiscordToIRC, formatFromIRCToDiscord } from '../lib/formatting'; | ||
|
||
chai.should(); | ||
|
||
describe('Formatting', () => { | ||
describe('Discord to IRC', () => { | ||
it('should convert bold markdown', () => { | ||
formatFromDiscordToIRC('**text**').should.equal('\x02text\x02'); | ||
}); | ||
|
||
it('should convert italic markdown', () => { | ||
formatFromDiscordToIRC('*text*').should.equal('\x16text\x16'); | ||
formatFromDiscordToIRC('_text_').should.equal('\x16text\x16'); | ||
}); | ||
|
||
it('should convert underline markdown', () => { | ||
formatFromDiscordToIRC('__text__').should.equal('\x1ftext\x1f'); | ||
}); | ||
|
||
it('should ignore strikethrough markdown', () => { | ||
formatFromDiscordToIRC('~~text~~').should.equal('text'); | ||
}); | ||
|
||
it('should convert nested markdown', () => { | ||
formatFromDiscordToIRC('**bold *italics***') | ||
.should.equal('\x02bold \x16italics\x16\x02'); | ||
}); | ||
}); | ||
|
||
describe('IRC to Discord', () => { | ||
it('should convert bold IRC format', () => { | ||
formatFromIRCToDiscord('\x02text\x02').should.equal('**text**'); | ||
}); | ||
|
||
it('should convert reverse IRC format', () => { | ||
formatFromIRCToDiscord('\x16text\x16').should.equal('*text*'); | ||
}); | ||
|
||
it('should convert italic IRC format', () => { | ||
formatFromIRCToDiscord('\x1dtext\x1d').should.equal('*text*'); | ||
}); | ||
|
||
it('should convert underline IRC format', () => { | ||
formatFromIRCToDiscord('\x1ftext\x1f').should.equal('__text__'); | ||
}); | ||
|
||
it('should ignore color IRC format', () => { | ||
formatFromIRCToDiscord('\x0306,08text\x03').should.equal('text'); | ||
}); | ||
|
||
it('should convert nested IRC format', () => { | ||
formatFromIRCToDiscord('\x02bold \x16italics\x16\x02') | ||
.should.equal('**bold *italics***'); | ||
}); | ||
|
||
it('should convert nested IRC format', () => { | ||
formatFromIRCToDiscord('\x02bold \x1funderline\x1f\x02') | ||
.should.equal('**bold __underline__**'); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When can
blocks[i]
ever beundefined
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah,
i <= blocks.length
noti < blocks.length
. Sneaky code.