Sharkey/src/client/app/common/views/components/misskey-flavored-markdown.ts

213 lines
4.7 KiB
TypeScript
Raw Normal View History

import Vue from 'vue';
import * as emojilib from 'emojilib';
import { length } from 'stringz';
import parse from '../../../../../mfm/parse';
2018-07-07 13:19:00 +03:00
import getAcct from '../../../../../misc/acct/render';
import { url } from '../../../config';
import MkUrl from './url.vue';
2018-04-21 12:59:16 +03:00
import MkGoogle from './google.vue';
const flatten = list => list.reduce(
(a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []
);
2018-06-20 13:55:34 +03:00
export default Vue.component('misskey-flavored-markdown', {
props: {
text: {
type: String,
required: true
},
ast: {
type: [],
required: false
},
shouldBreak: {
type: Boolean,
default: true
},
i: {
type: Object,
default: null
}
},
render(createElement) {
let ast;
if (this.ast == null) {
// Parse text to ast
ast = parse(this.text);
} else {
ast = this.ast;
}
let bigCount = 0;
let motionCount = 0;
// Parse ast to DOM
const els = flatten(ast.map(token => {
switch (token.type) {
case 'text': {
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 {
return createElement('span', text.replace(/\n/g, ' '));
}
}
case 'bold': {
2018-08-03 17:27:37 +03:00
return createElement('b', token.bold);
}
2018-08-03 17:27:37 +03:00
case 'big': {
bigCount++;
const isLong = length(token.big) > 10;
const isMany = bigCount > 3;
2018-08-03 17:27:37 +03:00
return (createElement as any)('strong', {
attrs: {
2018-08-05 17:38:31 +03:00
style: `display: inline-block; font-size: ${ isMany ? '100%' : '150%' };`
2018-08-03 17:27:37 +03:00
},
directives: [this.$store.state.settings.disableAnimatedMfm || isLong || isMany ? {} : {
2018-08-03 17:27:37 +03:00
name: 'animate-css',
value: { classes: 'tada', iteration: 'infinite' }
}]
}, token.big);
}
case 'motion': {
motionCount++;
const isLong = length(token.motion) > 10;
const isMany = motionCount > 3;
2018-08-05 06:33:51 +03:00
return (createElement as any)('span', {
attrs: {
style: 'display: inline-block;'
},
directives: [this.$store.state.settings.disableAnimatedMfm || isLong || isMany ? {} : {
2018-08-05 06:33:51 +03:00
name: 'animate-css',
value: { classes: 'rubberBand', iteration: 'infinite' }
}]
}, token.motion);
}
2018-08-05 06:33:51 +03:00
case 'url': {
return createElement(MkUrl, {
props: {
url: token.content,
target: '_blank'
}
});
}
case 'link': {
return createElement('a', {
attrs: {
class: 'link',
href: token.url,
target: '_blank',
title: token.url
}
}, token.title);
}
case 'mention': {
return (createElement as any)('a', {
attrs: {
href: `${url}/@${getAcct(token)}`,
target: '_blank',
dataIsMe: (this as any).i && getAcct((this as any).i) == getAcct(token)
},
directives: [{
name: 'user-preview',
value: token.content
}]
}, token.content);
}
case 'hashtag': {
return createElement('a', {
attrs: {
2018-07-26 07:48:08 +03:00
href: `${url}/tags/${encodeURIComponent(token.hashtag)}`,
target: '_blank'
}
}, token.content);
}
case 'code': {
2018-04-19 09:05:39 +03:00
return createElement('pre', {
class: 'code'
}, [
createElement('code', {
domProps: {
innerHTML: token.html
}
})
]);
}
case 'inline-code': {
return createElement('code', {
domProps: {
innerHTML: token.html
}
});
}
case 'quote': {
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();
return createElement('div', {
attrs: {
class: 'quote'
}
}, x);
} else {
return createElement('span', {
attrs: {
class: 'quote'
}
}, text2.replace(/\n/g, ' '));
}
}
case 'title': {
2018-04-19 09:05:39 +03:00
return createElement('div', {
attrs: {
class: 'title'
}
}, token.title);
}
2018-04-19 09:05:39 +03:00
case 'emoji': {
const emoji = emojilib.lib[token.emoji];
return createElement('span', emoji ? emoji.char : token.content);
}
case 'search': {
2018-04-21 12:59:16 +03:00
return createElement(MkGoogle, {
props: {
q: token.query
}
});
}
2018-04-21 12:59:16 +03:00
default: {
console.log('unknown ast type:', token.type);
}
}
}));
2018-09-01 16:32:51 +03: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)));
return createElement('span', _els);
}
});