2018-03-31 05:41:08 -07:00
|
|
|
import Vue from 'vue';
|
|
|
|
import * as emojilib from 'emojilib';
|
2018-04-01 20:58:53 -07:00
|
|
|
import parse from '../../../../../text/parse';
|
2018-04-01 21:44:32 -07:00
|
|
|
import getAcct from '../../../../../acct/render';
|
2018-03-31 05:41:08 -07:00
|
|
|
import { url } from '../../../config';
|
|
|
|
import MkUrl from './url.vue';
|
2018-04-21 02:59:16 -07:00
|
|
|
import MkGoogle from './google.vue';
|
2018-03-31 05:41:08 -07:00
|
|
|
|
|
|
|
const flatten = list => list.reduce(
|
|
|
|
(a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []
|
|
|
|
);
|
|
|
|
|
2018-04-07 10:30:37 -07:00
|
|
|
export default Vue.component('mk-note-html', {
|
2018-03-31 05:41:08 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-06-11 16:43:48 -07:00
|
|
|
if (ast.filter(x => x.type != 'hashtag').length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-11 17:10:34 -07:00
|
|
|
while (
|
|
|
|
ast[ast.length - 1].type == 'hashtag' ||
|
|
|
|
(ast[ast.length - 1].type == 'text' && ast[ast.length - 1].content == ' ') ||
|
|
|
|
(ast[ast.length - 1].type == 'text' && ast[ast.length - 1].content == '\n')) {
|
2018-06-11 16:43:48 -07:00
|
|
|
ast.pop();
|
|
|
|
}
|
|
|
|
|
2018-03-31 05:41:08 -07:00
|
|
|
// 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':
|
|
|
|
return createElement('strong', token.bold);
|
|
|
|
|
|
|
|
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-06-11 17:10:34 -07:00
|
|
|
href: `${url}/tags/${token.content}`,
|
2018-03-31 05:41:08 -07:00
|
|
|
target: '_blank'
|
|
|
|
}
|
|
|
|
}, token.content);
|
|
|
|
|
|
|
|
case 'code':
|
2018-04-18 23:05:39 -07:00
|
|
|
return createElement('pre', {
|
|
|
|
class: 'code'
|
|
|
|
}, [
|
2018-03-31 05:41:08 -07:00
|
|
|
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, ' '));
|
|
|
|
}
|
|
|
|
|
2018-04-18 23:05:39 -07:00
|
|
|
case 'title':
|
|
|
|
return createElement('div', {
|
|
|
|
attrs: {
|
|
|
|
class: 'title'
|
|
|
|
}
|
|
|
|
}, token.title);
|
|
|
|
|
2018-03-31 05:41:08 -07:00
|
|
|
case 'emoji':
|
|
|
|
const emoji = emojilib.lib[token.emoji];
|
|
|
|
return createElement('span', emoji ? emoji.char : token.content);
|
|
|
|
|
2018-04-21 02:59:16 -07:00
|
|
|
case 'search':
|
|
|
|
return createElement(MkGoogle, {
|
|
|
|
props: {
|
|
|
|
q: token.query
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-31 05:41:08 -07:00
|
|
|
default:
|
|
|
|
console.log('unknown ast type:', token.type);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
const _els = [];
|
|
|
|
els.forEach((el, i) => {
|
|
|
|
if (el.tag == 'br') {
|
2018-04-18 23:05:39 -07:00
|
|
|
if (!['div', 'pre'].includes(els[i - 1].tag)) {
|
2018-03-31 05:41:08 -07:00
|
|
|
_els.push(el);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_els.push(el);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return createElement('span', _els);
|
|
|
|
}
|
|
|
|
});
|