Linking to exactly one heading and its body

What I’m trying to do

I would like to embed exactly one heading and all the text it contains until the next heading.

Let’s say I have this file

example.md

# heading 1
a sample text
## heading 2
also a sample text
### heading 3
same here

# second heading 1

And what I would like to embed would look like this

# heading 1
a sample text

Things I have tried

When using ![[example#first heading 1]]
it shows the whole file

# heading 1
a sample text
## heading 2
also a sample text
### heading 3
same here

# second heading 1

When using a ![[#^]] i can only choose either the heading 1 or “a sample text”

I can embed both using
![[example#^1171f3]]
![[example#^15fcba]]

but it will look like this

# heading 1
a sample text

Which is a lot of visual clutter. Is there a simple and elegant way?

Everything “under” a heading is part of it, including lower level headings, until you reach an equal or higher-level heading. So for your level-1 heading, you would have to sneak another level-1 heading in there to stop the embed from showing the complete contents.

An option using comments:

# 1
embed this

# %% %%
## 1.1
don't embed this

# 2
don't embed this

---

![[#1]]

The commented header is invisible in reading view. But it is visible—and rather large—in the editing views:


Another option using a #hide-me tag:

# 1
embed this

# #hide-me
## 1.1
don't embed this

# 2
don't embed this

---

![[#1]]

… along with this CSS snippet:

/* #hide-me heading tag
	intended for separating heading sections and for keeping hr, white space, and other elements from collapsing into previous headings */
	
	/* Reading */
	[class*="el-h"] > [data-heading*="#hide-me"] {
		display: none;
	}

	/* Live Preview */
	.is-live-preview .HyperMD-header:has(> .cm-tag-hide-me) {
		font-size: 1px;
	}

	/* Source mode */
	.markdown-source-view:not(.is-live-preview) .HyperMD-header:has(> .cm-tag-hide-me) {
		font-size: 0.5em;
	}

In Reading, the sneaky heading is hidden.
In Live Preview, you can shrink it to any size you want, including 0 (invisible) by changing the font-size value.
In Source mode, it’s easy to find but inconspicuous.

I didn’t completely hide the sneaky header because I figure you might want to remove or rearrange it sometimes:

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.