# Adding id to all \<p\> elements

**URL:** <https://discourse.gohugo.io/t/adding-id-to-all-p-elements/44639>\
**Category:** support\
**Created:** [May 31, 2023, 2:05pm UTC](https://discourse.gohugo.io/t/adding-id-to-all-p-elements/44639 "2023-05-31T14:05:09Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fastidious](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fastidious/32/4746_2.png) [@Fastidious](https://discourse.gohugo.io/u/Fastidious)\
**Post date:** [May 31, 2023, 2:05pm UTC](https://discourse.gohugo.io/t/adding-id-to-all-p-elements/44639/1 "2023-05-31T14:05:09Z")

</div>

I tried the solution provided at [Is it possible to add a (different) identifier for each paragraph? - #6 by jmooring](https://discourse.gohugo.io/t/is-it-possible-to-add-a-different-identifier-for-each-paragraph/43047/6), but it doesn’t handle paragraphs that contain parenthesis, for example `:-)` will break it, and the error comes up:

```auto
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?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [May 31, 2023, 2:10pm UTC](https://discourse.gohugo.io/t/adding-id-to-all-p-elements/44639/2 "2023-05-31T14:10:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fastidious](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fastidious/32/4746_2.png) [@Fastidious](https://discourse.gohugo.io/u/Fastidious)\
**Post date:** [May 31, 2023, 2:13pm UTC](https://discourse.gohugo.io/t/adding-id-to-all-p-elements/44639/3 "2023-05-31T14:13:04Z")

</div>

Apologies. This is the entire markdown file:

```auto
---
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! :-)

```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [May 31, 2023, 2:47pm UTC](https://discourse.gohugo.io/t/adding-id-to-all-p-elements/44639/4 "2023-05-31T14:47:46Z")

</div>

Minor change, line 5:

```auto
{{ $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.

---

<div class="post-metadata">

**Author:** ![Fastidious](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fastidious/32/4746_2.png) [@Fastidious](https://discourse.gohugo.io/u/Fastidious)\
**Post date:** [May 31, 2023, 2:52pm UTC](https://discourse.gohugo.io/t/adding-id-to-all-p-elements/44639/5 "2023-05-31T14:52:57Z")

</div>

Excellent, thank you much!

---

<div class="post-metadata">

**Author:** ![chrillek](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/chrillek/32/19797_2.png) [@chrillek](https://discourse.gohugo.io/u/chrillek)\
**Post date:** [May 31, 2023, 7:30pm UTC](https://discourse.gohugo.io/t/adding-id-to-all-p-elements/44639/6 "2023-05-31T19:30:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fastidious](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fastidious/32/4746_2.png) [@Fastidious](https://discourse.gohugo.io/u/Fastidious)\
**Post date:** [June 1, 2023, 4:56pm UTC](https://discourse.gohugo.io/t/adding-id-to-all-p-elements/44639/7 "2023-06-01T16:56:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [June 1, 2023, 5:00pm UTC](https://discourse.gohugo.io/t/adding-id-to-all-p-elements/44639/8 "2023-06-01T17:00:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fastidious](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fastidious/32/4746_2.png) [@Fastidious](https://discourse.gohugo.io/u/Fastidious)\
**Post date:** [June 1, 2023, 5:59pm UTC](https://discourse.gohugo.io/t/adding-id-to-all-p-elements/44639/9 "2023-06-01T17:59:46Z")

</div>

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?).

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [June 1, 2023, 6:08pm UTC](https://discourse.gohugo.io/t/adding-id-to-all-p-elements/44639/10 "2023-06-01T18:08:15Z")

</div>

> [@Fastidious](#):
>
> I am looking to have the ability to link to all paragraphs

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

> [@jmooring](#):
>
> or just specific paragraphs

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.,

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

or

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

```

---

<div class="post-metadata">

**Author:** ![Fastidious](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fastidious/32/4746_2.png) [@Fastidious](https://discourse.gohugo.io/u/Fastidious)\
**Post date:** [June 1, 2023, 6:10pm UTC](https://discourse.gohugo.io/t/adding-id-to-all-p-elements/44639/11 "2023-06-01T18:10:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [June 3, 2023, 6:10pm UTC](https://discourse.gohugo.io/t/adding-id-to-all-p-elements/44639/12 "2023-06-03T18:10:26Z")

</div>

Closed on June 3, 2023, 6:10pm UTC

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