commonmark-hs icon indicating copy to clipboard operation
commonmark-hs copied to clipboard

Make source positions a parser option?

Open jgm opened this issue 5 years ago • 5 comments

Instead of handling it with typeclasses. This would allow simpler typeclasses: Html, Pandoc. It might also allow us to improve performance by avoiding the work of storing and computing ranges.

jgm avatar May 17 '20 17:05 jgm

One idea would be to add an Options data structure

data Options m =
  Options{
    optIncludeSourceRanges :: Bool,
    optWarn :: Warning -> m (),
    optAddToSourceMap :: Text -> SourceRange -> m ()
  }

optWarn would allow us to issue warnings for things like duplicate references in a flexible way (#48). optAddToSourceMap would allow us to construct source maps in a flexible way (#33). Putting these in options may be more flexible than using typeclasses. Defaults could be False, \_ -> return (), and \_ _ -> return ().

jgm avatar May 17 '20 19:05 jgm

We could use a reader monad transformer to make these available in block and inline parsers.

jgm avatar May 17 '20 19:05 jgm

I don't really care how they get there as long as the option is there! We're really looking forward to having a source maps available for vim-pandoc! We have a working prototype right now of a new syntax system using an external source map provided by pulldown-cmark which reports start and stop byte ranges. This is working great but we really need the full range of Pandoc extensions, not just CommonMark.

alerque avatar May 18 '20 08:05 alerque

The option is there already (as well as the ability to create a source map). This was just an idea about an alternative way to put it there.

jgm avatar May 18 '20 15:05 jgm

@alerque for a demo of the source map functionality, you can do commonmark --highlight -xall README.md. This does syntax highlighting with all the extensions enabled. To see how this is done see the code for commonmark-cli.

jgm avatar May 18 '20 16:05 jgm