UNPKG

3.26 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.replaceWith($block.root());
61 } else {
62 klass = parent.find('code').attr('class');
63 var m = klass.match(/lang-([a-z]+)/);
64
65 if (m) {
66 var lang = m[1];
67 var Hljs = require('highlight.js');
68 parent.html(Hljs.highlight(lang, parent.text()).value);
69 parent.addClass(pre('lang-'+lang));
70 parent.addClass(pre('code'));
71 }
72 }
73};
74
75/**
76 * Remove the configuration block.
77 *
78 * Removes the "Styleguide options" block from the DOM in `$`.
79 */
80
81exports.removeConfig = function ($) {
82 var $h1 = $('h1#styleguide-options');
83 $h1.nextUntil('h1').remove();
84 $h1.remove();
85};
86
87/**
88 * Process the configuration block
89 */
90
91exports.processConfig = function (src, options) {
92 var Mdconf = require('./mdconf');
93 try {
94 var data = Mdconf(src, { normalizer: 'camelcase' });
95 data = (data && data.styleguideOptions);
96
97 if (data) extend(options, data);
98 } catch (e) {
99 // Don't bother if mdconf fails.
100 }
101};
102
103/**
104 * Isolates text blocks
105 */
106
107exports.isolateTextBlocks = function ($, pre) {
108 var Cheerio = require('cheerio');
109
110 $('.'+pre('block')).each(function() {
111 // Check if there's an example block.
112 // $('.sg-example', this).length doesn't work.
113 var noExample = (this.html().indexOf(pre('example')) === -1);
114 var noCode = (this.html().indexOf(pre('code')) === -1);
115 if (noExample && noCode) return;
116
117 var $first = $('h3', this);
118 var $text = $first.nextUntil('.'+pre('example')+', .'+pre('code'));
119
120 var $block = Cheerio.load('<div>');
121 this.prepend($block.root());
122
123 $block(':root').addClass(pre('text'));
124 $block(':root').append($first);
125 $block(':root').append($text);
126 });
127};