Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
Trying to get text with properties from html data

I'm trying to get text from HTML with it's properties (bold, underlined, italic, superscript etc.) but I am struggling with nested ones (like <b> Lorem <u> Ipsum </u></b>, in this case Lorem should be bold and Ipsum should be bold and underlined).

Example Data

<p> Normal<b>Bold</b> <b>Bold<u>Underlined</u></b> <b><i>Bold Italic</i></b><p/>

I need to use this texts in Indesign Script and I need to assign character styles for these properties. Is there any tool or technique for PHP or Javascript that I can use?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try if DOMParser is available in the environment where you're going to run your JS code.

This parses the html string and outputs a tree structure of the nodes and their texts.

const htmlString = '<p> Normal<b>Bold</b> <b>Bold<u>Underlined</u></b> <b><i>Bold Italic</i></b><p/>';

const htmlElement = (new DOMParser().parseFromString(htmlString, 'text/html')).firstChild.childNodes[1].firstChild;

const tree = convertDomToArray(htmlElement);

console.log(tree);


function convertDomToArray(element) {
  if (element.nodeName === '#text') {
    return element.nodeValue;
  }
  
  let children = [];
  for (let childElement of element.childNodes) {
    children.push(convertDomToArray(childElement));
  }
  
  let output = {};
  output[element.nodeName] = children;
  
  return output;
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!