-
Hello, I would like to sort the following Markdown lists, maintain nesting, and not affect the rest of the content. What I have: # Animals
- Zoo Animals
- Herbivores
- Zebra
- Gazelle
- Carnivores
- Tiger
- Lion
- Omnivores
- Gorilla
- Baboon
- Chimpanzee
- Domestic Animals
- Felines
- Tabby
- Bengal
- Siamese
- Canines
- German Shepherd
- Cocker Spaniel Result I am aiming for: # Animals
- Domestic Animals
- Canines
- Cocker Spaniel
- German Shepherd
- Felines
- Bengal
- Siamese
- Tabby
- Zoo Animals
- Carnivores
- Lion
- Tiger
- Herbivores
- Gazelle
- Zebra
- Omnivores
- Baboon
- Chimpanzee
- Gorilla The following is as far as I have been able to get: const text =
"# Animals\n" +
"- Zoo Animals\n" +
" - Herbivores\n" +
" - Zebra\n" +
" - Gazelle\n" +
" - Carnivores\n" +
" - Tiger\n" +
" - Lion\n" +
" - Omnivores\n" +
" - Gorilla\n" +
" - Baboon\n" +
" - Chimpanzee\n" +
"- Domestic Animals\n" +
" - Felines\n" +
" - Tabby\n" +
" - Bengal\n" +
" - Siamese\n" +
" - Canines\n" +
" - German Shepherd\n" +
" - Cocker Spaniel\n";
const unified = require("unified");
const markdown = require("remark-parse");
const tree = unified().use(markdown).parse(text);
console.log("raw", tree);
const visit = require("unist-util-visit");
visit(tree, "list", (node) => {
console.log("walked", node);
visit(node, "text", (textNode) => console.log(textNode.value));
}); I hope that's enough info to go off of. Been banging my head against this for a couple of hours now so any help is appreciated. Thank you! |
Beta Was this translation helpful? Give feedback.
Answered by
ylor
Jan 29, 2021
Replies: 1 comment 2 replies
-
Solved this and it was simpler than I thought. 😅 const text =
"# Animals\n" +
"- Zoo Animals\n" +
" - Herbivores\n" +
" - Zebra\n" +
" - Gazelle\n" +
" - Carnivores\n" +
" - Tiger\n" +
" - Lion\n" +
" - Omnivores\n" +
" - Gorilla\n" +
" - Baboon\n" +
" - Chimpanzee\n" +
"- Domestic Animals\n" +
" - Felines\n" +
" - Tabby\n" +
" - Bengal\n" +
" - Siamese\n" +
" - Canines\n" +
" - German Shepherd\n" +
" - Cocker Spaniel\n";
const unified = require("unified");
const remarkParse = require("remark-parse");
const remarkStringify = require("remark-stringify");
const visit = require("unist-util-visit");
// parse markdown into mdast tree
const tree = unified().use(remarkParse).parse(text);
// visit each list node and sort children based on children[0].children[0].value
visit(tree, "list", (node) => {
node.children.sort(function (a, b) {
// Normalize casing for sorting
let listItemA = a.children[0].children[0].value.toLowerCase();
let listItemB = b.children[0].children[0].value.toLowerCase();
let comparison = 0;
if (listItemA > listItemB) {
comparison = 1;
} else if (listItemA < listItemB) {
comparison = -1;
}
return comparison;
});
});
// serialize from mdast back to markdown
const doc = unified().use(remarkStringify).stringify(tree);
console.log(doc); |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
ylor
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solved this and it was simpler than I thought. 😅