misskey/src/web/app/common/views/components/post-html.ts

105 lines
2.3 KiB
TypeScript
Raw Normal View History

2018-02-11 23:26:35 +09:00
declare const _URL_: string;
import Vue from 'vue';
import * as pictograph from 'pictograph';
import MkUrl from './url.vue';
const escape = text =>
text
.replace(/>/g, '>')
.replace(/</g, '&lt;');
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':
const text = escape(token.content)
.replace(/(\r\n|\n|\r)/g, '\n');
if ((this as any).shouldBreak) {
2018-02-23 02:06:35 +09:00
if (text.indexOf('\n') != -1) {
2018-02-23 05:15:20 +09:00
const x = text.split('\n').map(t => [createElement('span', t), createElement('br')]);
x[x.length - 1].pop();
return x;
2018-02-23 02:06:35 +09:00
} else {
return createElement('span', text);
}
2018-02-11 23:26:35 +09:00
} else {
return createElement('span', text.replace(/\n/g, ' '));
}
case 'bold':
return createElement('strong', escape(token.bold));
case 'url':
return createElement(MkUrl, {
props: {
2018-02-20 23:17:04 +09:00
url: escape(token.content),
2018-02-11 23:26:35 +09:00
target: '_blank'
}
});
case 'link':
return createElement('a', {
attrs: {
class: 'link',
href: escape(token.url),
target: '_blank',
title: escape(token.url)
}
}, escape(token.title));
case 'mention':
return (createElement as any)('a', {
attrs: {
href: `${_URL_}/${escape(token.username)}`,
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: {
href: `${_URL_}/search?q=${escape(token.content)}`,
target: '_blank'
}
}, escape(token.content));
case 'code':
return createElement('pre', [
createElement('code', token.html)
]);
case 'inline-code':
return createElement('code', token.html);
case 'emoji':
return createElement('span', pictograph.dic[token.emoji] || token.content);
}
}));
2018-02-22 22:27:28 +09:00
return createElement('span', els);
2018-02-11 23:26:35 +09:00
}
});