Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- const remark = require('remark');
- const md = fs.readFileSync('./fixtures/alert.md');
- const ast = remark().parse(md);
- function getCellValue(node) {
- return node.children[0].children[0].value;
- }
- function sort(nodes) {
- return nodes.sort((prev, next) => {
- prev = getCellValue(prev);
- next = getCellValue(next);
- return prev > next;
- });
- }
- ast.children.forEach(child => {
- let staticProp = [];
- // prefix with `on`
- let dynamicProp = [];
- if (child.type === 'table') {
- // slice will create new array, so sort can affect the original array
- // slice(1) cut down the thead
- child.children.slice(1).forEach(node => {
- let value = getCellValue(node);
- if (/^on[A-Z]/.test(value)) {
- dynamicProp.push(node);
- } else {
- staticProp.push(node);
- }
- });
- child.children = [
- child.children[0],
- ...sort(staticProp),
- ...sort(dynamicProp)
- ];
- }
- });
- console.log(remark().stringify(ast));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement