1 | var Marked = require('marked');
|
2 | var Cheerio = require('cheerio');
|
3 | var extend = require('util')._extend;
|
4 |
|
5 | module.exports = Styledown;
|
6 |
|
7 | var addClasses = require('./lib/filters').addClasses;
|
8 | var sectionize = require('./lib/filters').sectionize;
|
9 | var unpackExample = require('./lib/filters').unpackExample;
|
10 | var processConfig = require('./lib/filters').processConfig;
|
11 | var removeConfig = require('./lib/filters').removeConfig;
|
12 | var isolateTextBlocks = require('./lib/filters').isolateTextBlocks;
|
13 | var htmlize = require('./lib/utils').htmlize;
|
14 | var prefixClass = require('./lib/utils').prefixClass;
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function 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 |
|
44 | Styledown.defaults = {
|
45 |
|
46 | |
47 |
|
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 |
|
63 |
|
64 | head: false,
|
65 |
|
66 | body: "<div sg-content></div>",
|
67 |
|
68 | |
69 |
|
70 |
|
71 | prefix: 'sg',
|
72 |
|
73 | |
74 |
|
75 |
|
76 | indentSize: 2
|
77 | };
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | Styledown.parseSync = function (source, options) {
|
84 | return new Styledown(source, options).toHTML();
|
85 | };
|
86 |
|
87 | Styledown.parse = Styledown.parseSync;
|
88 |
|
89 | Styledown.prototype = {
|
90 |
|
91 | |
92 |
|
93 |
|
94 |
|
95 | toHTML: function() {
|
96 | var html = this.$.html();
|
97 |
|
98 | if (this.options.head !== false) {
|
99 |
|
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 |
|
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 |
|
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 |
|
136 |
|
137 |
|
138 | prefix: function(klass) {
|
139 | return klass ?
|
140 | prefixClass(klass, this.options.prefix) :
|
141 | this.options.prefix;
|
142 | }
|
143 | };
|