Extract MFM types

This commit is contained in:
Aya Morisawa 2019-01-30 14:51:30 +09:00
parent 6eb9ba31bf
commit 4c9b66b0f0
No known key found for this signature in database
GPG Key ID: 3E64865D70D579F2
10 changed files with 48 additions and 45 deletions

View File

@ -1,6 +1,6 @@
import Vue, { VNode } from 'vue'; import Vue, { VNode } from 'vue';
import { length } from 'stringz'; import { length } from 'stringz';
import { MfmForest } from '../../../../../mfm/parser'; import { MfmForest } from '../../../../../mfm/types';
import parse from '../../../../../mfm/parse'; import parse from '../../../../../mfm/parse';
import MkUrl from './url.vue'; import MkUrl from './url.vue';
import MkMention from './mention.vue'; import MkMention from './mention.vue';

View File

@ -3,7 +3,7 @@ const { JSDOM } = jsdom;
import config from '../config'; import config from '../config';
import { INote } from '../models/note'; import { INote } from '../models/note';
import { intersperse } from '../prelude/array'; import { intersperse } from '../prelude/array';
import { MfmForest, MfmTree } from './parser'; import { MfmForest, MfmTree } from './types';
export default (tokens: MfmForest, mentionedRemoteUsers: INote['mentionedRemoteUsers'] = []) => { export default (tokens: MfmForest, mentionedRemoteUsers: INote['mentionedRemoteUsers'] = []) => {
if (tokens == null) { if (tokens == null) {

View File

@ -1,6 +1,6 @@
import * as A from '../prelude/array'; import * as A from '../prelude/array';
import * as S from '../prelude/string'; import * as S from '../prelude/string';
import { MfmForest, MfmTree } from './parser'; import { MfmForest, MfmTree } from './types';
import { createTree, createLeaf } from '../prelude/tree'; import { createTree, createLeaf } from '../prelude/tree';
function isEmptyTextTree(t: MfmTree): boolean { function isEmptyTextTree(t: MfmTree): boolean {

View File

@ -1,4 +1,5 @@
import parser, { plainParser, MfmForest } from './parser'; import parser, { plainParser } from './parser';
import { MfmForest } from './types';
import { normalize } from './normalize'; import { normalize } from './normalize';
export default (source: string, plainText = false): MfmForest => { export default (source: string, plainText = false): MfmForest => {

File diff suppressed because one or more lines are too long

37
src/mfm/types.ts Normal file
View File

@ -0,0 +1,37 @@
import { Tree } from '../prelude/tree';
import * as T from '../prelude/tree';
type Node<T, P> = { type: T, props: P };
export type MentionNode = Node<'mention', {
canonical: string,
username: string,
host: string,
acct: string
}>;
export type HashtagNode = Node<'hashtag', {
hashtag: string
}>;
export type EmojiNode = Node<'emoji', {
name: string
}>;
export type MfmNode =
MentionNode |
HashtagNode |
EmojiNode |
Node<string, any>;
export type MfmTree = Tree<MfmNode>;
export type MfmForest = MfmTree[];
export function createLeaf(type: string, props: any): MfmTree {
return T.createLeaf({ type, props });
}
export function createTree(type: string, children: MfmForest, props: any): MfmTree {
return T.createTree({ type, props }, children);
}

View File

@ -1,4 +1,4 @@
import { EmojiNode, MfmForest } from '../mfm/parser'; import { EmojiNode, MfmForest } from '../mfm/types';
import { preorderF } from '../prelude/tree'; import { preorderF } from '../prelude/tree';
import { unique } from '../prelude/array'; import { unique } from '../prelude/array';

View File

@ -1,4 +1,4 @@
import { HashtagNode, MfmForest } from '../mfm/parser'; import { HashtagNode, MfmForest } from '../mfm/types';
import { preorderF } from '../prelude/tree'; import { preorderF } from '../prelude/tree';
import { unique } from '../prelude/array'; import { unique } from '../prelude/array';

View File

@ -1,6 +1,6 @@
// test is located in test/extract-mentions // test is located in test/extract-mentions
import { MentionNode, MfmForest } from '../mfm/parser'; import { MentionNode, MfmForest } from '../mfm/types';
import { preorderF } from '../prelude/tree'; import { preorderF } from '../prelude/tree';
export default function(mfmForest: MfmForest): MentionNode['props'][] { export default function(mfmForest: MfmForest): MentionNode['props'][] {

View File

@ -12,7 +12,8 @@ import * as assert from 'assert';
import analyze from '../src/mfm/parse'; import analyze from '../src/mfm/parse';
import toHtml from '../src/mfm/html'; import toHtml from '../src/mfm/html';
import { createTree as tree, createLeaf as leaf, MfmTree, removeOrphanedBrackets } from '../src/mfm/parser'; import { createTree as tree, createLeaf as leaf, MfmTree } from '../src/mfm/types';
import { removeOrphanedBrackets } from '../src/mfm/parser';
function text(text: string): MfmTree { function text(text: string): MfmTree {
return leaf('text', { text }); return leaf('text', { text });