Adding id to all <p> elements

I tried the solution provided at Is it possible to add a (different) identifier for each paragraph? - #6 by jmooring, but it doesn’t handle paragraphs that contain parenthesis, for example :-) will break it, and the error comes up:

executing "index.html" at <replaceRE . $newParagraph $c 1>: error calling replaceRE: error parsing regexp: unexpected ): `<p>Thanks to <a href="https://memo.claudrod.me/#1684111511">Claud</a> I am now binging Hall and Oates. I might not be having coffee, but they sure help a lot whilst working! :-)</p>`

What would the fix be?

Please provide a complete example of the markdown for your page.

Apologies. This is the entire markdown file:

---
title: "1684154770"
date: 2023-05-15T08:46:11-04:00
draft: false
---

Thanks to [Claud](https://memo.claudrod.me/#1684111511) I am now binging Hall and Oates. I might not be having coffee, but they sure
help a lot whilst working! :-)

Minor change, line 5:

{{ $c := .Content }}
{{ range $k, $_ := findRE `<p>.*?</p>` $c }}
  {{ $paragraphId := printf "p-%03d" (add 1 $k) }}
  {{ $newParagraph := replaceRE `<p>` (printf "<p id=%q>" $paragraphId) . }}
  {{ $c = strings.Replace $c . $newParagraph 1 }}
{{ end }}
{{ $c | safeHTML}}

I’ve edited the original post.

Excellent, thank you much!

1 Like

Out of curiosity why do you need each paragraph to have an id?

I want to be able to link to paragraphs, within a page.

Do you want the ability to link to all paragraphs, or just specific paragraphs?

I am looking to have the ability to link to all paragraphs (not that I will be linking to all, but for me to be able to do that at any given moment, the paragraphs have to have an id, right?).

Understood. Using the replacements/insertions on .Content as described above is the right approach.

I just wanted to make sure you knew about markup attributes in case you only wanted to add an id to specific paragraphs, e.g.,

Able was I ere I saw Elba.
{id=foo}

or

Able was I ere I saw Elba.
{#foo}
1 Like

Thank you, yes, I know that feature. Thank you so much for maintaining such a helpful website, I have learned a lot from it!

1 Like

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