const glob = require('glob');
const fs = require('fs');
const path = require('path');
const showdown  = require('showdown');
const converter = new showdown.Converter();
converter.setFlavor('github');
const camelCase = require('camel-case');
const titleCase = require('title-case');
const shell = require('shelljs');
const getFileNameFromPath = require('@lukeboyle/get-filename-from-path');

function sanitiseMarkdown(markdownBlock) {
	if (markdownBlock[0] !== 'code_block') {
		return markdownBlock;
	}

	return [
		markdownBlock[0],
		`{"${markdownBlock[1]}"}`
	]
}

// function getMarkupFromMarkdown(markdownString) {
// 	const parsedTree = markdown.parse(markdownString).map(sanitiseMarkdown);
//
// 	return `<div>${markdown.renderJsonML(markdown.toHTMLTree(parsedTree))}</div>`;
// }

function getMarkupFromMarkdown(markdownString) {
	return `<div>${converter.makeHtml(markdownString)}</div>`;
}

function generateComponent(post) {
	const fileName = getFileNameFromPath(post.path);

	return {
		path: post.path,
		fileName,
		component: `
			import React from 'react';
			import Helmet from 'react-helmet';
				
			export default class ${camelCase(fileName)} extends React.Component {
				render() {
					return (
						<div className="max-width-container">
							<Helmet>
								<title>${titleCase(fileName)} | Luke Boyle</title>
							</Helmet>
							${getMarkupFromMarkdown(post.contents)}
						</div>
					);
				}
			}
		`
	};
}

(() => {
	glob('blog-posts/**/*.md', {}, (err, files) => {
		const blogPosts = files.reduce((acc, curr) => {
			acc.push({
				path: curr,
				contents: fs.readFileSync(curr, {encoding: 'utf-8'})
			});

			return acc;
		}, []);

		const components = blogPosts.map(generateComponent);

		shell.rm('-rf', path.resolve(`${__dirname}/../src/pages/blog-posts`));

		components.forEach(component => {
			shell.mkdir('-p', path.resolve(`${__dirname}/../src/pages/${component.path.replace(`/${component.fileName}.md`, '')}`));
			fs.writeFileSync(path.resolve(`${__dirname}/../src/pages/${component.path.replace('.md', '.jsx')}`), component.component);
		});

		glob('./src/blog-posts/**/*.jsx', {}, (err, files) => {
			console.error(err);

			const pageCount = Math.ceil(files.length / 5);
		});
	});
})();
@@ -89,17 +89,14 @@
 nst 
-showdown 
+marked
  = r
@@ -103,24 +103,22 @@
 equire('
-showdown
+marked
 ');%0Acons
@@ -119,23 +119,22 @@
 ;%0Aconst 
-conv
+rend
 er
-t
 er = new
@@ -138,58 +138,24 @@
 new 
-showdown.Converter();%0Aconv
+marked.Rend
 er
-t
 er
-.setFlavor('github'
+(
 );%0Ac
@@ -348,404 +348,602 @@
 );%0A%0A
-function sanitiseMarkdown(markdownBlock) %7B%0A%09if (markdownBlock%5B0%5D !== 'code_block') %7B%0A%09%09return markdownBlock;%0A%09%7D%0A%0A%09return %5B%0A%09%09markdownBlock%5B0%5D,%0A%09%09%60%7B%22$%7BmarkdownBlock%5B1%5D%7D%22%7D%60%0A%09%5D%0A%7D%0A%0A// function getMarkupFromMarkdown(markdownString) %7B%0A// %09const parsedTree = markdown.parse(markdownString).map(sanitiseMarkdown);%0A//%0A// %09return %60%3Cdiv%3E$%7Bmarkdown.renderJsonML(markdown.toHTMLTree(parsedTree))%7D%3C/div%3E%60;%0A// %7D
+renderer.code = function(code, language) %7B%0A%09console.log(code.split('%5Cn'));%0A%09return %60%3Cpre%3E%3Ccode%3E%0A%09%09$%7Bcode.split('%5Cn').map(codeBlock =%3E %7B%0A%09%09%09console.log(codeBlock);%0A%09%09const codeWithEscapedQuotes = codeBlock.split('%22').join('%5C%5C%22');%0A%09%09const codeWithEscapedHashes = codeWithEscapedQuotes.split('#').join('%5C%5C#');%0A%09%09const codeWithEscapedCurlies = codeWithEscapedHashes.split('%7B').join('%5C%5C%7B').split('%7D').join('%5C%5C%7D');%0A%09%09return %60%3Cspan%3E%7B%22$%7BcodeWithEscapedCurlies%7D%22%7D%3C/span%3E%60;%0A%09%7D).join('')%7D%0A%09%3C/code%3E%3C/pre%3E%60;%0A%7D;%0A%0Arenderer.image = function(href, title, text) %7B%0A%09return %60%3Cimg src=%22$%7Bhref%7D%22 alt=%22$%7Btext%7D%22/%3E%60;%0A%7D;
 %0A%0Afu
@@ -1009,26 +1009,14 @@
 v%3E$%7B
-converter.
 ma
+r
 ke
-Html
+d
 (mar
@@ -1026,16 +1026,49 @@
 wnString
+, %7Brenderer: renderer, gfm: true%7D
 )%7D%3C/div%3E

add markdown parsing blocks for react