These are chat archives for wooorm/remark

26th
Aug 2016
Khoa Nguyen
@thangngoc89
Aug 26 2016 14:22
hi
I'm trying to create a custom plugin for this : :point_up: August 22, 2016 1:43 AM
I can' t find a API where I can loop through each node 1 by 1 by order
Khoa Nguyen
@thangngoc89
Aug 26 2016 14:26
:+1: thanks
Ben Briggs
@ben-eb
Aug 26 2016 14:27
Pleasure
Khoa Nguyen
@thangngoc89
Aug 26 2016 14:32
@ben-eb could you take a look at this
Ben Briggs
@ben-eb
Aug 26 2016 14:32
What's up?
Input is a ton of links and headings
Output is just text node
oops :D
my fault
haha
Ben Briggs
@ben-eb
Aug 26 2016 14:33
:D
Khoa Nguyen
@thangngoc89
Aug 26 2016 14:36
looks like I'm pretty dump about AST and stuff.
this is what I'm planning
I have a markdown like this
### Heading

[1 link](link)
[2 link](link)
[3 link](link)
4 text
* [link](link)
* [link](link)
desired output
Khoa Nguyen
@thangngoc89
Aug 26 2016 14:41
{
  name: "Heading"
  children: [{
      name: "1 link",
      link: "link"
    },
    {
      name: "2 link",
      link: "link"
    },
    {
      name: "3 link",
      link: "link"
    },
    {
      name: "4 link",
      // no link here, 
      children: [
        // more link here
      ]


    },

  ]
}
I'm planing to loop through each headings and push them to the tree
Ben Briggs
@ben-eb
Aug 26 2016 14:42
Ah in that case you might want https://github.com/azu/unist-util-map instead
All of these are useful across remark/retext etc https://github.com/wooorm/unist#list-of-utilities
Khoa Nguyen
@thangngoc89
Aug 26 2016 14:43
the original markdown syntax was a flat structure
that what's making this difficult :D
who compose a markdown document like that :'(
Ben Briggs
@ben-eb
Aug 26 2016 14:44
Yeah I can imagine
Khoa Nguyen
@thangngoc89
Aug 26 2016 14:45
I stuck at first step: move everything between 2 headings into their children.
Khoa Nguyen
@thangngoc89
Aug 26 2016 14:50
OMG >_<
I forgot the most important part
they was ordered
### Heading 0
0.0
0.1
0.2
0.3
I don't even need remark for this job
Ben Briggs
@ben-eb
Aug 26 2016 14:54
;)
Khoa Nguyen
@thangngoc89
Aug 26 2016 14:54
Sometime I'm so dump like this. haha
Ben Briggs
@ben-eb
Aug 26 2016 14:55
Sometimes I like to load in JSON files using fs and forgetting that require works with JSON too :D
Khoa Nguyen
@thangngoc89
Aug 26 2016 14:55
really ?
damn
Ben Briggs
@ben-eb
Aug 26 2016 14:55
Heh yep, I've done that in the past
Khoa Nguyen
@thangngoc89
Aug 26 2016 14:56
so I can just require('package.json')
Ben Briggs
@ben-eb
Aug 26 2016 14:56
Yep :)
Khoa Nguyen
@thangngoc89
Aug 26 2016 14:56
wow :100:
Ben Briggs
@ben-eb
Aug 26 2016 14:56
See. I just saved you half an hour of writing fs calls ;)
Khoa Nguyen
@thangngoc89
Aug 26 2016 14:56
lol :cool:
Ben Briggs
@ben-eb
Aug 26 2016 14:57
But yeah I know that one and yet I still think right I need to do fs.readFile and then callbacks etc etc
Khoa Nguyen
@thangngoc89
Aug 26 2016 14:57
I even have a small utility to require those json file
Ben Briggs
@ben-eb
Aug 26 2016 14:57
I like doing import {version} from './package.json'; to destructure it too
Khoa Nguyen
@thangngoc89
Aug 26 2016 14:57
we are human man
not machine
Ben Briggs
@ben-eb
Aug 26 2016 14:58
Yeah, my general point is that sometimes we all overthink problems
Khoa Nguyen
@thangngoc89
Aug 26 2016 14:58
agree
We should teach eslint to yell at us each time we do a fs call for json file
Ben Briggs
@ben-eb
Aug 26 2016 15:01
This is also a thing too :) https://github.com/shinnn/json-to-es-module
Khoa Nguyen
@thangngoc89
Aug 26 2016 15:02
Seriously ?
you can just prepend export default and it works
eslint --fix can take care of the linting problem
Ben Briggs
@ben-eb
Aug 26 2016 15:07
I guess it's to avoid big json files ending up in a bundle, right
Tree-shaking doesn't apply to a blob of JSON but it works on ES6 modules
Khoa Nguyen
@thangngoc89
Aug 26 2016 15:08
that. I think it's the case
it prepends export default
Ben Briggs
@ben-eb
Aug 26 2016 15:09
Yep
Ben Briggs
@ben-eb
Aug 26 2016 15:24
I'm sure there'll be someone who wants to write an ESLint plugin for requiring JSON :)