I’m stuck trying to get readfile and highlight to add JSON files to a page in an indented list item.
I’ve got a shortcode for adding text to a page from a data file:
{{/* readfile.html */}}
{{- $file := .Get "file" -}}
{{- if fileExists $file -}}
{{- highlight ($file | readFile) (.Get "highlight") "" -}}
{{- else -}}
{{- errorf "readfile did not find %q in %s" $file .Position -}}
{{- end -}}
Then add the JSON directly or with the shortcode:
1. original
```json
{
"name": "a simple job to perform one action",
"description": "a bunch of words"
}
```
1. readfile
{{< readfile file="test.json" highlight="json" >}}
If I add the JSON directly to the Markdown, it renders correctly. But if I add the same JSON with this shortcode, it comes out indented.
<ol>
<li>
<p>original</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span> <span style="color:#062873;font-weight:bold">"name"</span>: <span style="color:#4070a0">"a simple job to perform one action"</span>,
</span></span><span style="display:flex;"><span> <span style="color:#062873;font-weight:bold">"description"</span>: <span style="color:#4070a0">"a bunch of words"</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div></li>
<li>
<p>readfile</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span> <span style="color:#062873;font-weight:bold">"name"</span>: <span style="color:#4070a0">"a simple job to perform one action"</span>,
</span></span><span style="display:flex;"><span> <span style="color:#062873;font-weight:bold">"description"</span>: <span style="color:#4070a0">"a bunch of words"</span>
</span></span><span style="display:flex;"><span>}</span></span></code></pre></div>
</li>
</ol>
And it looks something like this on the rendered page:
1. original
{
"name": "a simple job to perform one action",
"description": "a bunch of words"
}
2. readfile
{
"name": "a simple job to perform one action",
"description": "a bunch of words"
}
It’s adding 4 extra spaces before the </span>
tag due to the indentation of the ordered list item.
I tried assigning the HTML of the highlighted JSON to a variable, slicing into an array, and then using replaceRE
or string.TrimLeft
to remove the spaces, but it looks like the extra spaces are added when the HTML is added to the page and not by the highlight function. I also made a very hacky attempt of wrapping the JSON text in markdown backticks and using markdownify
.