简体   繁体   中英

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

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.

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.

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

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:

[url](#header-id)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM