1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-12-12 05:38:12 +09:00
* 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;
function dive(nodes: Node[]): any[] {
return nodes.map(n => handlers[n.name](n));
function appendChildren(children: Node[], targetElement: any): void {
for (const child of children.map(n => handlers[n.name](n))) targetElement.appendChild(child);
}
const handlers: { [key: string]: (token: Node) => any } = {
bold(token) {
const el = doc.createElement('b');
dive(token.children).forEach(child => el.appendChild(child));
appendChildren(token.children, el);
return el;
},
big(token) {
const el = doc.createElement('strong');
dive(token.children).forEach(child => el.appendChild(child));
appendChildren(token.children, el);
return el;
},
small(token) {
const el = doc.createElement('small');
dive(token.children).forEach(child => el.appendChild(child));
appendChildren(token.children, el);
return el;
},
strike(token) {
const el = doc.createElement('del');
dive(token.children).forEach(child => el.appendChild(child));
appendChildren(token.children, el);
return el;
},
italic(token) {
const el = doc.createElement('i');
dive(token.children).forEach(child => el.appendChild(child));
appendChildren(token.children, el);
return el;
},
motion(token) {
const el = doc.createElement('i');
dive(token.children).forEach(child => el.appendChild(child));
appendChildren(token.children, el);
return el;
},
@ -65,7 +65,7 @@ export default (tokens: Node[], mentionedRemoteUsers: INote['mentionedRemoteUser
center(token) {
const el = doc.createElement('div');
dive(token.children).forEach(child => el.appendChild(child));
appendChildren(token.children, el);
return el;
},
@ -96,7 +96,7 @@ export default (tokens: Node[], mentionedRemoteUsers: INote['mentionedRemoteUser
link(token) {
const a = doc.createElement('a');
a.href = token.props.url;
dive(token.children).forEach(child => a.appendChild(child));
appendChildren(token.children, a);
return a;
},
@ -111,13 +111,13 @@ export default (tokens: Node[], mentionedRemoteUsers: INote['mentionedRemoteUser
quote(token) {
const el = doc.createElement('blockquote');
dive(token.children).forEach(child => el.appendChild(child));
appendChildren(token.children, el);
return el;
},
title(token) {
const el = doc.createElement('h1');
dive(token.children).forEach(child => el.appendChild(child));
appendChildren(token.children, el);
return el;
},
@ -147,9 +147,7 @@ export default (tokens: Node[], mentionedRemoteUsers: INote['mentionedRemoteUser
}
};
dive(tokens).forEach(x => {
doc.body.appendChild(x);
});
appendChildren(tokens, doc.body);
return `<p>${doc.body.innerHTML}</p>`;
};