Get specific data in content

I want to create a CollectionPage schema in home.

I’m trying to fetch data in my latest post:

{{ range first 1 (where .Site.Pages “Section” “post”) }}
{{ .Content }}
{{ end }}

The content looks like this:

| head 1 | head 2      | head 3 |
|--------|-------------|--------|
| body 1 | 1-2-3-4-5-6 | body 3 |

The data I need is the second td in the table.

1-2-3-4-5-6

Is there a way to fetch it? And be converted as:

[1, 2, 3, 4, 5, 6]

The CollectionPage schema markup:

{
  "@type": "ListItem",
  "position": 1,
  "item": {
    "@type": "Result",
    "name": "Sample post",
    "data": [1, 2, 3, 4, 5, 6],
    "datePublished": "2023-01-30"
}

Thank you.

If you can do this in markdown:

| head 1 | head 2                           | head 3 |
|--------|----------------------------------|--------|
| body 1 | <span id="data-id-1">1 2 3</span>| body 3 |
| body 2 | <span id="data-id-2">4 5 6</span>| body 2 |

Then do this in your site configuration (it’s not unsafe if you control the content):

[markup.goldmark.renderer]
unsafe = true

Then in your template you have two options.

Option 1 – Data only

{{ range findRE `(?s)<span id="data-id-\d+">.*?<\/span>` .Content  }}
  {{ split (replaceRE `.+"data-id-(\d+)">(.*)<\/span>` "$2" .) " " }}
{{ end }}

rendered

[1 2 3]
[4 5 6]

Option 2 – ID and data (requires Hugo v0.110.0)

{{ range findRESubmatch `(?s)<span id="(data-id-\d+)">(.*?)<\/span>` .Content }}
  {{ index . 1 }} ---> {{ split (index . 2) " " }}
{{ end }}

rendered

data-id-1 ---> [1 2 3]
data-id-2 ---> [4 5 6]
1 Like

Thank you.

I have 3 sections on my website namely blog, articles, and notes.

I’m trying to achieve this in schema markup:

{
  "@type": "ListItem",
  "position": 1,
  "item": {
    "@type": "Result",
    "name": "Sample blog post",
    "data": [1, 2, 3],
    "datePublished": "2023-01-30"
}
{
  "@type": "ListItem",
  "position": 1,
  "item": {
    "@type": "Result",
    "name": "Sample article post",
    "data": [4, 5, 6],
    "datePublished": "2023-01-30"
}
{
  "@type": "ListItem",
  "position": 1,
  "item": {
    "@type": "Result",
    "name": "Sample note post",
    "data": [7, 8, 9],
    "datePublished": "2023-01-30"
}

I’ve tried:

"data": {{ range first 1 (where .Site.Pages “Section” “post”) }}
  {{ range findRE `(?s)<span id="data-id-\d+">.*?<\/span>` .Content  }}
    {{ split (replaceRE `.+"data-id-(\d+)">(.*)<\/span>` "$2" .) " " }}
  {{ end }}
{{ end }}
"data": {{ range first 1 (where .Site.Pages “Section” “articles”) }}
  {{ range findRE `(?s)<span id="data-id-\d+">.*?<\/span>` .Content  }}
    {{ split (replaceRE `.+"data-id-(\d+)">(.*)<\/span>` "$2" .) " " }}
  {{ end }}
{{ end }}
"data": {{ range first 1 (where .Site.Pages “Section” “notes”) }}
  {{ range findRE `(?s)<span id="data-id-\d+">.*?<\/span>` .Content  }}
    {{ split (replaceRE `.+"data-id-(\d+)">(.*)<\/span>` "$2" .) " " }}
  {{ end }}
{{ end }}

But failed as I don’t know how to code. Thank you for your time and reply

That’s going to be a problem.

You might consider posting a bounty in the services section of this forum.

Thank you for your time and reply.