All files / src/plugins dataTable.ts

96.55% Statements 28/29
87.5% Branches 7/8
100% Functions 4/4
96.29% Lines 26/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145        1x 1x 1x   1x                                                                                                                                                                                   1x         1x 1x 1x   1x 3x 3x   3x       3x 3x   3x 3x 1x 2x 1x 1x 1x     3x 3x 3x       3x     1x                
import cheerio from 'cheerio';
import { FrontMatter, PluginContext } from './Plugin.js';
import { markdownIt as md } from '../lib/markdown-it/index.js';
 
const CSS_FILE_NAME = 'dataTableAssets/datatables.min.css';
const CSS_ADDITIONAL = 'dataTableAssets/datatables-additional.css';
const JS_FILE_NAME = 'dataTableAssets/datatables.min.js';
 
const initScript = `
  <script>
    function getTableOptions(el) {
      const options = {};
      if ($(el).hasClass('sortable-table')) {
        options.searching = false;
        options.paging = false;
        options.info = false;
      } else if ($(el).hasClass('searchable-table')) {
        options.ordering = false;
        options.paging = false;
        options.info = false;
        options.dom = '<"row"<"col-sm-12"f>>' + '<"row"<"col-sm-12"t>>';
      } else if ($(el).hasClass('sortable-searchable-table')) {
        options.paging = false;
        options.info = false;
        options.dom = '<"row"<"col-sm-12"f>>' + '<"row"<"col-sm-12"t>>';
      }
      return options;
    }
 
    function initializeTables(tables) {
      if (!tables || tables.length === 0) return;
      tables.forEach(table => {
        const options = getTableOptions(table);
        $(table).DataTable(options);
        $(table).addClass('dt-processed');
      });
    }
 
    function setupTableObserver() {
      const observer = new MutationObserver((mutations) => {
        let newTables = [];
        
        mutations.forEach(mutation => {
          if (mutation.addedNodes.length) {
            mutation.addedNodes.forEach(node => {
 
              if (node.nodeType === 1 && node.tagName === 'TABLE' && 
                  !$(node).hasClass('dt-processed') &&
                  ($(node).hasClass('sortable-table') || 
                   $(node).hasClass('searchable-table') || 
                   $(node).hasClass('sortable-searchable-table'))) {
                newTables.push(node);
              }
              
              if (node.nodeType === 1 && node.querySelectorAll) {
                const tablesInNode = node.querySelectorAll(
                'table.sortable-table:not(.dt-processed), ' +
                'table.searchable-table:not(.dt-processed), ' +
                'table.sortable-searchable-table:not(.dt-processed)');
                if (tablesInNode.length) {
                  newTables = [...newTables, ...tablesInNode];
                }
              }
            });
          }
        });
 
        if (newTables.length > 0) {
          initializeTables(newTables);
        }
      });
      
      // Start observing the entire document for changes
      observer.observe(document.body, {
        childList: true,
        subtree: true
      });
      
      return observer;
    }
 
    document.addEventListener('DOMContentLoaded', function() {
      // Process any existing tables
      const existingTables = document.querySelectorAll(
        'table.sortable-table:not(.dt-processed), ' +
        'table.searchable-table:not(.dt-processed), ' +
        'table.sortable-searchable-table:not(.dt-processed)'
      );
      if (existingTables.length > 0) {
        initializeTables(existingTables);
      }
      
      setupTableObserver();
    });
 
  </script>
`;
 
const getLinks = () => [
  `<link rel="stylesheet" href="${CSS_FILE_NAME}">`,
  `<link rel="stylesheet" href="${CSS_ADDITIONAL}">`,
];
 
const getScripts = () => [`<script src="${JS_FILE_NAME}"></script>`, initScript];
const postRender = (pluginContext: PluginContext, frontmatter: FrontMatter, content: string) => {
  const $ = cheerio.load(content);
 
  $('d-table').each((index: number, node: cheerio.Element) => {
    const $node = $(node);
    const html = $node.html();
 
    Iif (html == null) {
      return;
    }
 
    const isSortable = $node.attr('sortable') !== undefined;
    const isSearchable = $node.attr('searchable') !== undefined;
 
    let tableClass: string = '';
    if (isSortable && isSearchable) {
      tableClass = 'sortable-searchable-table';
    } else if (isSortable) {
      tableClass = 'sortable-table';
    } else if (isSearchable) {
      tableClass = 'searchable-table';
    }
 
    const renderedTable = md.render(html);
    const $renderedTable = $(renderedTable);
    $renderedTable.find('table')
      .addClass(tableClass)
      .attr('id', `datatable-${index}`);
 
    $node.replaceWith($renderedTable);
  });
 
  return $.html();
};
 
export {
  postRender,
  getScripts,
  getLinks,
};