0

How can i automatically generate anchor ids to h2 and h3 added using ck editor ? I need it for making a table of content.

  • 1
    Not sure if anyone has built an integration module for this, but I would leverage ckeditor.com/cke4/addon/toc – Kevin Jul 8 at 13:12
  • interesting but would probably create a toc for each content type fields. A module would be needed to generate a toc for the full page , including paragraphs etc . I have maded it in JS – Matoeil Jul 8 at 13:15
0

in JS, id set from the h2 label two first words :

const headings = document.querySelectorAll(
  ".article--full h2, .article--full h3"
);
const tocElements = createTocElementsArray(headings);

function createTocElementsArray(headings) {
  const tocElements = [];   
  for (const item of headings) {
    if (item.id === "") {
      item.id = item.firstChild.data
        .split(" ")
        .slice(0, 2)
        .join("-");
    }
....
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.