bem-contrib / md-to-bemjson

Converts markdown to bemjson
MIT License
7 stars 1 forks source link

line number/position in output? #55

Closed benbenbenbenbenben closed 4 years ago

benbenbenbenbenben commented 4 years ago

I'm experimenting with this project to compile markdown as part of a documentation-driven development project I'm working on.

Is it possible (easy?) to get position information in the output of convert?

benbenbenbenbenben commented 4 years ago

Okay, perhaps to answer my own question - I wrote a basic plugin inline that did what I needed.

I could then pickout the position data form the data property of the node.

If anyone else needs this:

const visit = require(`unist-util-visit`)
///
const bem = filename => require(`md-to-bemjson`).convertSync(require(`fs`).readFileSync(filename), {
    plugins: [
        {
            plugin: () => (tree, file) => {
                visit(tree, node => {
                    node.data = {
                        start: node.position.start,
                        end: node.position.end
                    }
                })
            }, options: {}
        }
    ]
})