showdown.Converter.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * Created by Estevao on 31-05-2015.
  3. */
  4. require('source-map-support').install();
  5. require('chai').should();
  6. require('sinon');
  7. var showdown = require('../bootstrap').showdown;
  8. describe('showdown.Converter', function () {
  9. 'use strict';
  10. describe('option methods', function () {
  11. var converter = new showdown.Converter();
  12. it('setOption() should set option foo=baz', function () {
  13. converter.setOption('foo', 'baz');
  14. });
  15. it('getOption() should get option foo to equal baz', function () {
  16. converter.getOption('foo').should.equal('baz');
  17. });
  18. it('getOptions() should contain foo=baz', function () {
  19. var options = converter.getOptions();
  20. options.should.have.ownProperty('foo');
  21. options.foo.should.equal('baz');
  22. });
  23. });
  24. describe('Converter.options extensions', function () {
  25. var runCount;
  26. showdown.extension('testext', function () {
  27. return [{
  28. type: 'output',
  29. filter: function (text) {
  30. runCount = runCount + 1;
  31. return text;
  32. }
  33. }];
  34. });
  35. var converter = new showdown.Converter({extensions: ['testext']});
  36. it('output extensions should run once', function () {
  37. runCount = 0;
  38. converter.makeHtml('# testext');
  39. runCount.should.equal(1);
  40. });
  41. });
  42. describe('metadata methods', function () {
  43. var converter = new showdown.Converter();
  44. it('_setMetadataPair() should set foo to bar', function () {
  45. converter._setMetadataPair('foo', 'bar');
  46. converter.getMetadata().should.eql({foo: 'bar'});
  47. });
  48. it('_setMetadata should set metadata to {baz: bazinga}', function () {
  49. converter._setMetadataRaw('{baz: bazinga}');
  50. converter.getMetadata(true).should.eql('{baz: bazinga}');
  51. });
  52. });
  53. describe('converter.setFlavor()', function () {
  54. /**
  55. * Test setFlavor('github')
  56. */
  57. describe('github', function () {
  58. var converter = new showdown.Converter(),
  59. ghOpts = showdown.getFlavorOptions('github');
  60. converter.setFlavor('github');
  61. for (var opt in ghOpts) {
  62. if (ghOpts.hasOwnProperty(opt)) {
  63. check(opt, ghOpts[opt]);
  64. }
  65. }
  66. function check (key, val) {
  67. it('should set ' + opt + ' to ' + val, function () {
  68. converter.getOption(key).should.equal(val);
  69. });
  70. }
  71. });
  72. });
  73. describe('getFlavor method', function () {
  74. // reset showdown
  75. showdown.setFlavor('vanilla');
  76. describe('flavor', function () {
  77. it('should be vanilla by default', function () {
  78. var converter = new showdown.Converter();
  79. converter.getFlavor().should.equal('vanilla');
  80. });
  81. it('should be changed if global option is changed', function () {
  82. showdown.setFlavor('github');
  83. var converter = new showdown.Converter();
  84. converter.getFlavor().should.equal('github');
  85. showdown.setFlavor('vanilla');
  86. });
  87. it('should not be changed if converter is initialized before global change', function () {
  88. var converter = new showdown.Converter();
  89. showdown.setFlavor('github');
  90. converter.getFlavor().should.equal('vanilla');
  91. showdown.setFlavor('vanilla');
  92. });
  93. });
  94. });
  95. describe('extension methods', function () {
  96. var extObjMock = {
  97. type: 'lang',
  98. filter: function () {}
  99. },
  100. extObjFunc = function () {
  101. return extObjMock;
  102. };
  103. it('addExtension() should add an extension Object', function () {
  104. var converter = new showdown.Converter();
  105. converter.addExtension(extObjMock);
  106. converter.getAllExtensions().language.should.contain(extObjMock);
  107. });
  108. it('addExtension() should unwrap an extension wrapped in a function', function () {
  109. var converter = new showdown.Converter();
  110. converter.addExtension(extObjFunc);
  111. converter.getAllExtensions().language.should.contain(extObjMock);
  112. });
  113. it('useExtension() should use a previous registered extension in showdown', function () {
  114. showdown.extension('foo', extObjMock);
  115. var converter = new showdown.Converter();
  116. converter.useExtension('foo');
  117. converter.getAllExtensions().language.should.contain(extObjMock);
  118. showdown.resetExtensions();
  119. });
  120. });
  121. describe('events', function () {
  122. var events = [
  123. 'anchors',
  124. 'autoLinks',
  125. 'blockGamut',
  126. 'blockQuotes',
  127. 'codeBlocks',
  128. 'codeSpans',
  129. 'githubCodeBlocks',
  130. 'headers',
  131. 'images',
  132. 'italicsAndBold',
  133. 'lists',
  134. 'paragraph',
  135. 'spanGamut'
  136. //'strikeThrough',
  137. //'tables'
  138. ];
  139. for (var i = 0; i < events.length; ++i) {
  140. runListener(events[i] + '.before');
  141. runListener(events[i] + '.after');
  142. }
  143. function runListener (name) {
  144. it('should listen to ' + name, function () {
  145. var converter = new showdown.Converter();
  146. converter.listen(name, function (evtName, text) {
  147. evtName.should.equal(name);
  148. text.should.match(/^[\s\S]*foo[\s\S]*$/);
  149. return text;
  150. })
  151. .makeHtml('foo');
  152. });
  153. }
  154. });
  155. });