yumechi-no-kuni/src/text/parse/index.ts

100 lines
2.3 KiB
TypeScript
Raw Normal View History

2016-12-28 16:49:51 -06:00
/**
* Misskey Text Analyzer
*/
2018-06-17 05:55:39 -05:00
import { TextElementBold } from "./elements/bold";
import { TextElementCode } from "./elements/code";
import { TextElementEmoji } from "./elements/emoji";
import { TextElementHashtag } from "./elements/hashtag";
import { TextElementInlineCode } from "./elements/inline-code";
import { TextElementLink } from "./elements/link";
import { TextElementMention } from "./elements/mention";
import { TextElementQuote } from "./elements/quote";
import { TextElementSearch } from "./elements/search";
import { TextElementTitle } from "./elements/title";
import { TextElementUrl } from "./elements/url";
2016-12-28 16:49:51 -06:00
const elements = [
require('./elements/bold'),
2018-04-19 01:05:39 -05:00
require('./elements/title'),
2016-12-28 16:49:51 -06:00
require('./elements/url'),
2017-03-17 11:16:32 -05:00
require('./elements/link'),
2016-12-28 16:49:51 -06:00
require('./elements/mention'),
2017-02-08 10:07:06 -06:00
require('./elements/hashtag'),
2017-02-11 08:41:57 -06:00
require('./elements/code'),
require('./elements/inline-code'),
2018-02-26 21:32:01 -06:00
require('./elements/quote'),
2018-04-21 17:21:51 -05:00
require('./elements/emoji'),
require('./elements/search')
2018-06-17 05:55:39 -05:00
].map(element => element.default as TextElementProcessor);
export type TextElement = {type: "text", content: string}
| TextElementBold
| TextElementCode
| TextElementEmoji
| TextElementHashtag
| TextElementInlineCode
| TextElementLink
| TextElementMention
| TextElementQuote
| TextElementSearch
| TextElementTitle
| TextElementUrl;
export type TextElementProcessor = (text: string, i: number) => TextElement | TextElement[];
2016-12-28 16:49:51 -06:00
2018-06-17 05:55:39 -05:00
export default (source: string): TextElement[] => {
2016-12-28 16:49:51 -06:00
if (source == '') {
return null;
}
2018-06-17 05:55:39 -05:00
const tokens: TextElement[] = [];
2016-12-28 16:49:51 -06:00
2018-06-17 05:55:39 -05:00
function push(token: TextElement) {
2016-12-28 16:49:51 -06:00
if (token != null) {
tokens.push(token);
source = source.substr(token.content.length);
}
}
let i = 0;
// パース
while (source != '') {
const parsed = elements.some(el => {
2018-02-26 21:32:01 -06:00
let _tokens = el(source, i);
if (_tokens) {
if (!Array.isArray(_tokens)) {
_tokens = [_tokens];
2016-12-28 16:49:51 -06:00
}
2018-02-26 21:32:01 -06:00
_tokens.forEach(push);
2016-12-28 16:49:51 -06:00
return true;
2017-03-18 06:05:11 -05:00
} else {
return false;
2016-12-28 16:49:51 -06:00
}
});
if (!parsed) {
push({
type: 'text',
content: source[0]
});
}
i++;
}
// テキストを纏める
return tokens.reduce((a, b) => {
2018-06-17 05:55:39 -05:00
if (a.length && a[a.length - 1].type == 'text' && b.type == 'text') {
2016-12-28 16:49:51 -06:00
const tail = a.pop();
return a.concat({
type: 'text',
content: tail.content + b.content
});
} else {
return a.concat(b);
}
2018-06-17 05:55:39 -05:00
}, [] as TextElement[]);
2017-03-18 06:05:11 -05:00
};