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