Skip to content
This repository has been archived by the owner on Jul 20, 2019. It is now read-only.

Commit

Permalink
Add marked lib to enable markdown importing
Browse files Browse the repository at this point in the history
  • Loading branch information
Vittorio Vittori committed Sep 26, 2014
1 parent 79674ac commit 4a45ffc
Show file tree
Hide file tree
Showing 6 changed files with 1,310 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
// Custom Globals // additional predefined global variables
"globals" : {
"Handlebars" : true,
"Include" : true
"Include" : true,
"marked" : true
},

"maxerr" : 50, // {int} Maximum error before stopping
Expand Down
39 changes: 28 additions & 11 deletions include.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*!
GitHub: https://github.com/vitto/includejs
License: MIT Licence
Version: 1.0.0
Date: 2014-08-25
Version: 1.1.0
Date: 2014-09-26
Author: Vittorio Vittori
Website: http://vit.to
*/
Expand All @@ -11,6 +11,7 @@ var Include = (function () {

"use strict";

var useMarkdown = false;
var useHandlebars = false;
var elements = [];
var requests = [];
Expand All @@ -22,7 +23,6 @@ var Include = (function () {
var findElements = function() {
elements = getElements();
requests = [];
//console.log("Notice: found " + elements.length + " element/s");
if (elements.length > 0) {
loadElements();
} else {
Expand All @@ -31,7 +31,6 @@ var Include = (function () {
};

var loadElements = function() {
//console.log("Notice: started load elements...");
var i;
for (i = 0; elements.length > i; i += 1) {
appendRequest(elements[i], i);
Expand All @@ -44,18 +43,22 @@ var Include = (function () {
isLoaded : false
};

//console.log(element.parentNode);

requests[i].client.open("GET", element.getAttribute("src"));

requests[i].client.ext = getExtension(element.getAttribute("src"));
requests[i].client.element = element;
requests[i].client.id = i;
requests[i].client.onloadend = function(e) {
var element, htmlString;
var element, htmlString, extension;

element = e.currentTarget.element;
extension = e.currentTarget.ext;

htmlString = getCompiledHTML(e.currentTarget.responseText, element);
if (extension === "html" || extension === "htm") {
htmlString = getCompiledHTML(e.currentTarget.responseText, element);
} else if (extension === "md" || extension === "markdown") {
htmlString = getCompiledMarkdown(e.currentTarget.responseText, element);
}

element.insertAdjacentHTML("beforeBegin", htmlString);
element.parentNode.removeChild(element);
Expand All @@ -68,11 +71,9 @@ var Include = (function () {
};

requests[i].client.send();
//console.log(requests[i].client);
};

var checkRequestsStatus = function() {
//console.log("Notice: checking if all elemnts are loaded");
var i;
for (i = 0; requests.length > i; i += 1) {
if (!requests[i].isLoaded) {
Expand All @@ -82,6 +83,10 @@ var Include = (function () {
return true;
};

var getExtension = function(filename) {
return filename.substr(filename.lastIndexOf(".") + 1).toLowerCase();
};

var getCompiledHTML = function(htmlString, element) {
var template, textContent;
textContent = element.textContent.trim();
Expand All @@ -93,6 +98,14 @@ var Include = (function () {
}
};

var getCompiledMarkdown = function(markdownString) {
if (useMarkdown) {
return marked(markdownString);
} else {
return markdownString;
}
};

var getJSON = function(textContent) {
try {
var jsonData = eval("(" + textContent + ")");
Expand All @@ -110,14 +123,18 @@ var Include = (function () {

return {
init : function() {
//console.log("Notice: document loaded");
if (typeof Handlebars !== "undefined") {
useHandlebars = true;
//console.log("Notice: found Handlebars js library");
} else {
useHandlebars = false;
//console.log("Warning: Handlebars is NOT loaded and will be not used to render HTML include elements");
}
if (typeof marked !== "undefined") {
useMarkdown = true;
} else {
useMarkdown = false;
}
findElements();
},
onLoad : function() {
Expand Down
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<meta name="description" content="IncludeJS is a library to include HTML partials, useful for static templates">

<script src="../vendor/handlebars-v1.3.0.js"></script>
<script src="../vendor/marked.js"></script>
<script src="../include.js"></script>

<!--[if lt IE 9]>
Expand Down
4 changes: 4 additions & 0 deletions test/template/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
"message" : "Hello world!"
}
</include>
<br>
<div class="sub-content">
<include src="template/example.md"></include>
</div>
</div>
3 changes: 3 additions & 0 deletions test/template/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
##Markdown block

with some text
Loading

0 comments on commit 4a45ffc

Please # to comment.