<commit_before>import Helmet from 'react-helmet';

export default function buildPage($doc, html, appState) {
  const { htmlAttributes, ...headChildren } = Helmet.rewind();

  const $html = $doc.find('html');
  for (const attributeName of Object.keys(htmlAttributes)) {
    $html.attr(attributeName, htmlAttributes[attributeName]);
  }

  const $head = $doc.find('head');
  const { title, base, ...appendableTags } = headChildren;
  replace($head, 'title', title.toString());
  replace($head, 'base', base.toString());

  // TODO replace meta tags ?

  for (const tagName of Object.keys(appendableTags)) {
    const tag = appendableTags[tagName];
    $head.append(tag.toString());
  }

  // Insert App HTML
  $doc.find('#app').append(`<div>${html}</div>`);

  // Insert Redux State
  $doc.find('script').last().before(`<script>window.__PRELOADED_STATE__ = ${JSON.stringify(preloadedState)}</script>`);
}

function replace($head, tagName, newTag) {
  const tag = $head.find(tagName);
  if (tag.length > 0) {
    tag.replaceWith(newTag);
  } else {
    $head.append(newTag);<commit_msg>Fix inserting the redux stats<commit_after>import Helmet from 'react-helmet';

export default function buildPage($doc, html, appState) {
  const { htmlAttributes, ...headChildren } = Helmet.rewind();

  const $html = $doc.find('html');
  for (const attributeName of Object.keys(htmlAttributes)) {
    $html.attr(attributeName, htmlAttributes[attributeName]);
  }

  const $head = $doc.find('head');
  const { title, base, ...appendableTags } = headChildren;
  replace($head, 'title', title.toString());
  replace($head, 'base', base.toString());

  // TODO replace meta tags ?

  for (const tagName of Object.keys(appendableTags)) {
    const tag = appendableTags[tagName];
    $head.append(tag.toString());
  }

  // Insert App HTML
  $doc.find('#app').append(`<div>${html}</div>`);

  // Insert Redux State
  $doc.find('script').last().before(`<script>window.__PRELOADED_STATE__ = ${JSON.stringify(appState)}</script>`);
}

function replace($head, tagName, newTag) {
  const tag = $head.find(tagName);
  if (tag.length > 0) {
    tag.replaceWith(newTag);
  } else {
    $head.append(newTag);
{'fraction_non_alphanumeric': 0.17647058823529413, 'fraction_numerical': 0.0009191176470588235, 'mean_word_length': 4.428927680798005, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '4824253', 'n_tokens_mistral': 708, 'n_tokens_neox': 691, 'n_words': 149}