2018-02-11 15:26:35 +01:00
|
|
|
import Vue from 'vue';
|
2018-02-25 09:03:39 +01:00
|
|
|
import * as emojilib from 'emojilib';
|
2018-02-23 10:34:14 +01:00
|
|
|
import { url } from '../../../config';
|
2018-02-11 15:26:35 +01:00
|
|
|
import MkUrl from './url.vue';
|
|
|
|
|
|
|
|
export default Vue.component('mk-post-html', {
|
|
|
|
props: {
|
|
|
|
ast: {
|
|
|
|
type: Array,
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
shouldBreak: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true
|
|
|
|
},
|
|
|
|
i: {
|
|
|
|
type: Object,
|
|
|
|
default: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
render(createElement) {
|
|
|
|
const els = [].concat.apply([], (this as any).ast.map(token => {
|
|
|
|
switch (token.type) {
|
|
|
|
case 'text':
|
2018-02-23 10:34:14 +01:00
|
|
|
const text = token.content.replace(/(\r\n|\n|\r)/g, '\n');
|
2018-02-11 15:26:35 +01:00
|
|
|
|
|
|
|
if ((this as any).shouldBreak) {
|
2018-02-22 18:06:35 +01:00
|
|
|
if (text.indexOf('\n') != -1) {
|
2018-02-23 10:34:14 +01:00
|
|
|
const x = text.split('\n')
|
|
|
|
.map(t => [createElement('span', t), createElement('br')]);
|
2018-02-22 21:15:20 +01:00
|
|
|
x[x.length - 1].pop();
|
|
|
|
return x;
|
2018-02-22 18:06:35 +01:00
|
|
|
} else {
|
|
|
|
return createElement('span', text);
|
|
|
|
}
|
2018-02-11 15:26:35 +01:00
|
|
|
} else {
|
|
|
|
return createElement('span', text.replace(/\n/g, ' '));
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'bold':
|
2018-02-23 10:34:14 +01:00
|
|
|
return createElement('strong', token.bold);
|
2018-02-11 15:26:35 +01:00
|
|
|
|
|
|
|
case 'url':
|
|
|
|
return createElement(MkUrl, {
|
|
|
|
props: {
|
2018-02-23 10:34:14 +01:00
|
|
|
url: token.content,
|
2018-02-11 15:26:35 +01:00
|
|
|
target: '_blank'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
case 'link':
|
|
|
|
return createElement('a', {
|
|
|
|
attrs: {
|
|
|
|
class: 'link',
|
2018-02-23 10:34:14 +01:00
|
|
|
href: token.url,
|
2018-02-11 15:26:35 +01:00
|
|
|
target: '_blank',
|
2018-02-23 10:34:14 +01:00
|
|
|
title: token.url
|
2018-02-11 15:26:35 +01:00
|
|
|
}
|
2018-02-23 10:34:14 +01:00
|
|
|
}, token.title);
|
2018-02-11 15:26:35 +01:00
|
|
|
|
|
|
|
case 'mention':
|
|
|
|
return (createElement as any)('a', {
|
|
|
|
attrs: {
|
2018-02-23 10:34:14 +01:00
|
|
|
href: `${url}/${token.username}`,
|
2018-02-11 15:26:35 +01:00
|
|
|
target: '_blank',
|
|
|
|
dataIsMe: (this as any).i && (this as any).i.username == token.username
|
|
|
|
},
|
|
|
|
directives: [{
|
|
|
|
name: 'user-preview',
|
|
|
|
value: token.content
|
|
|
|
}]
|
|
|
|
}, token.content);
|
|
|
|
|
|
|
|
case 'hashtag':
|
|
|
|
return createElement('a', {
|
|
|
|
attrs: {
|
2018-02-23 10:34:14 +01:00
|
|
|
href: `${url}/search?q=${token.content}`,
|
2018-02-11 15:26:35 +01:00
|
|
|
target: '_blank'
|
|
|
|
}
|
2018-02-23 10:34:14 +01:00
|
|
|
}, token.content);
|
2018-02-11 15:26:35 +01:00
|
|
|
|
|
|
|
case 'code':
|
|
|
|
return createElement('pre', [
|
2018-02-23 23:54:37 +01:00
|
|
|
createElement('code', {
|
|
|
|
domProps: {
|
|
|
|
innerHTML: token.html
|
|
|
|
}
|
|
|
|
})
|
2018-02-11 15:26:35 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
case 'inline-code':
|
|
|
|
return createElement('code', token.html);
|
|
|
|
|
|
|
|
case 'emoji':
|
2018-02-26 19:48:14 +01:00
|
|
|
const emoji = emojilib.lib[token.emoji];
|
|
|
|
return createElement('span', emoji ? emoji.char : token.content);
|
2018-02-11 15:26:35 +01:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2018-02-22 14:27:28 +01:00
|
|
|
return createElement('span', els);
|
2018-02-11 15:26:35 +01:00
|
|
|
}
|
|
|
|
});
|