Using readFile with a relative path?

Short version: How do I use readFile with a relative path?

Long version:
I am making something that allows me to include snippets of markdown in other pages. Kindof like “include” (see Replacement for mmark include file functionality)

According to experiments and the documentation, readFile takes a path from the root of the project. Which is normally cool. But how can I refer to a file relative to the current page.

For example, if I have…
a page “” in a section “tests” that also has an md file (sorry not to good at drawing tree pictures)

inside of I want to be able to write {{% include “” %}} - not the full path as {{% include “tests/” %}}

so, this (simplified) include:

{{- .Get 0 | readFile -}}

works file for the latter case (full path) - to make it work for the local case, I need to manually build the path (as far as I can tell), based on readFile assumes path relative to /content and not /, I did:

{{- $file := .Get 0 -}}
{{- $dir := .Page.File.Dir -}}
{{- $fullname := (printf "%s/%s" $dir $file)}}
{{- readFile $fullname -}}

But this only works for local files. Is there a way to make it work for both (e.g. “” is local, but “/tests/” is from the root)?

BTW: I know that there are solutions using .GetPage which does nice searching, but this is for files that don’t have frontmatter.

For stuff relative to the current page, shouldn’t you be using the page resources approach? (Like, with “get this resource”, print its .Content, .RawContent or whatever.)

For reference:

{{- $file := .Get 0 -}}
{{- with $.Page.Resources.GetMatch $file -}}
  {{- .Content -}}
{{- end -}}

Nevermind, it seems I got it wrong.

I am aware of this and use it extensively for other things. But, as far as I can tell, this only works with actual Hugo pages (e.g., markdown files with frontmatter). The “.Content” only works when the page is structured (as far as I can tell)

Maybe a different question is… how do I tell Hugo’s resources things to grab me files that are raw markdown. Because if I had a way to do that, I could use all of the cool resource mechanisms (e.g., the assets directory).

Right. I can’t remember if that’s a possibility right now, but maybe as a workaround you could simply check if there’s a .Title…?

Hugo ignores MD files that don’t begin with “—”, so it never gets that far.

Assuming you were going to use assets directory and resources.GetMatch. In this case, front matter would be totally ignored (as the front matter, it would be necessary to use something like replaceRE to remove it) and markdownify would be necessary to render the content.

I still can’t think a good way to detect raw markdown files, sorry. (Oh, maybe I can do some tests with findRE or output formats, but that still would be limited.)