2016-12-29 07:49:51 +09:00
|
|
|
/**
|
|
|
|
* Misskey Text Analyzer
|
|
|
|
*/
|
|
|
|
|
|
|
|
const elements = [
|
|
|
|
require('./elements/bold'),
|
2018-04-19 15:05:39 +09:00
|
|
|
require('./elements/title'),
|
2016-12-29 07:49:51 +09:00
|
|
|
require('./elements/url'),
|
2017-03-18 01:16:32 +09:00
|
|
|
require('./elements/link'),
|
2016-12-29 07:49:51 +09:00
|
|
|
require('./elements/mention'),
|
2017-02-09 01:07:06 +09:00
|
|
|
require('./elements/hashtag'),
|
2017-02-11 23:41:57 +09:00
|
|
|
require('./elements/code'),
|
2017-03-01 05:33:27 +09:00
|
|
|
require('./elements/inline-code'),
|
2018-02-27 12:32:01 +09:00
|
|
|
require('./elements/quote'),
|
2017-03-01 05:33:27 +09:00
|
|
|
require('./elements/emoji')
|
2016-12-29 07:49:51 +09:00
|
|
|
];
|
|
|
|
|
2018-03-31 21:41:08 +09:00
|
|
|
export default (source: string): any[] => {
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
if (source == '') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tokens = [];
|
|
|
|
|
|
|
|
function push(token) {
|
|
|
|
if (token != null) {
|
|
|
|
tokens.push(token);
|
|
|
|
source = source.substr(token.content.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let i = 0;
|
|
|
|
|
|
|
|
// パース
|
|
|
|
while (source != '') {
|
|
|
|
const parsed = elements.some(el => {
|
2018-02-27 12:32:01 +09:00
|
|
|
let _tokens = el(source, i);
|
|
|
|
if (_tokens) {
|
|
|
|
if (!Array.isArray(_tokens)) {
|
|
|
|
_tokens = [_tokens];
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|
2018-02-27 12:32:01 +09:00
|
|
|
_tokens.forEach(push);
|
2016-12-29 07:49:51 +09:00
|
|
|
return true;
|
2017-03-18 20:05:11 +09:00
|
|
|
} else {
|
|
|
|
return false;
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!parsed) {
|
|
|
|
push({
|
|
|
|
type: 'text',
|
|
|
|
content: source[0]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// テキストを纏める
|
|
|
|
tokens[0] = [tokens[0]];
|
|
|
|
return tokens.reduce((a, b) => {
|
|
|
|
if (a[a.length - 1].type == 'text' && b.type == 'text') {
|
|
|
|
const tail = a.pop();
|
|
|
|
return a.concat({
|
|
|
|
type: 'text',
|
|
|
|
content: tail.content + b.content
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return a.concat(b);
|
|
|
|
}
|
|
|
|
});
|
2017-03-18 20:05:11 +09:00
|
|
|
};
|