distilagency / starward

:boom: ReactJS Wordpress Boilerplate
27 stars 9 forks source link

Decode HTML Entities - Page Titles #62

Closed samlogan closed 6 years ago

samlogan commented 7 years ago

Helper Function which needs adding to the title inside components/Common/Head.jsx:

export const decodeHTMLEntities = (str) => {
  if (!str) return '';
  let formattedString = str;
  const entities = [
      ['amp', '&'],
      ['apos', '\''],
      ['#x27', '\''],
      ['#x2F', '/'],
      ['#39', '\''],
      ['#47', '/'],
      ['#8211', '-'],
      ['lt', '<'],
      ['gt', '>'],
      ['nbsp', ' '],
      ['quot', '"']
  ];
  for (let i = 0, max = entities.length; i < max; ++i) formattedString = formattedString.replace(new RegExp('&' + entities[i][0] + ';', 'g'), entities[i][1]);
  return formattedString;
};
AllanPooley commented 7 years ago

Perhaps less ideal, but I addressed this issue in Kaya (https://github.com/AllanPooley/kaya_app/commit/bae8edc20e62701bd37407e69ba3fd172f201b21) with an existing library: https://www.npmjs.com/package/html-entities