question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Include directive: Allow separate includes for reference links and reference definition

See original GitHub issue

Describe the bug

foo.md

<!-- contents-begin-md -->

Hello world [Test][test].

<!-- contents-end-md -->

MyST-Parser is awesome!!!

<!-- references-begin-md -->

[test]: http://example.com/

<!-- references-end-md -->

bar.rst

.. contents-begin-rst

Hello world `Test <test_>`__

.. contents-end-rst

.. references-begin-rst

.. _test: http://example.com/

.. references-end-rst

1.

When I include parts of foo.md in another md.

```{include} foo.md
---
start-after: <!-- contents-begin-md -->
end-before: <!-- contents-end-md -->
---
```

```{include} foo.md
---
start-after: <!-- references-begin-md -->
end-before: <!-- references-end-md -->
---
```

When rendering you get where the reference is lost:

Hello world [Test][test].

2.

When I include parts of bar.rst in an md.

```{eval-rst}
.. include:: bar.rst
   :start-after: contents-begin-rst
   :end-before: contents-end-rst

.. include:: bar.rst
   :start-after: references-begin-rst
   :end-before: references-end-rst
```

So it renders as a link but points to: host/#test instead of to http://example.com/

3.

If I include foo.md in an .rst:

.. include:: foo.md
   :parser: myst_parser.sphinx_
   :start-after: <!-- contents-begin-md -->
   :end-before: <!-- contents-end-md -->

.. include:: foo.md
   :parser: myst_parser.sphinx_
   :start-after: <!-- references-begin-md -->
   :end-before: <!-- references-end-md -->

Hello world [Test][test]. is obtained. The link is not rendered.

4.

When I include parts of bar.rst in another .rst

.. include:: bar.rst
   :start-after: contents-begin-rst
   :end-before: contents-end-rst

.. include:: bar.rst
   :start-after: references-begin-rst
   :end-before: references-end-rst

It renders correctly.

Reproduce the bug

Source code: https://github.com/eyllanesc/myst_bug Output: https://myst-bug.readthedocs.io/en/latest/index.html

List your environment

  • sphinx==4.4.0
  • myst_parser==0.16.1, 0.17.0

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
thhtcommented, Aug 19, 2022

Ok, I did some digging.

The problem is basically the order in which things are done:

  1. Convert the markdown in a file to a list of tokens. References are already resolved here and references defined within the same file are resolved correctly.
  2. Evaluate all the directives. This includes the {include} directive.

This means that if the included file defines some references, they are not available, when the references in the original file are resolved.

There would be a few ways to potentially solve this:

  1. Have MystParser.parse intercept any include directives before calling render and just add that content to the inputstring. You would need to make sure to handle recursion correctly.
  2. Write a markdown_it plugin that provides the include file functionality. I think, this might be doable but at the moment, myst-parser comes with a set of pre-activated plugins and I could not find a way to tell it to activate more…
  3. Anything else?

For me, having a collection of references in an included file is really something that makes my life easier…

@chrisjsewell and maybe @choldgraf : is there any chance of this getting included? I would be happy and capable of contributing code and time. But I would need some feedback from you:

  1. Would you be willing to include this feature? If yes, under what conditions (except for the obvious like code quality, flake, unit tests)?
  2. What strategy / approach would you prefer?
1reaction
mawielandcommented, Nov 17, 2022

I might have found a “workaround” for the first problem . Using the MySt directive with syntax from rst works in my case:

(note the extra colon before “start-after” and missing dashes)

```{include} ./my_file.md
:start-after: dummy-label start
:end-before: dummy-label end
```

myst-parser 0.18.0 Sphinx 5.1.1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Include directive - Wikipedia
Many programming languages and other computer files have a directive, often called include (sometimes copy or import ), that causes the contents of...
Read more >
Includes | Asciidoctor Docs
An include directive imports content from a separate file or URL into the content of the current document. When the current document is...
Read more >
JSP - Include Directive - Tutorialspoint
The include directive is used to include a file during the translation phase. This directive tells the container to merge the content of...
Read more >
java - What's the difference between including files with JSP ...
Include directive uses file attribute to specify resources to be included while include action uses page attribute for the same purpose. Share.
Read more >
Documentation - Triple-Slash Directives - TypeScript
Triple-slash directives are single-line comments containing a single XML tag. The contents of the comment are used as compiler directives.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found