.GetPage.Content filtered with findRe?

I want to reuse portions of some pages in another without copying/pasting. I want to maintain a single source. And I’d rather not create a unique shortcode for every single reusable snippet.

So I am trying to create a shortcode that would accept a file path and delimeter strings. For example, let’s say this is my source:

Welcome to HUGO —  **thanks for starting a new conversation!**

reuseStart

* Does the title sound interesting if you read it out loud? Is it a good summary?
* Who would be interested in this? Why does it matter? What kind of responses do you want?
* Include commonly used words in your topic so others can  *find*  it. To group your topic with related topics, select a category.

reuseEnd

And another sentence. 

I want to reuse those three bullet points but nothing else.

I use this shortcode (which would eventually be rewritten to take parameters):

{{ $mypage := .Site.GetPage "/example/mypage.md" }}
{{ $content := $mypage.Content }}
{{ $snippet := findRE "(?s)<p>reuseStart</p>.*?<p>reuseEnd</p>" $content }}

{{ $snippet | safeHTML }}

(Yeah I know it’s pretty janky but I’m just focusing on the broad strokes right now)

Anyway, this results in the following error:

execute of template failed: template: shortcodes/test.html:5:14: executing "shortcodes/test.html" at <safeHTML>: error calling safeHTML: unable to cast []string{"<p>reuseStart</p>\n<ul>\n<li>Does the title sound interesting if you read it out loud? Is it a good summary?</li>\n<li>Who would be interested in this? Why does it matter? What kind of responses do you want?</li>\n<li>Include commonly used words in your topic so others can\u00a0 <em>find</em> \u00a0it. To group your topic with related topics, select a category.</li>\n</ul>\n<p>reuseEnd</p>"} of type []string to string

So it is grabbing the content fine and returning the HTML I want, but won’t actually render it.

If I remove the safeHTML bit I get the HTML snippet rendered as a plain string:

[<p>reuseStart</p> <ul> <li>Does the title sound interesting if you read it out loud? Is it a good summary?</li> <li>Who would be interested in this? Why does it matter? What kind of responses do you want?</li> <li>Include commonly used words in your topic so others can <em>find</em> it. To group your topic with related topics, select a category.</li> </ul> <p>reuseEnd</p>]

So what am I doing wrong here?

because findRE returning slice/array.

make it like this: (using index function to get the item from array)

{{ index $snippet 0 | safeHTML }}

That works, thank you!!

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