sedyh
1
Hello, I’ve mounted static directory in order to get access to some resources in hugo.toml
like this:
[[module.mounts]]
source = 'static'
target = 'assets'
I can’t get readFile function to work in a snippet:
{{< code source="static/examples/bas_con_sin.txt" language="go" >}}
Which is:
start
{{ $language := .Get "language" }}
{{ $source := .Get "source" }}
{{ $options := .Get "options" }}
{{ with $source | readFile }}
{{ highlight (trim . "\n") $language $options }}
{{ end }}
end
How its look like:
Here is my sources:
Have I did something wrong with the config?
irkode
2
the mount has nothing to do with readFile
. readFile only sees your “real” filesystem not the virtual one.
static/examples/bas_con_sin.txt
will work
Code defensive and check the result of readFile
(same for $language)
{{ with $source | readFile }}
{{ highlight (trim . "\n") $language $options }}
{{ else }}
{{ errorf "could not readFile: %s" $source }}
{{ end }}
Now to your problem:
you wrapped the call to your shortcode within another one.
Use the {{< expand >}}
syntax instead of {{% %}}
{{< expand title="Full example" >}}
{{< code source="examples/bas_con_sin.txt" >}}
{{% /expand %}}
TL;TR
There’s a difference when calling shortcodes using {{%
or {{<
- The inner
{{< code ...
will return plain HTML (the highlighted content)
- The outer
{{% expand ...
will receive that as input and try to render it as Markdown
Since rendering HTML in Markdown is turned off by default in hugo see unsafe
The highlighting result is replaced with “” (check it your public folder)
read more about the difference here
sedyh
3
Thanks a lot for this.
I guess I should re-read the documentation next time I want to ask a question.
1 Like
bep
4
There’s a long story here, but I would strongly recommend that you use Hugo’s Resource abstraction instead of the readFile function, e.g.
- Mount some folder into /assets
- Use resources.Get etc.
1 Like
Hi @sedyh.
As you most likely are fiddling around with the Relearn theme, you could just try
```go
{{% include "examples/bas_con_sin.txt" %}}
```
or with expand
{{% expand title="Full example" %}}
```go
{{% include "examples/bas_con_sin.txt" %}}
```
{{% /expand %}}
This will save you from writing your own code
shortcode.
sedyh
6
Oh, cool, I didn’t expect that.
In fact, I need not just a code listing, but an insertion of several snippets by id from the same file.
I think I should make a separate topic for this…
1 Like
system
Closed
7
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.