empty-thead.js 998 B

12345678910111213141516171819202122232425262728293031323334
  1. /**
  2. * Plugin for Remarkable Markdown processor which removes empty thead of tables
  3. */
  4. module.exports = function(md, options) {
  5. function removeEmptyThead(state) {
  6. const tokens = state.tokens;
  7. let thead = -1;
  8. let empty = true;
  9. for (let i = tokens.length - 1; i >= 0; i--) {
  10. const tok = tokens[i];
  11. switch (tok.type) {
  12. case 'thead_close':
  13. thead = i;
  14. break;
  15. case 'thead_open':
  16. if (empty) {
  17. tokens.splice(i, thead - i + 1);
  18. }
  19. thead = -1;
  20. empty = true;
  21. break;
  22. case 'inline':
  23. if (thead !== -1 && tok.content.length > 0) {
  24. empty = false;
  25. }
  26. break;
  27. }
  28. }
  29. }
  30. md.core.ruler.after('block', 'empty_thead', removeEmptyThead);
  31. };