'use strict';

var gulp = require('gulp');
var path = require('path');
var _ = require('lodash');
var fs = require('fs');

var $ = require('gulp-load-plugins')({
	pattern: ['gulp-*', 'del']
});

module.exports = function(options) {
	function markdown(files,dest,folder,env){
		return function markdown(){
			var stream = gulp.src(files)
			.pipe($.markdown({
				highlight: function(code) {
					return require('highlight.js').highlightAuto(code).value;
				},
				header: true
			}));

			_.each([
				'String',
				'Number',
				'Boolean',
				'Object',
				'Array',
				'Phaser.Group',
			],function(val,i){
				stream.pipe($.replace(new RegExp("<"+val+">",'g'), "<span class='paramter-type'>&lt;"+val+"&gt;</span>"))
			})

			stream.pipe($.cheerio(function ($$, file) {
				// var firstTitle = $$('h1').eq(0).text();
				// if(!firstTitle) firstTitle=path.basename(file.path,path.extname(file.path));
				var firstTitle=path.basename(file.path,path.extname(file.path));

				file.path = path.join(path.dirname(file.path),
					folder,
					firstTitle.replace(/[\$|\.]/g,''),
					'/index'+path.extname(file.path)
				);
			}))
			// .pipe($.if(function(){
			// 	return env=='dist'
			// }, $.replace('src="images/', 'src="../src/images/')))
			.pipe(gulp.dest(dest));

			return stream;
		}
	}


	gulp.task('clean:docs', function (done) {
		return $.del([
			options.tmp + '/site/posts',
			options.tmp + '/site/portfolio-posts',
			options.tmp + '/site/partials',
		],{force:true});
	});

	gulp.task('markdown:portfolio', gulp.series(markdown([
			'src/portfolio-posts/*.md',
		],
		options.tmp+'/site',
		"/portfolio-posts/"
	)));

	gulp.task('markdown:posts', gulp.series(markdown([
			'src/posts/*.md',
		],
		options.tmp+'/site',
		"/posts/"
	)));

	gulp.task('markdown:mainPage', gulp.series(markdown([
			'src/partials/main.md',
		],
		options.tmp+'/site',
		"/partials/"
	)));

	// gulp.task('markdown:mainPage', function(){
	// 	return gulp.src(['src/partials/main.md'])
	// 	.pipe($.markdown({
	// 		highlight: function(code) {
	// 			return require('highlight.js').highlightAuto(code).value;
	// 		},
	// 		header: true
	// 	}))
	// 	.pipe(gulp.dest(options.tmp+'/site/partials'));
	// });

	gulp.task('markdown', gulp.series(
		'clean:docs',
		gulp.parallel(
			'markdown:portfolio',
			'markdown:mainPage',
			'markdown:posts'
		)
	));

};

@@ -262,19 +262,26 @@
 ,folder,
-env
+main=false
 )%7B%0A%09%09ret
@@ -494,246 +494,20 @@
 %0A%09%09%09
-_.each(%5B%0A%09%09%09%09'String',%0A%09%09%09%09'Number',%0A%09%09%09%09'Boolean',%0A%09%09%09%09'Object',%0A%09%09%09%09'Array',%0A%09%09%09%09'Phaser.Group',%0A%09%09%09%5D,function(val,i)%7B%0A%09%09%09%09stream.pipe($.replace(new RegExp(%22%3C%22+val+%22%3E%22,'g'), %22%3Cspan class='paramter-type'%3E&lt;%22+val+%22&gt;%3C/span%3E%22))%0A%09%09%09%7D)%0A%0A
+if(!main)%7B%0A%09
 %09%09%09s
@@ -545,24 +545,25 @@
 $$, file) %7B%0A
+%09
 %09%09%09%09// var f
@@ -601,16 +601,17 @@
 ();%0A%09%09%09%09
+%09
 // if(!f
@@ -686,16 +686,17 @@
 ));%0A%09%09%09%09
+%09
 var firs
@@ -753,16 +753,17 @@
 ath));%0A%0A
+%09
 %09%09%09%09file
@@ -814,16 +814,17 @@
 %09%09%09%09
+%09
 folder,%0A
 %09%09%09%09
@@ -819,16 +819,17 @@
 folder,%0A
+%09
 %09%09%09%09%09fir
@@ -860,16 +860,17 @@
 /g,''),%0A
+%09
 %09%09%09%09%09'/i
@@ -907,136 +907,36 @@
 %09%09%09%09
+%09
 );%0A
+%09
 %09%09%09%7D))
+;
 %0A%09%09%09
-// .pipe($.if(function()%7B%0A%09%09%09// %09return env=='dist'%0A%09%09%09// %7D, $.replace('src=%22images/', 'src=%22../src/images/')))%0A%09%09%09
+%7D%0A%0A%09%09%09stream
 .pip
@@ -1548,22 +1548,16 @@
 p+'/site
-',%0A%09%09%22
 /partial
@@ -1561,330 +1561,37 @@
 ials
-/%22%0A%09)));%0A%0A%09// gulp.task('markdown:mainPage', function()%7B%0A%09// %09return gulp.src(%5B'src/partials/main.md'%5D)%0A%09// %09.pipe($.markdown(%7B%0A%09// %09%09highlight: function(code) %7B%0A%09// %09%09%09return require('highlight.js').highlightAuto(code).value;%0A%09// %09%09%7D,%0A%09// %09%09header: true%0A%09// %09%7D))%0A%09// %09.pipe(gulp.dest(options.tmp+'/site/partials'));%0A%09// %7D
+',%0A%09%09%22/partials/%22,%0A%09%09true%0A%09))
 );%0A%0A
@@ -1653,16 +1653,14 @@
 ulp.
-parallel
+series
 (%0A%09%09

clean markdown code