2018-09-11 20:32:47 +02:00
|
|
|
import Vue, { VNode } from 'vue';
|
2018-03-31 14:41:08 +02:00
|
|
|
import * as emojilib from 'emojilib';
|
2018-08-05 12:20:26 +02:00
|
|
|
import { length } from 'stringz';
|
2018-06-20 18:21:57 +02:00
|
|
|
import parse from '../../../../../mfm/parse';
|
2018-07-07 12:19:00 +02:00
|
|
|
import getAcct from '../../../../../misc/acct/render';
|
2018-03-31 14:41:08 +02:00
|
|
|
import MkUrl from './url.vue';
|
2018-04-21 11:59:16 +02:00
|
|
|
import MkGoogle from './google.vue';
|
2018-09-11 20:32:47 +02:00
|
|
|
import { concat } from '../../../../../prelude/array';
|
2018-03-31 14:41:08 +02:00
|
|
|
|
2018-06-20 12:55:34 +02:00
|
|
|
export default Vue.component('misskey-flavored-markdown', {
|
2018-03-31 14:41:08 +02:00
|
|
|
props: {
|
|
|
|
text: {
|
|
|
|
type: String,
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
ast: {
|
|
|
|
type: [],
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
shouldBreak: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true
|
|
|
|
},
|
|
|
|
i: {
|
|
|
|
type: Object,
|
|
|
|
default: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
render(createElement) {
|
2018-09-11 20:32:47 +02:00
|
|
|
let ast: any[];
|
2018-03-31 14:41:08 +02:00
|
|
|
|
|
|
|
if (this.ast == null) {
|
|
|
|
// Parse text to ast
|
|
|
|
ast = parse(this.text);
|
|
|
|
} else {
|
2018-09-11 20:32:47 +02:00
|
|
|
ast = this.ast as any[];
|
2018-03-31 14:41:08 +02:00
|
|
|
}
|
|
|
|
|
2018-08-05 12:36:19 +02:00
|
|
|
let bigCount = 0;
|
|
|
|
let motionCount = 0;
|
|
|
|
|
2018-03-31 14:41:08 +02:00
|
|
|
// Parse ast to DOM
|
2018-09-11 20:32:47 +02:00
|
|
|
const els = concat(ast.map((token): VNode[] => {
|
2018-03-31 14:41:08 +02:00
|
|
|
switch (token.type) {
|
2018-08-05 12:20:26 +02:00
|
|
|
case 'text': {
|
2018-03-31 14:41:08 +02:00
|
|
|
const text = token.content.replace(/(\r\n|\n|\r)/g, '\n');
|
|
|
|
|
|
|
|
if (this.shouldBreak) {
|
|
|
|
const x = text.split('\n')
|
|
|
|
.map(t => t == '' ? [createElement('br')] : [createElement('span', t), createElement('br')]);
|
|
|
|
x[x.length - 1].pop();
|
|
|
|
return x;
|
|
|
|
} else {
|
2018-09-11 20:32:47 +02:00
|
|
|
return [createElement('span', text.replace(/\n/g, ' '))];
|
2018-03-31 14:41:08 +02:00
|
|
|
}
|
2018-08-05 12:20:26 +02:00
|
|
|
}
|
2018-03-31 14:41:08 +02:00
|
|
|
|
2018-08-05 12:20:26 +02:00
|
|
|
case 'bold': {
|
2018-09-11 20:32:47 +02:00
|
|
|
return [createElement('b', token.bold)];
|
2018-08-05 12:20:26 +02:00
|
|
|
}
|
2018-08-03 16:27:37 +02:00
|
|
|
|
2018-08-05 12:20:26 +02:00
|
|
|
case 'big': {
|
2018-08-05 12:36:19 +02:00
|
|
|
bigCount++;
|
2018-08-05 12:20:26 +02:00
|
|
|
const isLong = length(token.big) > 10;
|
2018-08-05 12:36:19 +02:00
|
|
|
const isMany = bigCount > 3;
|
2018-08-03 16:27:37 +02:00
|
|
|
return (createElement as any)('strong', {
|
|
|
|
attrs: {
|
2018-08-05 16:38:31 +02:00
|
|
|
style: `display: inline-block; font-size: ${ isMany ? '100%' : '150%' };`
|
2018-08-03 16:27:37 +02:00
|
|
|
},
|
2018-08-05 12:36:19 +02:00
|
|
|
directives: [this.$store.state.settings.disableAnimatedMfm || isLong || isMany ? {} : {
|
2018-08-03 16:27:37 +02:00
|
|
|
name: 'animate-css',
|
|
|
|
value: { classes: 'tada', iteration: 'infinite' }
|
|
|
|
}]
|
|
|
|
}, token.big);
|
2018-08-05 12:20:26 +02:00
|
|
|
}
|
2018-03-31 14:41:08 +02:00
|
|
|
|
2018-08-05 12:20:26 +02:00
|
|
|
case 'motion': {
|
2018-08-05 12:36:19 +02:00
|
|
|
motionCount++;
|
2018-08-05 12:20:26 +02:00
|
|
|
const isLong = length(token.motion) > 10;
|
2018-08-05 12:36:19 +02:00
|
|
|
const isMany = motionCount > 3;
|
2018-08-05 05:33:51 +02:00
|
|
|
return (createElement as any)('span', {
|
|
|
|
attrs: {
|
|
|
|
style: 'display: inline-block;'
|
|
|
|
},
|
2018-08-05 12:36:19 +02:00
|
|
|
directives: [this.$store.state.settings.disableAnimatedMfm || isLong || isMany ? {} : {
|
2018-08-05 05:33:51 +02:00
|
|
|
name: 'animate-css',
|
|
|
|
value: { classes: 'rubberBand', iteration: 'infinite' }
|
|
|
|
}]
|
|
|
|
}, token.motion);
|
2018-08-05 12:20:26 +02:00
|
|
|
}
|
2018-08-05 05:33:51 +02:00
|
|
|
|
2018-08-05 12:20:26 +02:00
|
|
|
case 'url': {
|
2018-09-11 20:32:47 +02:00
|
|
|
return [createElement(MkUrl, {
|
2018-03-31 14:41:08 +02:00
|
|
|
props: {
|
|
|
|
url: token.content,
|
2018-10-08 08:47:41 +02:00
|
|
|
target: '_blank',
|
|
|
|
style: 'color:var(--mfmLink);'
|
2018-03-31 14:41:08 +02:00
|
|
|
}
|
2018-09-11 20:32:47 +02:00
|
|
|
})];
|
2018-08-05 12:20:26 +02:00
|
|
|
}
|
2018-03-31 14:41:08 +02:00
|
|
|
|
2018-08-05 12:20:26 +02:00
|
|
|
case 'link': {
|
2018-09-11 20:32:47 +02:00
|
|
|
return [createElement('a', {
|
2018-03-31 14:41:08 +02:00
|
|
|
attrs: {
|
|
|
|
class: 'link',
|
|
|
|
href: token.url,
|
|
|
|
target: '_blank',
|
2018-10-08 08:47:41 +02:00
|
|
|
title: token.url,
|
|
|
|
style: 'color:var(--mfmLink);'
|
2018-03-31 14:41:08 +02:00
|
|
|
}
|
2018-09-11 20:32:47 +02:00
|
|
|
}, token.title)];
|
2018-08-05 12:20:26 +02:00
|
|
|
}
|
2018-03-31 14:41:08 +02:00
|
|
|
|
2018-08-05 12:20:26 +02:00
|
|
|
case 'mention': {
|
2018-10-20 02:34:34 +02:00
|
|
|
return (createElement as any)('router-link', {
|
2018-03-31 14:41:08 +02:00
|
|
|
attrs: {
|
2018-10-20 02:34:34 +02:00
|
|
|
to: `/${token.canonical}`,
|
2018-10-08 08:47:41 +02:00
|
|
|
dataIsMe: (this as any).i && getAcct((this as any).i) == getAcct(token),
|
|
|
|
style: 'color:var(--mfmMention);'
|
2018-03-31 14:41:08 +02:00
|
|
|
},
|
|
|
|
directives: [{
|
|
|
|
name: 'user-preview',
|
2018-10-14 09:56:19 +02:00
|
|
|
value: token.canonical
|
2018-03-31 14:41:08 +02:00
|
|
|
}]
|
2018-10-14 09:56:19 +02:00
|
|
|
}, token.canonical);
|
2018-08-05 12:20:26 +02:00
|
|
|
}
|
2018-03-31 14:41:08 +02:00
|
|
|
|
2018-08-05 12:20:26 +02:00
|
|
|
case 'hashtag': {
|
2018-10-20 02:34:34 +02:00
|
|
|
return [createElement('router-link', {
|
2018-03-31 14:41:08 +02:00
|
|
|
attrs: {
|
2018-10-20 02:34:34 +02:00
|
|
|
to: `/tags/${encodeURIComponent(token.hashtag)}`,
|
2018-10-08 08:47:41 +02:00
|
|
|
style: 'color:var(--mfmHashtag);'
|
2018-03-31 14:41:08 +02:00
|
|
|
}
|
2018-09-11 20:32:47 +02:00
|
|
|
}, token.content)];
|
2018-08-05 12:20:26 +02:00
|
|
|
}
|
2018-03-31 14:41:08 +02:00
|
|
|
|
2018-08-05 12:20:26 +02:00
|
|
|
case 'code': {
|
2018-09-11 20:32:47 +02:00
|
|
|
return [createElement('pre', {
|
2018-04-19 08:05:39 +02:00
|
|
|
class: 'code'
|
|
|
|
}, [
|
2018-03-31 14:41:08 +02:00
|
|
|
createElement('code', {
|
|
|
|
domProps: {
|
|
|
|
innerHTML: token.html
|
|
|
|
}
|
|
|
|
})
|
2018-09-11 20:32:47 +02:00
|
|
|
])];
|
2018-08-05 12:20:26 +02:00
|
|
|
}
|
2018-03-31 14:41:08 +02:00
|
|
|
|
2018-08-05 12:20:26 +02:00
|
|
|
case 'inline-code': {
|
2018-09-11 20:32:47 +02:00
|
|
|
return [createElement('code', {
|
2018-03-31 14:41:08 +02:00
|
|
|
domProps: {
|
|
|
|
innerHTML: token.html
|
|
|
|
}
|
2018-09-11 20:32:47 +02:00
|
|
|
})];
|
2018-08-05 12:20:26 +02:00
|
|
|
}
|
2018-03-31 14:41:08 +02:00
|
|
|
|
2018-08-05 12:20:26 +02:00
|
|
|
case 'quote': {
|
2018-03-31 14:41:08 +02:00
|
|
|
const text2 = token.quote.replace(/(\r\n|\n|\r)/g, '\n');
|
|
|
|
|
|
|
|
if (this.shouldBreak) {
|
|
|
|
const x = text2.split('\n')
|
|
|
|
.map(t => [createElement('span', t), createElement('br')]);
|
|
|
|
x[x.length - 1].pop();
|
2018-09-11 20:32:47 +02:00
|
|
|
return [createElement('div', {
|
2018-03-31 14:41:08 +02:00
|
|
|
attrs: {
|
|
|
|
class: 'quote'
|
|
|
|
}
|
2018-09-11 20:32:47 +02:00
|
|
|
}, x)];
|
2018-03-31 14:41:08 +02:00
|
|
|
} else {
|
2018-09-11 20:32:47 +02:00
|
|
|
return [createElement('span', {
|
2018-03-31 14:41:08 +02:00
|
|
|
attrs: {
|
|
|
|
class: 'quote'
|
|
|
|
}
|
2018-09-11 20:32:47 +02:00
|
|
|
}, text2.replace(/\n/g, ' '))];
|
2018-03-31 14:41:08 +02:00
|
|
|
}
|
2018-08-05 12:20:26 +02:00
|
|
|
}
|
2018-03-31 14:41:08 +02:00
|
|
|
|
2018-08-05 12:20:26 +02:00
|
|
|
case 'title': {
|
2018-09-11 20:32:47 +02:00
|
|
|
return [createElement('div', {
|
2018-04-19 08:05:39 +02:00
|
|
|
attrs: {
|
|
|
|
class: 'title'
|
|
|
|
}
|
2018-09-11 20:32:47 +02:00
|
|
|
}, token.title)];
|
2018-08-05 12:20:26 +02:00
|
|
|
}
|
2018-04-19 08:05:39 +02:00
|
|
|
|
2018-08-05 12:20:26 +02:00
|
|
|
case 'emoji': {
|
2018-11-01 03:51:49 +01:00
|
|
|
//#region カスタム絵文字
|
2018-11-01 10:05:14 +01:00
|
|
|
const customEmojis = (this.os.getMetaSync() || { emojis: [] }).emojis || [];
|
2018-11-01 03:51:49 +01:00
|
|
|
const customEmoji = customEmojis.find(e => e.name == token.emoji || (e.aliases || []).includes(token.emoji));
|
|
|
|
if (customEmoji) {
|
|
|
|
return [createElement('img', {
|
|
|
|
attrs: {
|
|
|
|
src: customEmoji.url,
|
|
|
|
alt: token.emoji,
|
|
|
|
title: token.emoji,
|
|
|
|
style: 'height: 2.5em; vertical-align: middle;'
|
|
|
|
}
|
|
|
|
})];
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2018-03-31 14:41:08 +02:00
|
|
|
const emoji = emojilib.lib[token.emoji];
|
2018-09-11 20:32:47 +02:00
|
|
|
return [createElement('span', emoji ? emoji.char : token.content)];
|
2018-08-05 12:20:26 +02:00
|
|
|
}
|
2018-03-31 14:41:08 +02:00
|
|
|
|
2018-08-05 12:20:26 +02:00
|
|
|
case 'search': {
|
2018-09-11 20:32:47 +02:00
|
|
|
return [createElement(MkGoogle, {
|
2018-04-21 11:59:16 +02:00
|
|
|
props: {
|
|
|
|
q: token.query
|
|
|
|
}
|
2018-09-11 20:32:47 +02:00
|
|
|
})];
|
2018-08-05 12:20:26 +02:00
|
|
|
}
|
2018-04-21 11:59:16 +02:00
|
|
|
|
2018-08-05 12:20:26 +02:00
|
|
|
default: {
|
2018-03-31 14:41:08 +02:00
|
|
|
console.log('unknown ast type:', token.type);
|
2018-09-11 20:32:47 +02:00
|
|
|
|
|
|
|
return [];
|
2018-08-05 12:20:26 +02:00
|
|
|
}
|
2018-03-31 14:41:08 +02:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2018-09-01 15:32:51 +02:00
|
|
|
// el.tag === 'br' のとき i !== 0 が保証されるため、短絡評価により els[i - 1] は配列外参照しない
|
|
|
|
const _els = els.filter((el, i) => !(el.tag === 'br' && ['div', 'pre'].includes(els[i - 1].tag)));
|
2018-03-31 14:41:08 +02:00
|
|
|
return createElement('span', _els);
|
|
|
|
}
|
|
|
|
});
|