showdown.Converter.makeHtml.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * Created by Estevao on 15-01-2015.
  3. */
  4. describe('showdown.Converter', function () {
  5. 'use strict';
  6. require('source-map-support').install();
  7. require('chai').should();
  8. var fs = require('fs'),
  9. showdown = require('../../dist/showdown.js'),
  10. cases = fs.readdirSync('test/cases/')
  11. .filter(filter())
  12. .map(map('test/cases/')),
  13. issues = fs.readdirSync('test/issues/')
  14. .filter(filter())
  15. .map(map('test/issues/'));
  16. describe('Converter.options extensions', function () {
  17. showdown.extensions.testext = function () {
  18. return [{
  19. type: 'output',
  20. filter: function (text) {
  21. runCount = runCount + 1;
  22. return text;
  23. }
  24. }];
  25. };
  26. var runCount,
  27. converter = new showdown.Converter({extensions: ['testext']});
  28. it('output extensions should run once', function () {
  29. runCount = 0;
  30. converter.makeHtml('# testext');
  31. runCount.should.equal(1);
  32. });
  33. });
  34. function filter() {
  35. return function (file) {
  36. var ext = file.slice(-3);
  37. return (ext === '.md');
  38. };
  39. }
  40. function map(dir) {
  41. return function (file) {
  42. var name = file.replace('.md', ''),
  43. htmlPath = dir + name + '.html',
  44. html = fs.readFileSync(htmlPath, 'utf8'),
  45. mdPath = dir + name + '.md',
  46. md = fs.readFileSync(mdPath, 'utf8');
  47. return {
  48. name: name,
  49. input: md,
  50. expected: html
  51. };
  52. };
  53. }
  54. function assertion(testCase, converter) {
  55. return function () {
  56. testCase.actual = converter.makeHtml(testCase.input);
  57. testCase = normalize(testCase);
  58. // Compare
  59. testCase.actual.should.equal(testCase.expected);
  60. };
  61. }
  62. //Normalize input/output
  63. function normalize(testCase) {
  64. // Normalize line returns
  65. testCase.expected = testCase.expected.replace(/\r/g, '');
  66. testCase.actual = testCase.actual.replace(/\r/g, '');
  67. // Ignore all leading/trailing whitespace
  68. testCase.expected = testCase.expected.split('\n').map(function (x) {
  69. return x.trim();
  70. }).join('\n');
  71. testCase.actual = testCase.actual.split('\n').map(function (x) {
  72. return x.trim();
  73. }).join('\n');
  74. // Remove extra lines
  75. testCase.expected = testCase.expected.trim();
  76. // Convert whitespace to a visible character so that it shows up on error reports
  77. testCase.expected = testCase.expected.replace(/ /g, '·');
  78. testCase.expected = testCase.expected.replace(/\n/g, '•\n');
  79. testCase.actual = testCase.actual.replace(/ /g, '·');
  80. testCase.actual = testCase.actual.replace(/\n/g, '•\n');
  81. return testCase;
  82. }
  83. //Tests
  84. describe('makeHtml() output testcase', function () {
  85. var converter = new showdown.Converter();
  86. for (var i = 0; i < cases.length; ++i) {
  87. it(cases[i].name, assertion(cases[i], converter));
  88. }
  89. });
  90. describe('makeHtml() issues testcase', function () {
  91. var converter = new showdown.Converter();
  92. for (var i = 0; i < issues.length; ++i) {
  93. it(issues[i].name, assertion(issues[i], converter));
  94. }
  95. });
  96. describe('makeHtml() with option omitExtraWLInCodeBlocks', function () {
  97. var converter = new showdown.Converter({omitExtraWLInCodeBlocks: true}),
  98. text = 'var foo = bar;',
  99. html = converter.makeHtml(' ' + text);
  100. it('should omit extra line after code tag', function () {
  101. var expectedHtml = '<pre><code>' + text + '</code></pre>';
  102. html.should.equal(expectedHtml);
  103. });
  104. });
  105. describe('makeHtml() with option prefixHeaderId', function () {
  106. var converter = new showdown.Converter(),
  107. text = 'foo header';
  108. it('should prefix header id with "section"', function () {
  109. converter.setOption('prefixHeaderId', true);
  110. var html = converter.makeHtml('# ' + text),
  111. expectedHtml = '<h1 id="sectionfooheader">' + text + '</h1>';
  112. html.should.equal(expectedHtml);
  113. });
  114. it('should prefix header id with custom string', function () {
  115. converter.setOption('prefixHeaderId', 'blabla');
  116. var html = converter.makeHtml('# ' + text),
  117. expectedHtml = '<h1 id="blablafooheader">' + text + '</h1>';
  118. html.should.equal(expectedHtml);
  119. });
  120. });
  121. });