UNPKG

3.3 kBJavaScriptView Raw
1var extend = require('util')._extend;
2var parseCodeText = require('./utils').parseCodeText;
3var parseTags = require('./utils').parseTags;
4var prefixClass = require('./utils').prefixClass;
5var htmlize = require('./utils').htmlize;
6
7/**
8 * Adds classes
9 */
10
11exports.addClasses = function ($, pre) {
12 $("*").addClass(pre());
13};
14
15/**
16 * Break it apart into sections.
17 *
18 * Puts <h3> blocks into <section> blocks.
19 */
20
21exports.sectionize = function ($, tag, pre, options) {
22 options = extend({
23 'class': '',
24 'until': 'h1, h2, h3, section',
25 }, options);
26
27 $(tag).each(function (i) {
28 var $heading = $(this);
29 var $extras = $heading.nextUntil(options.until);
30 $heading.before("<section class='"+options.class+"'>");
31
32 var $div = $("section."+options.class).eq(-1);
33 $div.addClass(pre('section-' + $heading.attr('id')));
34 $div.append($heading.remove());
35 $div.append($extras.remove());
36 });
37};
38
39/**
40 * Unpacks `pre` blocks into examples.
41 */
42
43exports.unpackExample = function (parent, pre, highlight) {
44 var Cheerio = require('cheerio');
45 var code = parent.text();
46 var block = parseCodeText(code);
47 var tags = parseTags(block.tag);
48
49 if (tags.example) {
50 var html = htmlize(block.code);
51 var canvas = "<div class='"+pre('canvas')+"'>"+html+"</div>";
52 var codeblock = "<pre class='"+pre('code')+"'>"+highlight(html)+"</pre>";
53 var $block = Cheerio.load("<div class='"+pre('example')+"'>" + canvas + codeblock + "</div>");
54
55 if (tags['class']) {
56 klass = pre(tags['class']);
57 $block(':root').addClass(klass);
58 }
59
60 parent.after($block.root());
61 parent.remove();
62 } else {
63 klass = parent.find('code').attr('class');
64 var m = klass.match(/lang-([a-z]+)/);
65
66 if (m) {
67 var lang = m[1];
68 var Hljs = require('highlight.js');
69 parent.html(Hljs.highlight(lang, parent.text()).value);
70 parent.addClass(pre('lang-'+lang));
71 parent.addClass(pre('code'));
72 }
73 }
74};
75
76/**
77 * Remove the configuration block.
78 *
79 * Removes the "Styleguide options" block from the DOM in `$`.
80 */
81
82exports.removeConfig = function ($) {
83 var $h1 = $('h1#styleguide-options');
84 $h1.nextUntil('h1').remove();
85 $h1.remove();
86};
87
88/**
89 * Process the configuration block
90 */
91
92exports.processConfig = function (src, options) {
93 var Mdconf = require('./mdconf');
94 try {
95 var data = Mdconf(src, { normalizer: 'camelcase' });
96 data = (data && data.styleguideOptions);
97
98 if (data) extend(options, data);
99 } catch (e) {
100 // Don't bother if mdconf fails.
101 }
102};
103
104/**
105 * Isolates text blocks
106 */
107
108exports.isolateTextBlocks = function ($, pre) {
109 var Cheerio = require('cheerio');
110
111 $('.'+pre('block')).each(function() {
112 var $this = $(this);
113 // Check if there's an example block.
114 // $('.sg-example', this).length doesn't work.
115 var noExample = ($this.html().indexOf(pre('example')) === -1);
116 var noCode = ($this.html().indexOf(pre('code')) === -1);
117 if (noExample && noCode) return;
118
119 var $first = $('h3', this);
120 var $text = $first.nextUntil('.'+pre('example')+', .'+pre('code'));
121
122 var $block = Cheerio.load('<div>');
123 $this.prepend($block.root());
124
125 $block(':root').addClass(pre('text'));
126 $block(':root').append($first);
127 $block(':root').append($text);
128 });
129};