forked from arturadib/strapdown
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrapdown.js
142 lines (118 loc) · 4.27 KB
/
strapdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
;(function(window, document) {
//////////////////////////////////////////////////////////////////////
//
// Shims for IE < 9
//
document.head = document.getElementsByTagName('head')[0];
if (!('getElementsByClassName' in document)) {
document.getElementsByClassName = function(name) {
function getElementsByClassName(node, classname) {
var a = [];
var re = new RegExp('(^| )'+classname+'( |$)');
var els = node.getElementsByTagName("*");
for(var i=0,j=els.length; i<j; i++)
if(re.test(els[i].className))a.push(els[i]);
return a;
}
return getElementsByClassName(document.body, name);
}
}
//////////////////////////////////////////////////////////////////////
//
// Get user elements we need
//
var markdownEl = document.getElementsByTagName('xmp')[0] || document.getElementsByTagName('textarea')[0],
titleEl = document.getElementsByTagName('title')[0],
scriptEls = document.getElementsByTagName('script'),
navbarEl = document.getElementsByClassName('navbar')[0];
if (!markdownEl) {
console.warn('No embedded Markdown found in this document for Strapdown.js to work on! Visit http://strapdownjs.com/ to learn more.');
return;
}
// Hide body until we're done fiddling with the DOM
document.body.style.display = 'none';
//////////////////////////////////////////////////////////////////////
//
// <head> stuff
//
// Use <meta> viewport so that Bootstrap is actually responsive on mobile
var metaEl = document.createElement('meta');
metaEl.name = 'viewport';
metaEl.content = 'width=device-width, initial-scale=1';
if (document.head.firstChild)
document.head.insertBefore(metaEl, document.head.firstChild);
else
document.head.appendChild(metaEl);
// Get origin of script
var origin = '';
for (var i = 0; i < scriptEls.length; i++) {
if (scriptEls[i].src.match('strapdown')) {
origin = scriptEls[i].src;
}
}
var originBase = origin.substr(0, origin.lastIndexOf('/'));
// Get theme
var theme = markdownEl.getAttribute('theme') || 'bootstrap';
theme = theme.toLowerCase();
// Get navbar color
var bg = markdownEl.getAttribute('navbar') || 'primary';
bg = bg.toLowerCase();
if ( bg == "primary" ) {
var navbar = " navbar-dark";
} else {
var navbar = " navbar-" + bg;
}
bg = " bg-" + bg;
// Stylesheets
var linkEl = document.createElement('link');
linkEl.href = originBase + '/themes/'+theme+'.min.css';
linkEl.rel = 'stylesheet';
document.head.appendChild(linkEl);
var linkEl = document.createElement('link');
linkEl.href = originBase + '/strapdown.css';
linkEl.rel = 'stylesheet';
document.head.appendChild(linkEl);
//////////////////////////////////////////////////////////////////////
//
// <body> stuff
//
var markdown = markdownEl.textContent || markdownEl.innerText;
var newNode = document.createElement('main');
newNode.className = 'container';
newNode.id = 'content';
document.body.replaceChild(newNode, markdownEl);
// Insert navbar if there's none
var newNode = document.createElement('nav');
newNode.className = 'navbar navbar-expand-lg fixed-top' + navbar + bg;
if (!navbarEl && titleEl) {
newNode.innerHTML = '<div class="container"> <div id="headline" class="navbar-brand"> </div> </div>';
document.body.insertBefore(newNode, document.body.firstChild);
var title = titleEl.innerHTML;
var headlineEl = document.getElementById('headline');
if (headlineEl)
headlineEl.innerHTML = title;
}
//////////////////////////////////////////////////////////////////////
//
// Markdown!
//
// Generate Markdown
var html = marked.parse(markdown);
document.getElementById('content').innerHTML = html;
// Prettify
var codeEls = document.getElementsByTagName('code');
for (var i=0, ii=codeEls.length; i<ii; i++) {
var codeEl = codeEls[i];
var lang = codeEl.className;
codeEl.className = 'prettyprint lang-' + lang;
}
prettyPrint();
// Style tables
var tableEls = document.getElementsByTagName('table');
for (var i=0, ii=tableEls.length; i<ii; i++) {
var tableEl = tableEls[i];
tableEl.className = 'table table-striped table-bordered';
}
// All done - show body
document.body.style.display = '';
})(window, document);