1
0
mirror of https://github.com/ikatyang/emoji-cheat-sheet.git synced 2024-11-22 10:23:51 +01:00
emoji-cheat-sheet/scripts/markdown.js
2017-03-18 17:14:04 +08:00

61 lines
1.7 KiB
JavaScript

const format = str => str.trim().replace(/^ +/mg, '');
module.exports = class Markdown {
static create(urls, title, emojiTable, columnDivisions) {
const categories = Object.keys(emojiTable);
const urlDescriptions = Object.keys(urls).map((site) => `[${site}](${urls[site]})`).join(' and ');
return format(`
# ${title}
[![build](https://travis-ci.org/ikatyang/emoji-cheat-sheet.svg?branch=generator)](https://travis-ci.org/ikatyang/emoji-cheat-sheet)
This cheat sheet is auto-generated from ${urlDescriptions} using [emoji-cheat-sheet-generator](https://github.com/ikatyang/emoji-cheat-sheet/tree/generator).
## Table of Contents
${categories.map(category => `- [${category}](#${category.toLowerCase()})`).join('\n')}
${
categories.map(category => {
const emojis = emojiTable[category];
return format(`
### ${category}
${this.createTable(emojis, columnDivisions)}
`);
}).join(('\n').repeat(2))
}
`);
}
static createTableHead(columnDivisions) {
return format(`
| |${(' ico | emoji |').repeat(columnDivisions)}
| - |${(' --- | ----- |').repeat(columnDivisions)}
`);
}
static createTable(emojis, columnDivisions) {
let table = this.createTableHead(columnDivisions) + '\n';
for (let i = 0; i < emojis.length; i += columnDivisions) {
const rowEmojis = emojis.slice(i, i + columnDivisions);
while (rowEmojis.length < columnDivisions)
rowEmojis.push('');
table += format(`
| [top](#table-of-contents) |${rowEmojis.map((emoji) => emoji ? ` :${emoji}: | \`:${emoji}:\` ` : ' | ').join(' | ')}|
`) + '\n';
}
return table;
}
};