Referring to sub-section in a markdown file in another folder

1.2k Views Asked by At

Related to Markdown: Reference to section from another file

I have two markdown files:

├── parent.md
└── content/
    └── child.md

In parent.md:

# Main section
## sub-section

I'd like to make a reference to ##sub-section from child.md. How do I do that? Note that child.md is in a sub-folder.

Note that I am using Jupyter book to process the markdown.

3

There are 3 best solutions below

2
On BEST ANSWER

Preface

Generally, markdown processors will apply an ID to document headers so that one can create a hyperlink.

Simply doing the following should work for most markdown processors:

[parent sub-section](parent.md#sub-section)

The downside to this approach is that when the header text changes, the ID changes, and therefore breaks the anchor link. Depending on the markdown processor you choose, there may be an idiosyncratic way to hardcode the anchor explicitly into the heading.

Jupyter-book

Since the processor you are using is Jupyter book, you can use section labels to cross-reference sections throughout your project.

Example:

Parent

Markdown input:

(parent:sub-section)=
# sub-section

Jupyterbook build output:

<section id="sub-section">
<span id="parent-sub-section"></span><h2>Sub-section<a class="headerlink" href="#sub-section" title="Permalink to this headline">#</a></h2>
</section>

Child

Markdown input:

[parent sub-section](parent:sub-section)
{ref}`parent:sub-section`

Jupyterbook build output:

<p><a class="reference internal" href="../parent.html#parent-sub-section"><span class="std std-ref">parent sub-section</span></a></p>
<p><a class="reference internal" href="../parent.html#parent-sub-section"><span class="std std-ref">Sub-section</span></a></p>

Note: Depending on the size of your project, it may be worthwhile planning out how you would like to namespace your labels in advance.

References

1
On

In child.md you can do this:

[link text](../parent.md#sub-section)

Note the use of the prefacing relative path ../ to reference the parent one level higher. Also note that depending on your generator, spaces in your #sub-section name may need to be replaced with dashes when referencing.

You can also use an absolute path. If / is your highest level, this should also work:

[link text](/parent.md#sub-section)  

In Jupyter Book, you can use custom labels:

Place a label right before the section you want to reference:

In parent.md:

# Main section

(sub-section-label)=
## sub-section

Then in child.md, refer to it like so:

[Link text](sub-section-label)

The text between the parentheses just has to match.

0
On

providing another thought, since markdown support some html syntax, you can

  1. add an empty link to the target position in child.md file
<a id="sub"></a>
## sub-section
  1. add a href link in parent.md file

I tried on obsidian, if I click the link, the child file will show, but it does not jump to the sub section

<a href="obsidian://open?vault=content&file=content%2Fchild.md#sub">link</a> 

probably change the href to ".content/child.md#sub" in another editor