* refactor mfm/html

* fix

* fix

* https://github.com/syuilo/misskey/pull/3567#discussion_r240023301
This commit is contained in:
tamaina 2018-12-09 13:15:32 +09:00 committed by syuilo
parent fb8b0c291d
commit 197e2c8377

View File

@ -14,44 +14,44 @@ export default (tokens: Node[], mentionedRemoteUsers: INote['mentionedRemoteUser
const doc = window.document; const doc = window.document;
function dive(nodes: Node[]): any[] { function appendChildren(children: Node[], targetElement: any): void {
return nodes.map(n => handlers[n.name](n)); for (const child of children.map(n => handlers[n.name](n))) targetElement.appendChild(child);
} }
const handlers: { [key: string]: (token: Node) => any } = { const handlers: { [key: string]: (token: Node) => any } = {
bold(token) { bold(token) {
const el = doc.createElement('b'); const el = doc.createElement('b');
dive(token.children).forEach(child => el.appendChild(child)); appendChildren(token.children, el);
return el; return el;
}, },
big(token) { big(token) {
const el = doc.createElement('strong'); const el = doc.createElement('strong');
dive(token.children).forEach(child => el.appendChild(child)); appendChildren(token.children, el);
return el; return el;
}, },
small(token) { small(token) {
const el = doc.createElement('small'); const el = doc.createElement('small');
dive(token.children).forEach(child => el.appendChild(child)); appendChildren(token.children, el);
return el; return el;
}, },
strike(token) { strike(token) {
const el = doc.createElement('del'); const el = doc.createElement('del');
dive(token.children).forEach(child => el.appendChild(child)); appendChildren(token.children, el);
return el; return el;
}, },
italic(token) { italic(token) {
const el = doc.createElement('i'); const el = doc.createElement('i');
dive(token.children).forEach(child => el.appendChild(child)); appendChildren(token.children, el);
return el; return el;
}, },
motion(token) { motion(token) {
const el = doc.createElement('i'); const el = doc.createElement('i');
dive(token.children).forEach(child => el.appendChild(child)); appendChildren(token.children, el);
return el; return el;
}, },
@ -65,7 +65,7 @@ export default (tokens: Node[], mentionedRemoteUsers: INote['mentionedRemoteUser
center(token) { center(token) {
const el = doc.createElement('div'); const el = doc.createElement('div');
dive(token.children).forEach(child => el.appendChild(child)); appendChildren(token.children, el);
return el; return el;
}, },
@ -96,7 +96,7 @@ export default (tokens: Node[], mentionedRemoteUsers: INote['mentionedRemoteUser
link(token) { link(token) {
const a = doc.createElement('a'); const a = doc.createElement('a');
a.href = token.props.url; a.href = token.props.url;
dive(token.children).forEach(child => a.appendChild(child)); appendChildren(token.children, a);
return a; return a;
}, },
@ -111,13 +111,13 @@ export default (tokens: Node[], mentionedRemoteUsers: INote['mentionedRemoteUser
quote(token) { quote(token) {
const el = doc.createElement('blockquote'); const el = doc.createElement('blockquote');
dive(token.children).forEach(child => el.appendChild(child)); appendChildren(token.children, el);
return el; return el;
}, },
title(token) { title(token) {
const el = doc.createElement('h1'); const el = doc.createElement('h1');
dive(token.children).forEach(child => el.appendChild(child)); appendChildren(token.children, el);
return el; return el;
}, },
@ -147,9 +147,7 @@ export default (tokens: Node[], mentionedRemoteUsers: INote['mentionedRemoteUser
} }
}; };
dive(tokens).forEach(x => { appendChildren(tokens, doc.body);
doc.body.appendChild(x);
});
return `<p>${doc.body.innerHTML}</p>`; return `<p>${doc.body.innerHTML}</p>`;
}; };