From e3b1d00e4ce9a26c1d7a2e6fc93b931911b9820e Mon Sep 17 00:00:00 2001 From: Aya Morisawa Date: Wed, 30 Jan 2019 15:27:54 +0900 Subject: [PATCH] Extract parsePlain function --- src/client/app/common/views/components/mfm.ts | 4 ++-- src/mfm/parse.ts | 13 ++++++++++--- src/server/api/endpoints/i/update.ts | 4 ++-- test/mfm.ts | 10 +++++----- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/client/app/common/views/components/mfm.ts b/src/client/app/common/views/components/mfm.ts index 5ae2104915..61eeb4e076 100644 --- a/src/client/app/common/views/components/mfm.ts +++ b/src/client/app/common/views/components/mfm.ts @@ -1,7 +1,7 @@ import Vue, { VNode } from 'vue'; import { length } from 'stringz'; import { MfmForest } from '../../../../../mfm/types'; -import parse from '../../../../../mfm/parse'; +import parse, { parsePlain } from '../../../../../mfm/parse'; import MkUrl from './url.vue'; import MkMention from './mention.vue'; import { concat, sum } from '../../../../../prelude/array'; @@ -46,7 +46,7 @@ export default Vue.component('misskey-flavored-markdown', { render(createElement) { if (this.text == null || this.text == '') return; - const ast = parse(this.text, this.plainText); + const ast = (this.plainText ? parsePlain : parse)(this.text); let bigCount = 0; let motionCount = 0; diff --git a/src/mfm/parse.ts b/src/mfm/parse.ts index 0eb1f810f2..014904f329 100644 --- a/src/mfm/parse.ts +++ b/src/mfm/parse.ts @@ -2,11 +2,18 @@ import parser from './parser'; import { MfmForest } from './types'; import { normalize } from './normalize'; -export default (source: string, plainText = false): MfmForest => { +export default (source: string): MfmForest => { if (source == null || source == '') { return null; } - const raw = plainText ? parser.plain.tryParse(source) : parser.root.tryParse(source) as MfmForest; - return normalize(raw); + return normalize(parser.root.tryParse(source)); }; + +export function parsePlain(source: string): MfmForest { + if (source == null || source == '') { + return null; + } + + return normalize(parser.plain.tryParse(source)); +} diff --git a/src/server/api/endpoints/i/update.ts b/src/server/api/endpoints/i/update.ts index ec6aaa04da..a98bd5029a 100644 --- a/src/server/api/endpoints/i/update.ts +++ b/src/server/api/endpoints/i/update.ts @@ -6,7 +6,7 @@ import acceptAllFollowRequests from '../../../../services/following/requests/acc import { publishToFollowers } from '../../../../services/i/update'; import define from '../../define'; import getDriveFileUrl from '../../../../misc/get-drive-file-url'; -import parse from '../../../../mfm/parse'; +import parse, { parsePlain } from '../../../../mfm/parse'; import extractEmojis from '../../../../misc/extract-emojis'; const langmap = require('langmap'); @@ -206,7 +206,7 @@ export default define(meta, (ps, user, app) => new Promise(async (res, rej) => { let emojis = [] as string[]; if (updates.name != null) { - const tokens = parse(updates.name, true); + const tokens = parsePlain(updates.name); emojis = emojis.concat(extractEmojis(tokens)); } diff --git a/test/mfm.ts b/test/mfm.ts index 9532b7659c..a13c5eaf8e 100644 --- a/test/mfm.ts +++ b/test/mfm.ts @@ -10,7 +10,7 @@ import * as assert from 'assert'; -import analyze from '../src/mfm/parse'; +import analyze, { parsePlain } from '../src/mfm/parse'; import toHtml from '../src/mfm/toHtml'; import { createTree as tree, createLeaf as leaf, MfmTree } from '../src/mfm/types'; import { removeOrphanedBrackets } from '../src/mfm/parser'; @@ -1093,21 +1093,21 @@ describe('MFM', () => { describe('plainText', () => { it('text', () => { - const tokens = analyze('foo', true); + const tokens = parsePlain('foo'); assert.deepStrictEqual(tokens, [ text('foo'), ]); }); it('emoji', () => { - const tokens = analyze(':foo:', true); + const tokens = parsePlain(':foo:'); assert.deepStrictEqual(tokens, [ leaf('emoji', { name: 'foo' }) ]); }); it('emoji in text', () => { - const tokens = analyze('foo:bar:baz', true); + const tokens = parsePlain('foo:bar:baz'); assert.deepStrictEqual(tokens, [ text('foo'), leaf('emoji', { name: 'bar' }), @@ -1116,7 +1116,7 @@ describe('MFM', () => { }); it('disallow other syntax', () => { - const tokens = analyze('foo **bar** baz', true); + const tokens = parsePlain('foo **bar** baz'); assert.deepStrictEqual(tokens, [ text('foo **bar** baz'), ]);