-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstyleguide.js
90 lines (72 loc) · 2.74 KB
/
styleguide.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
(function styleguide(){
var categories = [];
[].forEach.call( document.querySelectorAll('[data-markdown]'), function(elem, i){
// modified from https://gist.github.com/paulirish/1343518
// strip leading whitespace so it isn't evaluated as code
var text = elem.innerHTML.replace(/\n\s*\n/g,'\n'),
// set indentation level so your markdown can be indented within your HTML
leadingws = text.match(/^\n?(\s*)/)[1].length,
regex = new RegExp('\\n?\\s{' + leadingws + '}','g'),
md = text.replace(regex,'\n'),
html = marked(md);
elem.innerHTML = html;
// add in the example code using jQuery
var codeElem = $(elem).find('code');
var code = $.parseHTML(codeElem.text());
codeElem.parent().before(code);
codeElem.attr('id', 'code-block-' + i);
// add copy button
codeElem.before('<button class="copy" role="presentation" data-clipboard-target="#code-block-' + i + '" title="Copy">✄</button>');
// add the category to the sections index
var category = $(elem).data('category');
if ($.inArray(category, categories) === -1) {
categories.push(category);
}
// hide this section if it isn't part of the first category in the array
if (category !== categories[0]) {
$(elem).hide();
}
});
// init copy to clipboard buttons
new Clipboard('.copy');
// build section tabs
$.each(categories, function(i, category) {
var readableName = category.replace(/-/g, " ");
if (i === 0) {
$('#category-tabs').append('<li class="active"><a href="#" data-category="' + category + '">' + readableName + '</a></li>');
} else {
$('#category-tabs').append('<li><a href="#" data-category="' + category + '">' + readableName + '</a></li>');
}
});
// handle tab click
$('#category-tabs a').click(function(event) {
var category = $(this).data('category');
event.preventDefault();
$(this).parent().siblings().removeClass('active');
$(this).parent().addClass('active');
$('[data-markdown]').each(function(){
if ($(this).data('category') !== category) {
$(this).hide();
} else {
$(this).show();
}
});
});
// prevent example clicks going elsewhere
$('[data-markdown] a').click(function(event) {
event.preventDefault();
});
// scroll to top button position
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll < 100) {
$('#scroll-to-top').removeClass('fixed');
} else {
$('#scroll-to-top').addClass('fixed');
}
});
$('#scroll-to-top a').click(function(event) {
event.preventDefault();
$(window).scrollTop(0);
});
}());