-
Notifications
You must be signed in to change notification settings - Fork 292
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
Convert text styles between IRC/Discord #205
Changes from 7 commits
31042c1
43d5e27
0684668
cb35b49
336e87e
bc71228
86fadd5
200a0b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
import _ from 'lodash'; | ||
|
||
function mdNodeToIRC(node) { | ||
let content = node.content; | ||
if (_.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 | ||
_.assign({}, block, { italic: block.italic || block.reverse }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The package has a Node >6 requirement anyway, since discord.js enforces that, so could do {
...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] || {}; | ||
Comment on lines
+27
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When can There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, |
||
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; | ||
} |
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__**'); | ||
}); | ||
}); | ||
}); |
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.
Could just use
Array.isArray
I guess?