rich-text icon indicating copy to clipboard operation
rich-text copied to clipboard

Page Jump, Anchor Links, Jump Links

Open geoffgolder-ah opened this issue 4 years ago • 16 comments

I am trying to have a link to go to a certain section of the page, this basically means setting the name field of an <a> tag and linking to it. Obviously I can link to it, but I am not able to actually set the name of a link.

Is there a way to do this I am missing, or a way to override the actual rich text editor so that it allows us to set the "name" attribute on the link tag?

geoffgolder-ah avatar Dec 30 '19 19:12 geoffgolder-ah

@geoffgolder-ah Did you find a solution in the end? I am having the same issue right now.

AndreiEtumian-earnin avatar Apr 24 '20 19:04 AndreiEtumian-earnin

No. Effective duplicate of this: https://github.com/contentful/rich-text/issues/56

geoffgolder-ah avatar Apr 24 '20 19:04 geoffgolder-ah

#56 has been closed - am I right in thinking that the official answer is "fork our editor and write your own"?

parkersweb avatar Jun 19 '20 07:06 parkersweb

#56 has been closed - am I right in thinking that the official answer is "fork our editor and write your own"?

"and then we will adopt your solution", must admit not a bad move :)

yevmoroz avatar Jul 13 '20 18:07 yevmoroz

I'm struggling to explain to my Product owner why such basic and commonly used feature is not available in Rich Text editor. One solution that everyone seems to be suggesting is to create Content Type to be used for Headings (or any other text to be jump/anchor linked to) and then use RTE hyperlink functionality to link to entry when creating a link itself. This should be enough data to render Jump links on the FE. However creating yet another content type just for this seems like an overkill.

konstantib avatar Feb 11 '21 18:02 konstantib

Hi, curious if this is on a roadmap? 2+ years, I am assuming this will be closed?

waltercolindres avatar Jul 06 '21 20:07 waltercolindres

how we doin?

sebbean avatar Dec 23 '21 01:12 sebbean

Any updates on this one?

EliseiNicolae avatar May 03 '22 18:05 EliseiNicolae

any updates?

pavelrahman619 avatar Jun 30 '22 08:06 pavelrahman619

Same, any updates on this?

norahine avatar Jul 20 '22 11:07 norahine

Any updates for 2022/2023?

hapiben avatar Oct 19 '22 09:10 hapiben

We could really need this in November 2022 as well. Updates here would be great

Lucsy3012 avatar Nov 23 '22 16:11 Lucsy3012

Adding another nice to have here

ShaunMWallace avatar Jan 20 '23 15:01 ShaunMWallace

I guess still nothing yet?

Not the same but an alternative would be building a Table of Contents component out of the headings. Live example here

bdogaru avatar May 24 '23 15:05 bdogaru

There seems to be a manual approach to creating "Jump Links" as per this Contentful community thread: https://www.contentfulcommunity.com/t/jump-links-how-do-you-do-em/1598

Also, those 2 articles are presenting how to automatically generate a table of contents out of heading tags:

  • https://headlesstemplates.com/blog/table-of-contents-contentful-rich-text-field
  • https://vinceparulan.com/blog/how-to-create-table-of-contents-from-contentful-s-rich-text-field/

Hope that helps!

JulienDefrance avatar Jun 07 '23 19:06 JulienDefrance

Hi all, I highly recommend using chatgpt to help with this. You can do it with javascript to grab all the headings and dynamically create anchor links. A simple example to start:

// Function to build the table of contents
function buildTableOfContents() {
  // 1. Select all <h2> elements
  const h2Elements = document.querySelectorAll('h2');

  // 2. Create an <ol> element
  const olElement = document.createElement('ol');

  // 3. Iterate over the <h2> elements
  h2Elements.forEach(function(h2, index) {
    // Create an <li> element
    const liElement = document.createElement('li');
    
    // Optional: Create an <a> element for linking
    const aElement = document.createElement('a');
    const anchorId = `toc_${index}`;
    h2.id = anchorId; // Set an id for the <h2> if it doesn't already have one
    aElement.href = `#${anchorId}`;
    aElement.textContent = h2.textContent; // Set the text of the <a> to the <h2> text
    
    // Append the <a> to the <li>
    liElement.appendChild(aElement);
    
    // 4. Append each <li> to the <ol>
    olElement.appendChild(liElement);
  });

  // 5. Insert the <ol> into the page
  // For example, prepend to the body or another container element
  document.body.prepend(olElement);
}

// Call the function to build and insert the table of contents
buildTableOfContents();

But I think it's likely best to parse the contentful entry with nokogiri first to add all headings to the page in a table of contents container, then use javascript to re-arrange and add the anchor text to each heading. This is also helpful for SEO.

yoshyosh avatar Feb 23 '24 17:02 yoshyosh