From 6d0faf7955b009a88059623eeb8bbac2789cffd6 Mon Sep 17 00:00:00 2001 From: marihachi Date: Sat, 24 Apr 2021 13:32:38 +0900 Subject: [PATCH 1/3] Enable syntaxes. nesting is restricted. --- src/parser.pegjs | 85 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 64 insertions(+), 21 deletions(-) diff --git a/src/parser.pegjs b/src/parser.pegjs index e7e660b0..5eeaccf1 100644 --- a/src/parser.pegjs +++ b/src/parser.pegjs @@ -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); } @@ -92,9 +92,9 @@ full / mention / hashtag / url - /// fnVer2 + / fnVer2 / link - /// fnVer1 + / fnVer1 / search // block / inlineText @@ -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 // @@ -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 @@ -368,28 +395,41 @@ 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(); } @@ -397,14 +437,14 @@ linkUrl // 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)); @@ -430,6 +470,9 @@ fnArg return { k: k, v: true }; } +fnContentPart + = !("]") i:inlineWithoutFn { return i; } + // inline: text inlineText From 4eec75ad3988756d7eaa0e5970fa224984fee0ab Mon Sep 17 00:00:00 2001 From: marihachi Date: Sat, 24 Apr 2021 13:32:55 +0900 Subject: [PATCH 2/3] disable nest tests --- test/parser.ts | 69 ++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/test/parser.ts b/test/parser.ts index b3e7f8fd..5d136813 100644 --- a/test/parser.ts +++ b/test/parser.ts @@ -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', () => { @@ -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', () => { @@ -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', () => { From 64d0429bb671852e385d6ff2d6db9b9996df9acf Mon Sep 17 00:00:00 2001 From: marihachi Date: Sat, 24 Apr 2021 13:46:18 +0900 Subject: [PATCH 3/3] v0.16.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dffc005b..497723de 100644 --- a/package.json +++ b/package.json @@ -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",