This commit is contained in:
syuilo 2018-09-12 03:32:47 +09:00
parent 0ed197d4d9
commit ca2230f690
No known key found for this signature in database
GPG Key ID: BDC4C49D06AB9D69

View File

@ -1,4 +1,4 @@
import Vue from 'vue'; import Vue, { VNode } from 'vue';
import * as emojilib from 'emojilib'; import * as emojilib from 'emojilib';
import { length } from 'stringz'; import { length } from 'stringz';
import parse from '../../../../../mfm/parse'; import parse from '../../../../../mfm/parse';
@ -6,10 +6,7 @@ import getAcct from '../../../../../misc/acct/render';
import { url } from '../../../config'; import { url } from '../../../config';
import MkUrl from './url.vue'; import MkUrl from './url.vue';
import MkGoogle from './google.vue'; import MkGoogle from './google.vue';
import { concat } from '../../../../../prelude/array';
const flatten = list => list.reduce(
(a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []
);
export default Vue.component('misskey-flavored-markdown', { export default Vue.component('misskey-flavored-markdown', {
props: { props: {
@ -32,20 +29,20 @@ export default Vue.component('misskey-flavored-markdown', {
}, },
render(createElement) { render(createElement) {
let ast; let ast: any[];
if (this.ast == null) { if (this.ast == null) {
// Parse text to ast // Parse text to ast
ast = parse(this.text); ast = parse(this.text);
} else { } else {
ast = this.ast; ast = this.ast as any[];
} }
let bigCount = 0; let bigCount = 0;
let motionCount = 0; let motionCount = 0;
// Parse ast to DOM // Parse ast to DOM
const els = flatten(ast.map(token => { const els = concat(ast.map((token): VNode[] => {
switch (token.type) { switch (token.type) {
case 'text': { case 'text': {
const text = token.content.replace(/(\r\n|\n|\r)/g, '\n'); const text = token.content.replace(/(\r\n|\n|\r)/g, '\n');
@ -56,12 +53,12 @@ export default Vue.component('misskey-flavored-markdown', {
x[x.length - 1].pop(); x[x.length - 1].pop();
return x; return x;
} else { } else {
return createElement('span', text.replace(/\n/g, ' ')); return [createElement('span', text.replace(/\n/g, ' '))];
} }
} }
case 'bold': { case 'bold': {
return createElement('b', token.bold); return [createElement('b', token.bold)];
} }
case 'big': { case 'big': {
@ -95,23 +92,23 @@ export default Vue.component('misskey-flavored-markdown', {
} }
case 'url': { case 'url': {
return createElement(MkUrl, { return [createElement(MkUrl, {
props: { props: {
url: token.content, url: token.content,
target: '_blank' target: '_blank'
} }
}); })];
} }
case 'link': { case 'link': {
return createElement('a', { return [createElement('a', {
attrs: { attrs: {
class: 'link', class: 'link',
href: token.url, href: token.url,
target: '_blank', target: '_blank',
title: token.url title: token.url
} }
}, token.title); }, token.title)];
} }
case 'mention': { case 'mention': {
@ -129,16 +126,16 @@ export default Vue.component('misskey-flavored-markdown', {
} }
case 'hashtag': { case 'hashtag': {
return createElement('a', { return [createElement('a', {
attrs: { attrs: {
href: `${url}/tags/${encodeURIComponent(token.hashtag)}`, href: `${url}/tags/${encodeURIComponent(token.hashtag)}`,
target: '_blank' target: '_blank'
} }
}, token.content); }, token.content)];
} }
case 'code': { case 'code': {
return createElement('pre', { return [createElement('pre', {
class: 'code' class: 'code'
}, [ }, [
createElement('code', { createElement('code', {
@ -146,15 +143,15 @@ export default Vue.component('misskey-flavored-markdown', {
innerHTML: token.html innerHTML: token.html
} }
}) })
]); ])];
} }
case 'inline-code': { case 'inline-code': {
return createElement('code', { return [createElement('code', {
domProps: { domProps: {
innerHTML: token.html innerHTML: token.html
} }
}); })];
} }
case 'quote': { case 'quote': {
@ -164,43 +161,45 @@ export default Vue.component('misskey-flavored-markdown', {
const x = text2.split('\n') const x = text2.split('\n')
.map(t => [createElement('span', t), createElement('br')]); .map(t => [createElement('span', t), createElement('br')]);
x[x.length - 1].pop(); x[x.length - 1].pop();
return createElement('div', { return [createElement('div', {
attrs: { attrs: {
class: 'quote' class: 'quote'
} }
}, x); }, x)];
} else { } else {
return createElement('span', { return [createElement('span', {
attrs: { attrs: {
class: 'quote' class: 'quote'
} }
}, text2.replace(/\n/g, ' ')); }, text2.replace(/\n/g, ' '))];
} }
} }
case 'title': { case 'title': {
return createElement('div', { return [createElement('div', {
attrs: { attrs: {
class: 'title' class: 'title'
} }
}, token.title); }, token.title)];
} }
case 'emoji': { case 'emoji': {
const emoji = emojilib.lib[token.emoji]; const emoji = emojilib.lib[token.emoji];
return createElement('span', emoji ? emoji.char : token.content); return [createElement('span', emoji ? emoji.char : token.content)];
} }
case 'search': { case 'search': {
return createElement(MkGoogle, { return [createElement(MkGoogle, {
props: { props: {
q: token.query q: token.query
} }
}); })];
} }
default: { default: {
console.log('unknown ast type:', token.type); console.log('unknown ast type:', token.type);
return [];
} }
} }
})); }));