table.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. var markdownTable = require('markdown-table');
  3. module.exports = table;
  4. /* Stringify table.
  5. *
  6. * Creates a fenced table by default, but not in
  7. * `looseTable: true` mode:
  8. *
  9. * Foo | Bar
  10. * :-: | ---
  11. * Baz | Qux
  12. *
  13. * NOTE: Be careful with `looseTable: true` mode, as a
  14. * loose table inside an indented code block on GitHub
  15. * renders as an actual table!
  16. *
  17. * Creates a spaced table by default, but not in
  18. * `spacedTable: false`:
  19. *
  20. * |Foo|Bar|
  21. * |:-:|---|
  22. * |Baz|Qux|
  23. */
  24. function table(node) {
  25. var self = this;
  26. var options = self.options;
  27. var loose = options.looseTable;
  28. var spaced = options.spacedTable;
  29. var pad = options.paddedTable;
  30. var stringLength = options.stringLength;
  31. var rows = node.children;
  32. var index = rows.length;
  33. var exit = self.enterTable();
  34. var result = [];
  35. var start;
  36. var end;
  37. while (index--) {
  38. result[index] = self.all(rows[index]);
  39. }
  40. exit();
  41. if (loose) {
  42. start = '';
  43. end = '';
  44. } else if (spaced) {
  45. start = '| ';
  46. end = ' |';
  47. } else {
  48. start = '|';
  49. end = '|';
  50. }
  51. return markdownTable(result, {
  52. align: node.align,
  53. pad: pad,
  54. start: start,
  55. end: end,
  56. stringLength: stringLength,
  57. delimiter: spaced ? ' | ' : '|'
  58. });
  59. }