SHARE
TWEET

Untitled

a guest Oct 12th, 2017 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2. const remark = require('remark');
  3. const md = fs.readFileSync('./fixtures/alert.md');
  4.  
  5. const ast = remark().parse(md);
  6.  
  7. function getCellValue(node) {
  8.   return node.children[0].children[0].value;
  9. }
  10.  
  11. function sort(nodes) {
  12.   return nodes.sort((prev, next) => {
  13.     prev = getCellValue(prev);
  14.     next = getCellValue(next);
  15.  
  16.     return prev > next;
  17.   });
  18. }
  19.  
  20. ast.children.forEach(child => {
  21.   let staticProp = [];
  22.   // prefix with `on`
  23.   let dynamicProp = [];
  24.  
  25.   if (child.type === 'table') {
  26.     // slice will create new array, so sort can affect the original array
  27.     // slice(1) cut down the thead
  28.     child.children.slice(1).forEach(node => {
  29.       let value = getCellValue(node);
  30.       if (/^on[A-Z]/.test(value)) {
  31.         dynamicProp.push(node);
  32.       } else {
  33.         staticProp.push(node);
  34.       }
  35.     });
  36.  
  37.     child.children = [
  38.       child.children[0],
  39.       ...sort(staticProp),
  40.       ...sort(dynamicProp)
  41.     ];
  42.   }
  43. });
  44.  
  45. console.log(remark().stringify(ast));
RAW Paste Data
Top