forked from FoundKeyGang/FoundKey
Extract parsePlain function
This commit is contained in:
parent
98795aad9a
commit
e3b1d00e4c
4 changed files with 19 additions and 12 deletions
|
@ -1,7 +1,7 @@
|
||||||
import Vue, { VNode } from 'vue';
|
import Vue, { VNode } from 'vue';
|
||||||
import { length } from 'stringz';
|
import { length } from 'stringz';
|
||||||
import { MfmForest } from '../../../../../mfm/types';
|
import { MfmForest } from '../../../../../mfm/types';
|
||||||
import parse from '../../../../../mfm/parse';
|
import parse, { parsePlain } from '../../../../../mfm/parse';
|
||||||
import MkUrl from './url.vue';
|
import MkUrl from './url.vue';
|
||||||
import MkMention from './mention.vue';
|
import MkMention from './mention.vue';
|
||||||
import { concat, sum } from '../../../../../prelude/array';
|
import { concat, sum } from '../../../../../prelude/array';
|
||||||
|
@ -46,7 +46,7 @@ export default Vue.component('misskey-flavored-markdown', {
|
||||||
render(createElement) {
|
render(createElement) {
|
||||||
if (this.text == null || this.text == '') return;
|
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 bigCount = 0;
|
||||||
let motionCount = 0;
|
let motionCount = 0;
|
||||||
|
|
|
@ -2,11 +2,18 @@ import parser from './parser';
|
||||||
import { MfmForest } from './types';
|
import { MfmForest } from './types';
|
||||||
import { normalize } from './normalize';
|
import { normalize } from './normalize';
|
||||||
|
|
||||||
export default (source: string, plainText = false): MfmForest => {
|
export default (source: string): MfmForest => {
|
||||||
if (source == null || source == '') {
|
if (source == null || source == '') {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const raw = plainText ? parser.plain.tryParse(source) : parser.root.tryParse(source) as MfmForest;
|
return normalize(parser.root.tryParse(source));
|
||||||
return normalize(raw);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function parsePlain(source: string): MfmForest {
|
||||||
|
if (source == null || source == '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return normalize(parser.plain.tryParse(source));
|
||||||
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import acceptAllFollowRequests from '../../../../services/following/requests/acc
|
||||||
import { publishToFollowers } from '../../../../services/i/update';
|
import { publishToFollowers } from '../../../../services/i/update';
|
||||||
import define from '../../define';
|
import define from '../../define';
|
||||||
import getDriveFileUrl from '../../../../misc/get-drive-file-url';
|
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';
|
import extractEmojis from '../../../../misc/extract-emojis';
|
||||||
const langmap = require('langmap');
|
const langmap = require('langmap');
|
||||||
|
|
||||||
|
@ -206,7 +206,7 @@ export default define(meta, (ps, user, app) => new Promise(async (res, rej) => {
|
||||||
let emojis = [] as string[];
|
let emojis = [] as string[];
|
||||||
|
|
||||||
if (updates.name != null) {
|
if (updates.name != null) {
|
||||||
const tokens = parse(updates.name, true);
|
const tokens = parsePlain(updates.name);
|
||||||
emojis = emojis.concat(extractEmojis(tokens));
|
emojis = emojis.concat(extractEmojis(tokens));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
test/mfm.ts
10
test/mfm.ts
|
@ -10,7 +10,7 @@
|
||||||
|
|
||||||
import * as assert from 'assert';
|
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 toHtml from '../src/mfm/toHtml';
|
||||||
import { createTree as tree, createLeaf as leaf, MfmTree } from '../src/mfm/types';
|
import { createTree as tree, createLeaf as leaf, MfmTree } from '../src/mfm/types';
|
||||||
import { removeOrphanedBrackets } from '../src/mfm/parser';
|
import { removeOrphanedBrackets } from '../src/mfm/parser';
|
||||||
|
@ -1093,21 +1093,21 @@ describe('MFM', () => {
|
||||||
|
|
||||||
describe('plainText', () => {
|
describe('plainText', () => {
|
||||||
it('text', () => {
|
it('text', () => {
|
||||||
const tokens = analyze('foo', true);
|
const tokens = parsePlain('foo');
|
||||||
assert.deepStrictEqual(tokens, [
|
assert.deepStrictEqual(tokens, [
|
||||||
text('foo'),
|
text('foo'),
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('emoji', () => {
|
it('emoji', () => {
|
||||||
const tokens = analyze(':foo:', true);
|
const tokens = parsePlain(':foo:');
|
||||||
assert.deepStrictEqual(tokens, [
|
assert.deepStrictEqual(tokens, [
|
||||||
leaf('emoji', { name: 'foo' })
|
leaf('emoji', { name: 'foo' })
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('emoji in text', () => {
|
it('emoji in text', () => {
|
||||||
const tokens = analyze('foo:bar:baz', true);
|
const tokens = parsePlain('foo:bar:baz');
|
||||||
assert.deepStrictEqual(tokens, [
|
assert.deepStrictEqual(tokens, [
|
||||||
text('foo'),
|
text('foo'),
|
||||||
leaf('emoji', { name: 'bar' }),
|
leaf('emoji', { name: 'bar' }),
|
||||||
|
@ -1116,7 +1116,7 @@ describe('MFM', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('disallow other syntax', () => {
|
it('disallow other syntax', () => {
|
||||||
const tokens = analyze('foo **bar** baz', true);
|
const tokens = parsePlain('foo **bar** baz');
|
||||||
assert.deepStrictEqual(tokens, [
|
assert.deepStrictEqual(tokens, [
|
||||||
text('foo **bar** baz'),
|
text('foo **bar** baz'),
|
||||||
]);
|
]);
|
||||||
|
|
Loading…
Reference in a new issue