Created
July 15, 2016 06:31
-
-
Save pastak/8bbd0ea227da33c7852c9a4b894b4f42 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MdFormatter { | |
renderNode (node) { | |
if (typeof node === 'string') { | |
return node | |
} else if (node instanceof Array) { | |
let result = '' | |
node.forEach((item) => { | |
result += this.renderNode(item) | |
}) | |
return result | |
} | |
let title, url, text | |
switch (node.type) { | |
case 'code': | |
text = this.renderNode(node.children) | |
return ` \`${text}\` ` | |
case 'strong': | |
text = this.renderNode(node.children) | |
return ` **${text}** ` | |
case 'url': | |
return node.children | |
case 'image': | |
const imageUrl = node.unit.content | |
return `` | |
case 'gyazo': | |
const imageUrl = node.unit.content | |
return `` | |
case 'imageLink': | |
return `[](${node.unit.link})` | |
case 'urlLink': | |
url = node.unit.link | |
title = node.unit.title | |
if (title === '') { | |
title = url | |
} | |
return `[${title}](${url})` | |
case 'icon': | |
title = node.unit.title | |
return ` *${title}* ` | |
case 'link': | |
title = node.unit.content | |
return title | |
url = this._baseUrl + title | |
return `<${url}|${title}>` | |
case 'hashTag': | |
return '' | |
url = this._baseUrl + node.unit.content | |
title = node.children | |
return `<${url}|${title}>` | |
case 'quote': | |
text = this.renderNode(node.children) | |
return node.unit.tag + text | |
case 'indent': | |
text = this.renderNode(node.children) | |
const indentSize = node.unit.tag.length | |
let pads = '' | |
for (let i = 0; i < indentSize; i++) { | |
pads += ' ' | |
} | |
return pads + '- ' + text | |
} | |
return this.renderNode(node.children) | |
} | |
set baseUrl (baseUrl) { | |
this._baseUrl = baseUrl | |
} | |
render (text) { | |
window.fetch('/kmc/祇園祭2016.parsed.json', {credentials: 'include'}).then(r => r.json()) | |
.then((res) => { | |
console.log(res.map((node) => { | |
return this.renderNode(node) | |
}).join('\n')) | |
}) | |
} | |
} | |
(new MdFormatter()).render() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment