-
-
Save wefiy/ac4f82f4ac63d7edb52e477c778fea2b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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