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 24, 2021
2 parents 16fb2a4 + 64d0429 commit 15c2ad5
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 55 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.16.2",
"version": "0.16.3",
"description": "An MFM parser implementation with PEG.js",
"main": "./built/index.js",
"types": "./built/index.d.ts",
Expand Down
85 changes: 64 additions & 21 deletions src/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fullParser
= nodes:(&. n:full { return n; })* { return mergeText(nodes); }

plainParser
= nodes:(&. n:(emojiCode / unicodeEmoji / plainText) { return n; })* { return mergeText(nodes); }
= nodes:(&. n:plain { return n; })* { return mergeText(nodes); }

inlineParser
= nodes:(&. n:inline { return n; })* { return mergeText(nodes); }
Expand All @@ -92,9 +92,9 @@ full
/ mention
/ hashtag
/ url
/// fnVer2
/ fnVer2
/ link
/// fnVer1
/ fnVer1
/ search // block
/ inlineText

Expand All @@ -111,11 +111,32 @@ inline
/ mention
/ hashtag
/ url
/// fnVer2
/ fnVer2
/ link
/// fnVer1
/ fnVer1
/ inlineText

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

plain
= emojiCode
/ unicodeEmoji
/ plainText

//
// block rules
//
Expand Down Expand Up @@ -336,15 +357,21 @@ hashtag
}

hashtagContent
= (hashtagBracketPair / hashtagChar)+ { return text(); }

hashtagBracketPair
= "(" hashtagContent* ")"
/ "[" hashtagContent* "]"
/ "「" hashtagContent* "」"
= hashtagChar+ { return text(); }

hashtagChar
= ![  \t.,!?'"#:\/\[\]【】()「」] CHAR
= ![  \t.,!?'"#:\/【】] CHAR

// hashtagContent
// = (hashtagBracketPair / hashtagChar)+ { return text(); }

// hashtagBracketPair
// = "(" hashtagContent* ")"
// / "[" hashtagContent* "]"
// / "「" hashtagContent* "」"

// hashtagChar
// = ![  \t.,!?'"#:\/\[\]【】()「」] CHAR

// inline: URL

Expand All @@ -368,43 +395,56 @@ urlContent
= urlContentPart+

urlContentPart
= urlBracketPair
/ [.,] &urlContentPart // last char is neither "." nor ",".
= [.,] &urlContentPart // last char is neither "." nor ",".
/ [a-z0-9_/:%#@$&?!~=+-]i

urlBracketPair
= "(" urlContentPart* ")"
/ "[" urlContentPart* "]"
// urlContentPart
// = urlBracketPair
// / [.,] &urlContentPart // last char is neither "." nor ",".
// / [a-z0-9_/:%#@$&?!~=+-]i

// urlBracketPair
// = "(" urlContentPart* ")"
// / "[" urlContentPart* "]"

// inline: link

link
= silent:"?"? "[" label:linkLabelPart+ "](" url:linkUrl ")"
= silent:"?"? "[" label:linkLabel "](" url:linkUrl ")"
{
return LINK((silent != null), url, mergeText(label));
}

linkLabel
= parts:linkLabelPart+
{
return parts;
//return parts.flat(Infinity);
}

// linkLabelPart
// = url { return text(); /* text node */ }
// / link { return text(); /* text node */ }
// / !"]" n:inline { return n; }

linkLabelPart
= !"]" . { return text(); }
// = "[" linkLabelPart* "]"
= !"]" p:plain { return p; }

linkUrl
= url { return text(); }

// inline: fn

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

fnVer2
= "$[" name:$([a-z0-9_]i)+ args:fnArgs? _ content:(!"]" i:inline { return i; })+ "]"
= "$[" name:$([a-z0-9_]i)+ args:fnArgs? _ content:fnContentPart+ "]"
{
args = args || {};
return FN(name, args, mergeText(content));
Expand All @@ -430,6 +470,9 @@ fnArg
return { k: k, v: true };
}

fnContentPart
= !("]") i:inlineWithoutFn { return i; }

// inline: text

inlineText
Expand Down
69 changes: 36 additions & 33 deletions test/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,17 +608,18 @@ describe('FullParser', () => {
assert.deepStrictEqual(mfm.parse(input), output);
});

it('do not yield link node even if label is recognisable as a link', () => {
const input = 'official instance: [[https://misskey.io/@ai](https://misskey.io/@ai)](https://misskey.io/@ai).';
const output = [
TEXT('official instance: '),
LINK(false, 'https://misskey.io/@ai', [
TEXT('[https://misskey.io/@ai](https://misskey.io/@ai)')
]),
TEXT('.')
];
assert.deepStrictEqual(mfm.parse(input), output);
});
// disabled
// it('do not yield link node even if label is recognisable as a link', () => {
// const input = 'official instance: [[https://misskey.io/@ai](https://misskey.io/@ai)](https://misskey.io/@ai).';
// const output = [
// TEXT('official instance: '),
// LINK(false, 'https://misskey.io/@ai', [
// TEXT('[https://misskey.io/@ai](https://misskey.io/@ai)')
// ]),
// TEXT('.')
// ];
// assert.deepStrictEqual(mfm.parse(input), output);
// });
});

describe('fn v1', () => {
Expand All @@ -642,17 +643,18 @@ 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);
});
// fn nest is disabled
// 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', () => {
Expand All @@ -676,17 +678,18 @@ 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);
});
// fn nest is disabled
// 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 15c2ad5

Please sign in to comment.