Skip to content

Instantly share code, notes, and snippets.

@wefiy
Forked from monkindey/alert.md
Created October 12, 2017 01:04
Show Gist options
  • Select an option

  • Save wefiy/ac4f82f4ac63d7edb52e477c778fea2b to your computer and use it in GitHub Desktop.

Select an option

Save wefiy/ac4f82f4ac63d7edb52e477c778fea2b to your computer and use it in GitHub Desktop.
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));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment