Skip to content

Commit

Permalink
Merge branch 'develop' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
marihachi committed Apr 18, 2021
2 parents 027edb4 + 86cbc7c commit 0039f42
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mfm-js",
"version": "0.15.1",
"version": "0.16.0",
"description": "An MFM parser implementation with PEG.js",
"main": "./built/index.js",
"types": "./built/index.d.ts",
Expand Down
83 changes: 57 additions & 26 deletions src/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
//

fullParser
= nodes:(&. n:(block / inline) { return n; })* { return mergeText(nodes); }
= nodes:(&. n:full { return n; })* { return mergeText(nodes); }

plainParser
= nodes:(&. n:(emojiCode / unicodeEmoji / plainText) { return n; })* { return mergeText(nodes); }
Expand All @@ -72,19 +72,60 @@ inlineParser
= nodes:(&. n:inline { return n; })* { return mergeText(nodes); }

//
// block rules
// syntax list
//

block
= quote
/ search
/ codeBlock
/ mathBlock
/ center
full
= quote // block
/ codeBlock // block
/ mathBlock // block
/ center // block
/ emojiCode
/ unicodeEmoji
/ big
/ bold
/ small
/ italic
/ strike
/ inlineCode
/ mathInline
/ mention
/ hashtag
/ url
/ fnVer2
/ link
/ fnVer1
/ search // block
/ inlineText

inline
= emojiCode
/ unicodeEmoji
/ big
/ bold
/ small
/ italic
/ strike
/ inlineCode
/ mathInline
/ mention
/ hashtag
/ url
/ fnVer2
/ link
/ fnVer1
/ inlineText

//
// block rules
//

// block: quote

quote
= &(BEGIN ">") q:quoteInner { return q; }

quoteInner
= head:quoteMultiLine tails:quoteMultiLine+
{
const children = applyParser([head, ...tails].join('\n'), 'fullParser');
Expand Down Expand Up @@ -161,23 +202,6 @@ center
// inline rules
//

inline
= emojiCode
/ unicodeEmoji
/ big
/ bold
/ small
/ italic
/ strike
/ inlineCode
/ mathInline
/ mention
/ hashtag
/ url
/ link
/ fn
/ inlineText

// inline: emoji code

emojiCode
Expand Down Expand Up @@ -370,13 +394,20 @@ linkUrl

// inline: fn

fn
fnVer1
= "[" name:$([a-z0-9_]i)+ args:fnArgs? _ content:(!"]" i:inline { return i; })+ "]"
{
args = args || {};
return FN(name, args, mergeText(content));
}

fnVer2
= "$[" name:$([a-z0-9_]i)+ args:fnArgs? _ content:(!"]" i:inline { return i; })+ "]"
{
args = args || {};
return FN(name, args, mergeText(content));
}

fnArgs
= "." head:fnArg tails:("," arg:fnArg { return arg; })*
{
Expand Down
48 changes: 47 additions & 1 deletion test/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ describe('FullParser', () => {
});
});

describe('fn', () => {
describe('fn v1', () => {
it('basic', () => {
const input = '[tada abc]';
const output = [
Expand All @@ -641,6 +641,52 @@ describe('FullParser', () => {
];
assert.deepStrictEqual(mfm.parse(input), output);
});

it('nest', () => {
const input = '[spin.speed=1.1s [shake a]]';
const output = [
FN('spin', { speed: '1.1s' }, [
FN('shake', { }, [
TEXT('a')
])
])
];
assert.deepStrictEqual(mfm.parse(input), output);
});
});

describe('fn v2', () => {
it('basic', () => {
const input = '$[tada abc]';
const output = [
FN('tada', { }, [
TEXT('abc')
])
];
assert.deepStrictEqual(mfm.parse(input), output);
});

it('with a string argument', () => {
const input = '$[spin.speed=1.1s a]';
const output = [
FN('spin', { speed: '1.1s' }, [
TEXT('a')
])
];
assert.deepStrictEqual(mfm.parse(input), output);
});

it('nest', () => {
const input = '$[spin.speed=1.1s $[shake a]]';
const output = [
FN('spin', { speed: '1.1s' }, [
FN('shake', { }, [
TEXT('a')
])
])
];
assert.deepStrictEqual(mfm.parse(input), output);
});
});

it('composite', () => {
Expand Down

0 comments on commit 0039f42

Please sign in to comment.