<commit_before>import { initCommentsApp } from 'wagtail-comment-frontend';
import { STRINGS } from '../../config/wagtailConfig';

function initComments() {
  // in case any widgets try to initialise themselves before the comment app,
  // store their initialisations as callbacks to be executed when the comment app
  // itself is finished initialising.
  const callbacks = [];
  window.commentApp = {
    registerWidget: (widget) => {
      callbacks.push(() => { window.commentApp.registerWidget(widget); });
    }
  };
  document.addEventListener('DOMContentLoaded', () => {
    const commentsElement = document.getElementById('comments');
    const commentsOutputElement = document.getElementById('comments-output');
    const data = JSON.parse(document.getElementById('comments-data').textContent);

    if (commentsElement && commentsOutputElement && data) {
      window.commentApp = initCommentsApp(commentsElement, commentsOutputElement, data.user, data.comments, new Map(Object.entries(data.authors)), STRINGS);
      callbacks.forEach((callback) => { callback(); });
    }
  });
}

function getContentPath(fieldNode) {
  // Return the total contentpath for an element as a string, in the form field.streamfield_uid.block...<commit_msg>Throw nicer error if comment element is missing and initialisation fails<commit_after>import { initCommentsApp } from 'wagtail-comment-frontend';
import { STRINGS } from '../../config/wagtailConfig';

function initComments() {
  // in case any widgets try to initialise themselves before the comment app,
  // store their initialisations as callbacks to be executed when the comment app
  // itself is finished initialising.
  const callbacks = [];
  window.commentApp = {
    registerWidget: (widget) => {
      callbacks.push(() => { window.commentApp.registerWidget(widget); });
    }
  };
  document.addEventListener('DOMContentLoaded', () => {
    const commentsElement = document.getElementById('comments');
    const commentsOutputElement = document.getElementById('comments-output');
    const dataElement = document.getElementById('comments-data');
    if (!commentsElement || !commentsOutputElement || !dataElement) {
      throw new Error('Comments app failed to initialise. Missing HTML element');
    }
    const data = JSON.parse(dataElement.textContent);
    window.commentApp = initCommentsApp(
      commentsElement, commentsOutputElement, data.user, data.comments, new Map(Object.entries(data.authors)), STRINGS
    );
    callbacks.forEach((callback) => { callback(); });
  });
}

function getContentPath(fieldNode) {
  // Return the total contentpath for an element as a string, in the form field.streamfield_uid.block...
{'fraction_non_alphanumeric': 0.11946408634164496, 'fraction_numerical': 0.0, 'mean_word_length': 4.365269461077844, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5618900', 'n_tokens_mistral': 691, 'n_tokens_neox': 678, 'n_words': 193}