From f3155ea180a9dfcfad9f8d356e5d334fe8ccf48f Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 25 Nov 2018 13:36:40 +0900 Subject: [PATCH] [MFM] Add center syntax Resolve #1775 --- src/client/app/common/views/components/mfm.ts | 8 ++++++++ src/mfm/html.ts | 6 ++++++ src/mfm/parse.ts | 2 +- src/mfm/parser.ts | 18 ++++++++++++++++++ test/mfm.ts | 11 +++++++++++ 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/client/app/common/views/components/mfm.ts b/src/client/app/common/views/components/mfm.ts index e93ea5ccb..d1ce4873f 100644 --- a/src/client/app/common/views/components/mfm.ts +++ b/src/client/app/common/views/components/mfm.ts @@ -111,6 +111,14 @@ export default Vue.component('misskey-flavored-markdown', { }, genEl(token.children)); } + case 'center': { + return [createElement('div', { + attrs: { + style: 'text-align:center;' + } + }, genEl(token.children))]; + } + case 'motion': { motionCount++; const isLong = getTextCount(token.children) > 10 || getChildrenCount(token.children) > 5; diff --git a/src/mfm/html.ts b/src/mfm/html.ts index d45cc13af..8b63d8f82 100644 --- a/src/mfm/html.ts +++ b/src/mfm/html.ts @@ -45,6 +45,12 @@ export default (tokens: Node[], mentionedRemoteUsers: INote['mentionedRemoteUser return pre; }, + center(token) { + const el = doc.createElement('div'); + dive(token.children).forEach(child => el.appendChild(child)); + return el; + }, + emoji(token) { return doc.createTextNode(token.props.emoji ? token.props.emoji : `:${token.props.name}:`); }, diff --git a/src/mfm/parse.ts b/src/mfm/parse.ts index 28e61272d..ee434d8dd 100644 --- a/src/mfm/parse.ts +++ b/src/mfm/parse.ts @@ -41,7 +41,7 @@ export default (source: string): Node[] => { } function isBlockNode(node: Node): boolean { - return ['blockCode', 'quote', 'title'].includes(node.name); + return ['blockCode', 'center', 'quote', 'title'].includes(node.name); } /** diff --git a/src/mfm/parser.ts b/src/mfm/parser.ts index b02a88596..1d6a4b810 100644 --- a/src/mfm/parser.ts +++ b/src/mfm/parser.ts @@ -73,6 +73,7 @@ const mfm = P.createLanguage({ r.math, r.search, r.title, + r.center, r.text ).atLeast(1), @@ -113,6 +114,23 @@ const mfm = P.createLanguage({ ).atLeast(1).tryParse(x))), //#endregion + //#region Center + center: r => + P.regexp(/
([\s\S]+?)<\/center>/, 1) + .map(x => makeNodeWithChildren('center', P.alt( + r.big, + r.bold, + r.motion, + r.mention, + r.hashtag, + r.emoji, + r.math, + r.url, + r.link, + r.text + ).atLeast(1).tryParse(x))), + //#endregion + //#region Emoji emoji: r => P.alt( diff --git a/test/mfm.ts b/test/mfm.ts index ac52041bb..98184c9ad 100644 --- a/test/mfm.ts +++ b/test/mfm.ts @@ -641,6 +641,17 @@ describe('Text', () => { ], tokens); }); }); + + describe('center', () => { + it('simple', () => { + const tokens = analyze('
foo
'); + assert.deepEqual([ + nodeWithChildren('center', [ + text('foo') + ]), + ], tokens); + }); + }); }); describe('toHtml', () => {