diff --git a/src/index.js b/src/index.js index e7435e1..b80f932 100644 --- a/src/index.js +++ b/src/index.js @@ -3,38 +3,54 @@ export default { { name: 'mfm', level: 'inline', - start(src) { return src.match(/\$\[/)?.index }, + start (src) { return src.match(/\$\[/)?.index }, tokenizer (src, tokens) { - const rule = /^\$\[([\w\d]+) (.+)\]$/ + const rule = /^\$\[(?[\w\d]+)(?:\.(?\S+))? (?.+)\]$/ const match = rule.exec(src) if (match) { const token = { type: 'mfm', raw: match[0], - markup: match[1], - text: match[2], - tokens: [] + markup: match.groups.markup, + options: match.groups.options, + text: match.groups.text, + tokens: [], } this.lexer.inline(token.text, token.tokens) return token } }, renderer (token) { - const el = tokenClass => `${this.parser.parseInline(token.tokens)}` + let options = {} + if (token.options) { + options = token.options.split(',').reduce((i, opt) => { + i[opt.split('=')[0]] = opt.split('=')[1] || true + return i + }, {}) + } + const el = (mfmClass, mfmStyle = '') => `${this.parser.parseInline(token.tokens)}` switch (token.markup) { - case 'x2': - return el('x2') - break - case 'twitch': - return el('twitch') - break - case 'sparkle': - return el('sparkle') - break - default: - return `$[${token.markup} ${this.parser.parseInline(token.tokens)}]` + case 'x2': { + return el('x2') + } + case 'twitch': { + const speed = options.speed || '0.5s' + return el('twitch', ` animation: mfm-twitch ${speed} ease infinite;`) + } + case 'sparkle': { + return el('sparkle') + } + case 'spin': { + const direction = options.left ? 'reverse' : options.alternate ? 'alternate' : 'normal' + const anime = options.x ? 'mfm-spinX' : options.y ? 'mfm-spinY' : 'mfm-spin' + const speed = options.speed || '1.5s' + return el('spin', ` animation: ${anime} ${speed} linear infinite; animation-direction: ${direction};`) + } + default: { + return `$[${token.markup} ${this.parser.parseInline(token.tokens)}]` + } } }, }, - ] + ], } diff --git a/test/index.test.js b/test/index.test.js index dc972bb..356a4fb 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -4,16 +4,26 @@ import markedMfm from '../src/index.js' describe('marked-mfm', () => { test('inline', () => { marked.use(markedMfm) - expect(marked('$[x2 this text is bigger]')).toBe('

this text is bigger

\n') + expect(marked('$[x2 this text is bigger]')).toBe('

this text is bigger

\n') }) test('nested', () => { marked.use(markedMfm) - expect(marked('$[twitch twitch $[sparkle sparkle]]')).toBe('

twitch sparkle

\n') + expect(marked('$[x2 x2 $[sparkle sparkle]]')).toBe('

x2 sparkle

\n') }) test('invalid', () => { marked.use(markedMfm) - expect(marked('$[invalid $[x2 test]]')).toBe('

$[invalid test]

\n') + expect(marked('$[invalid $[x2 test]]')).toBe('

$[invalid test]

\n') + }) + + test('one option', () => { + marked.use(markedMfm) + expect(marked('$[twitch.speed=2s test]')).toBe('

test

\n') + }) + + test('multiple options', () => { + marked.use(markedMfm) + expect(marked('$[spin.alternate,speed=0.5s test]')).toBe('

test

\n') }) })