CrossNox / m2r2

Markdown to reStructuredText converter
https://crossnox.github.io/m2r2
MIT License
107 stars 26 forks source link

mdinclude accepts / as reference to doc root #8

Open ITler opened 4 years ago

ITler commented 4 years ago

Really thank you for providing this library. I hope this will overcome my usage issues with recommonmark also in the long run.

It would be helpful, if .. mdinclude:: /_static/whatever.md would always starts looking on the doc root level (where sphinx-build command is executed) when reference starts with /. This would match the behaviour of sphinx's ..include: directive.

ravindk89 commented 3 years ago

Does anyone have a workaround for this? I can't really make good use of this plugin otherwise.

It seems like passing in something like .. mdinclude:: /source/imports/file.md results in the directive looking at the /source from the fs root instead of the project root.

I don't actually see anything different in how mdinclude handles finding the file compared to Include :

docutils/parsers/rst/directives/misc.py L56-66

if not self.state.document.settings.file_insertion_enabled:
   raise self.warning('"%s" directive disabled.' % self.name)

source = self.state_machine.input_lines.source(
   self.lineno - self.state_machine.input_offset - 1)

source_dir = os.path.dirname(os.path.abspath(source))

path = directives.path(self.arguments[0])

if path.startswith('<') and path.endswith('>'):
   path = os.path.join(self.standard_include_path, path[1:-1])

path = os.path.normpath(os.path.join(source_dir, path))

path = utils.relative_path(None, path)

path = nodes.reprunicode(path)

vs

if not self.state.document.settings.file_insertion_enabled:
   raise self.warning('"%s" directive disabled.' % self.name)

source = self.state_machine.input_lines.source(
   self.lineno - self.state_machine.input_offset - 1
)

source_dir = os.path.dirname(os.path.abspath(source))

path = rst.directives.path(self.arguments[0])

path = os.path.normpath(os.path.join(source_dir, path))

path = utils.relative_path(None, path)

path = nodes.reprunicode(path)

The code looks the same, but both directives behave completely differently. I don't know nearly enough about Sphinx API to dig much further than this :/ cc @CrossNox - I'd take a look at this but I'm not really sure what would be causing this behavior given how similar the code is between Include and MdInclude.