showdown.Converter.makeHtml.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. function filter() {
  17. return function (file) {
  18. var ext = file.slice(-3);
  19. return (ext === '.md');
  20. };
  21. }
  22. function map(dir) {
  23. return function (file) {
  24. var name = file.replace('.md', ''),
  25. htmlPath = dir + name + '.html',
  26. html = fs.readFileSync(htmlPath, 'utf8'),
  27. mdPath = dir + name + '.md',
  28. md = fs.readFileSync(mdPath, 'utf8');
  29. return {
  30. name: name,
  31. input: md,
  32. expected: html
  33. };
  34. };
  35. }
  36. function assertion(testCase, converter) {
  37. return function () {
  38. testCase.actual = converter.makeHtml(testCase.input);
  39. testCase = normalize(testCase);
  40. // Compare
  41. testCase.actual.should.equal(testCase.expected);
  42. };
  43. }
  44. //Normalize input/output
  45. function normalize(testCase) {
  46. // Normalize line returns
  47. testCase.expected = testCase.expected.replace(/\r/g, '');
  48. testCase.actual = testCase.actual.replace(/\r/g, '');
  49. // Ignore all leading/trailing whitespace
  50. testCase.expected = testCase.expected.split('\n').map(function (x) {
  51. return x.trim();
  52. }).join('\n');
  53. testCase.actual = testCase.actual.split('\n').map(function (x) {
  54. return x.trim();
  55. }).join('\n');
  56. // Remove extra lines
  57. testCase.expected = testCase.expected.trim();
  58. // Convert whitespace to a visible character so that it shows up on error reports
  59. testCase.expected = testCase.expected.replace(/ /g, '·');
  60. testCase.expected = testCase.expected.replace(/\n/g, '•\n');
  61. testCase.actual = testCase.actual.replace(/ /g, '·');
  62. testCase.actual = testCase.actual.replace(/\n/g, '•\n');
  63. return testCase;
  64. }
  65. //Tests
  66. describe('makeHtml() output testcase', function () {
  67. var converter = new showdown.Converter();
  68. for (var i = 0; i < cases.length; ++i) {
  69. it(cases[i].name, assertion(cases[i], converter));
  70. }
  71. });
  72. describe('makeHtml() issues testcase', function () {
  73. var converter = new showdown.Converter();
  74. for (var i = 0; i < issues.length; ++i) {
  75. it(issues[i].name, assertion(issues[i], converter));
  76. }
  77. });
  78. describe('makeHtml() with option omitExtraWLInCodeBlocks', function () {
  79. var converter = new showdown.Converter({omitExtraWLInCodeBlocks: true}),
  80. text = 'var foo = bar;',
  81. html = converter.makeHtml(' ' + text);
  82. it('should omit extra line after code tag', function () {
  83. var expectedHtml = '<pre><code>' + text + '</code></pre>';
  84. html.should.equal(expectedHtml);
  85. });
  86. });
  87. describe('makeHtml() with option prefixHeaderId', function () {
  88. var converter = new showdown.Converter(),
  89. text = 'foo header';
  90. it('should prefix header id with "section"', function () {
  91. converter.setOption('prefixHeaderId', true);
  92. var html = converter.makeHtml('# ' + text),
  93. expectedHtml = '<h1 id="sectionfooheader">' + text + '</h1>';
  94. html.should.equal(expectedHtml);
  95. });
  96. it('should prefix header id with custom string', function () {
  97. converter.setOption('prefixHeaderId', 'blabla');
  98. var html = converter.makeHtml('# ' + text),
  99. expectedHtml = '<h1 id="blablafooheader">' + text + '</h1>';
  100. html.should.equal(expectedHtml);
  101. });
  102. });
  103. });