mixu/markdown-styles

Links to internal anchors (or implicit anchors) cause error

nazrhyn opened this issue · 9 comments

In convert-md.js, when the renderer.link function is called on a link that looks like this:

1. [Connections](#connections)

Its call to url.parse produces something like this (assigned to parsed):

{
  protocol: null,
  slashes: null,
  auth: null,
  host: null,
  port: null,
  hostname: null,
  hash: '#connections',
  search: null,
  query: null,
  pathname: null,
  path: null,
  href: '#connections'
}

parsed.pathname is then passed to path.extname which fails with the following because pathname is null.

TypeError: Path must be a string. Received null

I use links like that to link to the implicit anchors provided by the ids generated for heading elements. Changing line 58 to...

if (!parsed.protocol && parsed.pathname) {

...fixes the problem. Not sure if that's more shotgun than you'd want, though.

+1
i fixed it with
var ext = path.extname(parsed.pathname || '');
should i send a pr with this fix?

I hope someone can comment on this and hopefully push a fix. it's almost blocking for us now!

I'd be happy to do a PR to resolve this, I just need some interaction to let me know what the maintainers would like.

mixu commented

@nazrhyn sorry for the delay! A PR would be great!

mixu commented

@nazrhyn I went ahead and fixed this, thanks @a7madgamal fir reporting and sorry for the delay again!

Published as v3.1.8

thx all <3

@mixu I will say that I think that doing the || '' is a bit disingenuous. It is known, by the time url.parse is called, that there is no pathname. There's no reason to even execute path.extname or run the if statement below that.

/shrug

But that's just a stylistic point. Thanks for the fix!

tbh I didn't look much into the code. I just tried to fix an error that was blocking a build and that's why I didn't send a pr with that hack :)

assuming I was the inspiration for the fix :D