|
7 | 7 |
|
8 | 8 | 'use strict';
|
9 | 9 |
|
10 |
| -var should = require('should'); |
| 10 | +require('mocha'); |
| 11 | +var assert = require('assert'); |
11 | 12 | var handlebars = require('handlebars');
|
12 |
| -var _ = require('lodash'); |
| 13 | +var Templates = require('templates'); |
| 14 | +var hljs = require('highlight.js'); |
13 | 15 | var md = require('..');
|
| 16 | +var _ = require('lodash'); |
| 17 | +var app; |
14 | 18 |
|
15 |
| -var Template = require('template'); |
16 |
| -var template; |
17 |
| - |
18 |
| - |
19 |
| -describe('md helper', function() { |
| 19 | +describe('sync', function() { |
20 | 20 | beforeEach(function() {
|
21 |
| - template = new Template(); |
22 |
| - template.helper('md', md); |
| 21 | + app = new Templates(); |
23 | 22 |
|
24 |
| - template.engine('md', require('engine-base')); |
| 23 | + app.helper('md', md.sync); |
| 24 | + app.engine('md', require('engine-base')); |
| 25 | + app.option('engine', 'md'); |
25 | 26 |
|
26 |
| - template.create('page'); |
27 |
| - template.create('partial', {isPartial: true}); |
28 |
| - template.create('include', {isPartial: true}); |
| 27 | + app.create('page'); |
| 28 | + app.create('partial', {viewType: ['partial']}); |
| 29 | + app.create('include', {viewType: ['partial']}); |
29 | 30 |
|
30 |
| - template.include('one', {content: '# heading <%= name %>', name: 'one'}); |
31 |
| - template.partial('two', {content: '# heading <%= name %>', name: 'two'}); |
32 |
| - |
33 |
| - template.page('home.md', { |
34 |
| - content: '<%= md("one") %>' |
35 |
| - }); |
| 31 | + app.include('one', {content: '# heading <%= name %>', data: {name: 'one'}}); |
| 32 | + app.partial('two', {content: '# heading <%= name %>', data: {name: 'two'}}); |
36 | 33 | });
|
37 | 34 |
|
38 |
| - it('should render markdown on the `content` property for the specified template:', function(cb) { |
39 |
| - template.render('home.md', function(err, content) { |
40 |
| - content.should.equal('<h1>heading one</h1>\n'); |
| 35 | + it('should convert markdown on the `content` property of a template to HTML:', function(cb) { |
| 36 | + app.page('home.md', {content: '<%= md("one") %>'}); |
| 37 | + |
| 38 | + app.render('home.md', function(err, view) { |
| 39 | + if (err) return cb(err); |
| 40 | + assert.equal(view.content, '<h1>heading one</h1>\n'); |
41 | 41 | cb();
|
42 | 42 | });
|
43 | 43 | });
|
44 | 44 |
|
45 | 45 | it('should support rendering markdown from a file:', function() {
|
46 |
| - md('test/fixtures/a.md').should.equal('<h1>AAA</h1>\n<blockquote>\n<p>this is aaa</p>\n</blockquote>\n'); |
| 46 | + assert.equal(md.sync('test/fixtures/a.md'), '<h1>AAA</h1>\n<blockquote>\n<p>this is aaa</p>\n</blockquote>\n'); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('handlebars:', function() { |
| 50 | + it('should support rendering markdown from a file:', function() { |
| 51 | + handlebars.registerHelper('md', md.sync); |
| 52 | + assert.equal(handlebars.compile('{{{md "test/fixtures/a.md"}}}')(), '<h1>AAA</h1>\n<blockquote>\n<p>this is aaa</p>\n</blockquote>\n'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should use the `render` function passed on the locals to render templates in partials :', function() { |
| 56 | + handlebars.registerHelper('md', md.sync); |
| 57 | + var locals = {name: 'CCC', compile: handlebars.compile}; |
| 58 | + assert.equal(handlebars.compile('{{{md "test/fixtures/c.md"}}}')(locals), '<h1>CCC</h1>\n<p>This is CCC</p>\n'); |
| 59 | + }); |
47 | 60 | });
|
48 | 61 | });
|
49 | 62 |
|
50 |
| -describe('handlebars:', function() { |
51 |
| - it('should support rendering markdown from a file:', function() { |
52 |
| - handlebars.registerHelper('md', md); |
53 |
| - handlebars.compile('{{{md "test/fixtures/a.md"}}}')().should.equal('<h1>AAA</h1>\n<blockquote>\n<p>this is aaa</p>\n</blockquote>\n'); |
| 63 | +describe('async', function() { |
| 64 | + beforeEach(function() { |
| 65 | + app = new Templates(); |
| 66 | + |
| 67 | + app.asyncHelper('md', md); |
| 68 | + app.engine('md', require('engine-base')); |
| 69 | + app.option('engine', 'md'); |
| 70 | + |
| 71 | + app.create('page'); |
| 72 | + app.create('partial', {viewType: ['partial']}); |
| 73 | + app.create('include', {viewType: ['partial']}); |
| 74 | + |
| 75 | + app.include('one', {content: '# heading <%= name %>', data: {name: 'one'}}); |
| 76 | + app.partial('two', {content: '# heading <%= name %>', data: {name: 'two'}}); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should convert markdown on the `content` property of a template to HTML:', function(cb) { |
| 80 | + app.page('home.md', {content: '<%= md("one") %>'}); |
| 81 | + |
| 82 | + app.render('home.md', function(err, view) { |
| 83 | + if (err) return cb(err); |
| 84 | + assert.equal(view.content, '<h1>heading one</h1>\n'); |
| 85 | + cb(); |
| 86 | + }); |
54 | 87 | });
|
55 | 88 |
|
56 |
| - it('should use the `render` function passed on the locals to render templates in partials :', function() { |
57 |
| - handlebars.registerHelper('md', md); |
58 |
| - var locals = {name: 'CCC', render: handlebars.compile}; |
59 |
| - handlebars.compile('{{{md "test/fixtures/c.md"}}}')(locals).should.equal('<h1>CCC</h1>\n<p>This is CCC</p>\n'); |
| 89 | + it('should support rendering from a file', function(cb) { |
| 90 | + app.page('home.md', {content: '<%= md("test/fixtures/d.md") %>'}); |
| 91 | + |
| 92 | + app.render('home.md', {name: 'DDD'}, function(err, view) { |
| 93 | + if (err) return cb(err); |
| 94 | + assert.equal(view.content, '<h1>DDD</h1>\n<p>This is DDD</p>\n'); |
| 95 | + cb(); |
| 96 | + }); |
60 | 97 | });
|
61 | 98 | });
|
62 | 99 |
|
63 | 100 | describe('lodash:', function() {
|
64 | 101 | it('should work as a lodash mixin:', function() {
|
65 |
| - _.mixin({md: md}); |
66 |
| - _.template('<%= _.md("test/fixtures/a.md") %>', {}).should.equal('<h1>AAA</h1>\n<blockquote>\n<p>this is aaa</p>\n</blockquote>\n'); |
| 102 | + _.mixin({md: md.sync}); |
| 103 | + assert.equal(_.template('<%= _.md("test/fixtures/a.md") %>', {})(), '<h1>AAA</h1>\n<blockquote>\n<p>this is aaa</p>\n</blockquote>\n'); |
67 | 104 | });
|
68 | 105 |
|
69 | 106 | it('should work when passed to lodash on the locals:', function() {
|
70 |
| - _.template('<%= _.md("test/fixtures/a.md") %>', {md: md}).should.equal('<h1>AAA</h1>\n<blockquote>\n<p>this is aaa</p>\n</blockquote>\n'); |
| 107 | + assert.equal(_.template('<%= _.md("test/fixtures/a.md") %>')({md: md.sync}), '<h1>AAA</h1>\n<blockquote>\n<p>this is aaa</p>\n</blockquote>\n'); |
71 | 108 | });
|
72 | 109 |
|
73 | 110 | it('should work as a lodash import:', function() {
|
74 |
| - var settings = {imports: {md: md}}; |
75 |
| - _.template('<%= _.md("test/fixtures/a.md") %>', {}, settings).should.equal('<h1>AAA</h1>\n<blockquote>\n<p>this is aaa</p>\n</blockquote>\n'); |
| 111 | + var settings = {imports: {md: md.sync}}; |
| 112 | + assert.equal(_.template('<%= _.md("test/fixtures/a.md") %>', {}, settings)(), '<h1>AAA</h1>\n<blockquote>\n<p>this is aaa</p>\n</blockquote>\n'); |
76 | 113 | });
|
77 | 114 | });
|
78 | 115 |
|
79 | 116 | describe('highlight:', function(argument) {
|
80 |
| - var hljs = require('highlight.js'); |
81 |
| - it('should support syntax highlighting', function() { |
82 |
| - md('test/fixtures/e.md', {highlight: function highlight(code, lang) { |
| 117 | + it('should support syntax highlighting', function() { |
| 118 | + var actual = md.sync('test/fixtures/e.md', { |
| 119 | + highlight: function(code, lang) { |
| 120 | + try { |
83 | 121 | try {
|
84 |
| - try { |
85 |
| - return hljs.highlight(lang, code).value; |
86 |
| - } catch (err) { |
87 |
| - if (!/Unknown language/i.test(err.message)) { |
88 |
| - throw err; |
89 |
| - } |
90 |
| - return hljs.highlightAuto(code).value; |
91 |
| - } |
| 122 | + return hljs.highlight(lang, code).value; |
92 | 123 | } catch (err) {
|
93 |
| - return code; |
| 124 | + if (!/Unknown language/i.test(err.message)) { |
| 125 | + throw err; |
| 126 | + } |
| 127 | + return hljs.highlightAuto(code).value; |
94 | 128 | }
|
| 129 | + } catch (err) { |
| 130 | + return code; |
95 | 131 | }
|
96 |
| - }).should.equal('<h1>EEE</h1>\n<pre><code><span class="hljs-keyword">var</span> <span class="hljs-keyword">message</span> = <span class="hljs-string">\'This is an alert\'</span>;\nalert(<span class="hljs-keyword">message</span>);\n</code></pre>\n'); |
| 132 | + } |
97 | 133 | });
|
| 134 | + assert.equal(actual, '<h1>EEE</h1>\n<pre><code><span class="hljs-keyword">var</span> <span class="hljs-keyword">message</span> = <span class="hljs-string">\'This is an alert\'</span>;\nalert(<span class="hljs-keyword">message</span>);\n</code></pre>\n'); |
| 135 | + }); |
98 | 136 | });
|
0 commit comments