Here is example markdown file
# Test
## First List
* Hello World
* Lorem
* foo
## Second List
- item
## Third List
+ item 1
part of item 1
+ item 2
## Not a List
bla bla bla
## Empty
## Another List
bla bla bla
bla
* ITEM
## Nested List
### Inside Nested
* foo
* bar
I have this code so far:
const markdown = await fs.promises.readFile(path.join(__dirname, 'test.md'), 'utf8');
const regexp = /^#{1,6} (.*)[.
]*[*-+] (.*)/gm;
const result = markdown.matchAll(regexp);
console.log([...result].map(m => m.slice(1)));
[
[ 'First List', 'Hello World' ],
[ 'Second List', 'item' ],
[ 'Third List', 'item 1' ],
[ 'Inside Nested', 'foo' ]
]
First issue is that it's only grabbing first item, Second is that if item is multiline it will only grab first line, and finally it doesn't include Another List
because there text in between heading and list.
I'm pretty new to regexp and not sure if current regexp I have is even safe to use.
So basically I want to find every list in markdown file put it items in an array then look if there a heading any were above and not another list of some kind and then put that heading in a beginning of that array (all thought it's not necessary have to be in that format, could be object too, I just thought array would be simpler)
Desired result:
[
['First List', 'Hello World', 'Lorem', 'foo'],
['Second List', 'item'],
['Third List', 'item 1
part of item 1', 'item 2'],
['Another List', 'ITEM'],
['Inside Nested', 'foo', 'bar']
]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…