Extract headers and paragraphs from markdown

Here is content of .md file located in content/en/posts/homepage.md

### Header 1
paragraph 1

### Header 2
paragraph 2

### Header 3
paragraph 3

Here is shortcode template located in layouts/shortcodes/post.html

{{ $elements := where .Site.RegularPages "Type" "in" (slice "posts" "homepage") }}
{{ range $elements }}
{{ $headers := split .Content "<h3>" }}
{{ $paragraphs := split .Content "<p>" }}
{{ range $h := $headers }}
{{ range $p := $paragraphs }}
    <div class="wrapper">
        <h3>{{ $h | safeHTML }}</h3>
        <p>{{ $p | safeHTML }}</p>
    </div>
{{ end }}
{{ end }}
{{ end }}

<div class="wrapper">
    <h3>Header 1</h3>
    <p>paragraph 1</p>
</div>

<div class="wrapper">
    <h3>Header 2</h3>
    <p>paragraph 2</p>
</div>

<div class="wrapper">
    <h3>Header 3</h3>
    <p>paragraph 3</p>
</div>

How can I extract it correctly? Or could you please provide another solution

What problem are you trying to solve?

I have list of headers and paragraphs in one markdown file and I need to put each in corresponding tag

Hmm, it might be easier to put them in a data file and range through that in your template, so you can put each in a div like you show.

Yes, I know about data templates. I’m having all data in .md files and to be consistent need to keep it with this extension. In theory I can create block-1.md, block-2.md, block-3.md in post directory and then after range through it, but it’s kinda dirty.

Why don’t you just put that in your homepage.md:

<div class="wrapper">
    <h3>Header 1</h3>
    <p>paragraph 1</p>
</div>

<div class="wrapper">
    <h3>Header 2</h3>
    <p>paragraph 2</p>
</div>

<div class="wrapper">
    <h3>Header 3</h3>
    <p>paragraph 3</p>
</div>

Markdown is a superset of HTML, so you put HTML right in your content.

Now, this example is a little simple and strange, so that may not solve your issue, but I would point out that one should not wrap a bunch headers in a single file with divs, so I think it works as an answer. :slight_smile: