evilstreak / markdown-js

A Markdown parser for javascript
7.69k stars 863 forks source link

Passing line numbers up to consumers? #178

Closed cymen closed 10 years ago

cymen commented 10 years ago

So I want to use this project as part of another one I'm working on (literate-jasmine) and one of the things I would like to be able to do is access the line number that a code block came from in the original markdown file. I see internally that line numbers are kept track of up to a certain point. There is definitely some complexity here... What I would like to see is that each block has a map of line numbers in current block to line numbers in original markdown file. So say we had a code_block that was really multiple code blocks, it combined code block might look like:

var xyz = 10;
console.log('xyz');
var z = 20;

And the change would be to attach a map to it:

code_block.lineMap = {0: 17, 2: 20}

Which would mean line 1 of the code block is line 17 of the original markdown file and line 2 is 18 but line 3 is 20.

cymen commented 10 years ago

So my question is -- if I get this working, is this something that would be accepted into this module or is it outside the scope and I should fork?

My reason for wanting this is so I can say the exact line and column number in the markdown input that caused an exception in an evaluated code block.

evilstreak commented 10 years ago

Blank lines are preserved in code blocks and parsed into a single node in the JsonML tree. Could you expand your example input and output to clarify what you're aiming to do?

cymen commented 10 years ago

I want the original line number in the markdown file that a line of code in a code block came from.

cymen commented 10 years ago

It might be simpler to say: "I want the line number in the markdown file from which the current code block started on." I'm using markdown to write Jasmine specifications and if there is an exception due to code in the code block (say a reference error), I want to convey the exact location that the error originated from in the markdown file.

cymen commented 10 years ago

After some thought, I ended up switching to jshint for most of this -- it would still be useful to have a line numbers map but not nearly as much as before and it does seem like it would make things untidy.