11ty / eleventy

A simpler site generator. Transforms a directory of templates (of varying types) into HTML.
https://www.11ty.dev/
MIT License
16.86k stars 487 forks source link

Is there an existing way to know the path of the Markdown file in Markdown-it (and its plugins)? #2883

Closed nhoizey closed 1 year ago

nhoizey commented 1 year ago

I'm using a bunch of Markdown-it plugins, and face an issue with markdown-it-image-size which looks for an image in my project's root folder instead of the folder where the Markdown file is.

Is there an existing way to know the path of the Markdown file in Markdown-it (and its plugins)?

I might use it with markdown-it-replace-link to add the Markdown path to the image, for example.

nhoizey commented 1 year ago

It looks like there is a solution described here:

@manunamz do you know any Markdown-it plugin that already used this?

manunamz commented 1 year ago

Hiya @nhoizey !

Yep! markdown-it-wikirefs is setup to look for urls in a configurable way via its resolveHtmlHref option.

You can also see it in action in eleventy-wikibonsai, which gives several examples of how to access the inputPath in its configuration.

Since you might be looking for image urls, I ended up doing something sort of hacky to just get it up and running, but it works if files are placed where the code expects them to be.

nhoizey commented 1 year ago

@manunamz thanks a lot, I'll look at these examples! 🙏

nhoizey commented 1 year ago

And now there's also markdown-it-image-size thanks to @boyum:

It works without adding anything to my code, just adding the plugin. 🤩