UNPKG

3.07 kBJavaScriptView Raw
1var Marked = require('marked');
2var Cheerio = require('cheerio');
3var extend = require('util')._extend;
4
5module.exports = Styledown;
6
7var addClasses = require('./lib/filters').addClasses;
8var sectionize = require('./lib/filters').sectionize;
9var unpackExample = require('./lib/filters').unpackExample;
10var processConfig = require('./lib/filters').processConfig;
11var removeConfig = require('./lib/filters').removeConfig;
12var isolateTextBlocks = require('./lib/filters').isolateTextBlocks;
13var htmlize = require('./lib/utils').htmlize;
14var prefixClass = require('./lib/utils').prefixClass;
15
16/**
17 * Document.
18 */
19
20function Styledown (src, options) {
21 this.raw = src;
22 this.options = extend(extend({}, Styledown.defaults), options || {});
23 this.$ = Cheerio.load(Marked(src));
24
25 var highlightHTML = this._highlightHTML.bind(this);
26 var p = this.prefix.bind(this);
27
28 processConfig(src, this.options);
29 removeConfig(this.$);
30
31 var pre = this.options.prefix;
32
33 addClasses(this.$, p);
34 sectionize(this.$, 'h3', p, { 'class': p('block') });
35 sectionize(this.$, 'h2', p, { 'class': p('section'), until: 'h1, h2' });
36
37 this.$('pre').each(function () {
38 unpackExample(this, p, highlightHTML);
39 });
40
41 isolateTextBlocks(this.$, p);
42}
43
44Styledown.defaults = {
45
46 /**
47 * HTML template
48 */
49 template: [
50 "<!doctype html>",
51 "<html>",
52 "<head>",
53 "<meta charset='utf-8'>",
54 "<title>Styledown</title>",
55 "</head>",
56 "<body>",
57 "</body>",
58 "</html>"
59 ].join("\n"),
60
61 /**
62 * Things to put into `head`
63 */
64 head: false,
65
66 body: "<div sg-content></div>",
67
68 /**
69 * Prefix for classnames
70 */
71 prefix: 'sg',
72
73 /**
74 * Indentation spaces
75 */
76 indentSize: 2
77};
78
79/**
80 * Shorthand for parsing.
81 */
82
83Styledown.parseSync = function (source, options) {
84 return new Styledown(source, options).toHTML();
85};
86
87Styledown.parse = Styledown.parseSync;
88
89Styledown.prototype = {
90
91 /**
92 * Converts to HTML
93 */
94
95 toHTML: function() {
96 var html = this.$.html();
97
98 if (this.options.head !== false) {
99 // Unpack template
100 var $ = Cheerio.load(this.options.template);
101 $('body').append(htmlize(this.options.body));
102 $('[sg-content]').append(html).removeAttr('sg-content');
103 $('html, body').addClass(this.options.prefix);
104 $('head').append(htmlize(this.options.head));
105
106 html = $.html();
107 }
108
109 html = this._prettyprint(html);
110 return html;
111 },
112
113 /**
114 * Reindents HTML based on indent size option
115 */
116
117 _prettyprint: function (html) {
118 var Html = require('html');
119 return Html.prettyPrint(html, { indent_size: this.options.indentSize });
120 },
121
122 /**
123 * Syntax highlighting helper
124 */
125
126 _highlightHTML: function (html) {
127 var Hljs = require('highlight.js');
128
129 html = this._prettyprint(html);
130 html = Hljs.highlight('html', html).value;
131 return html;
132 },
133
134 /**
135 * Prefix classnames.
136 */
137
138 prefix: function(klass) {
139 return klass ?
140 prefixClass(klass, this.options.prefix) :
141 this.options.prefix;
142 }
143};