title: inDesign JSX scripted add of heading and content into textFrame
body: <p>I'm attempting to use inDesign JSX scripts to insert the following data into a document:</p>
  
  <pre><code>data = [{heading:"Heading 1", content: ["Some content"]},
   {heading:"Heading 2", content: ["Some other content with", "Multiple paragraphs"]}]
  </code></pre>
  
  <p>The data has to be placed into a single TextFrame, but have different styling on the heading and content. </p>
  
  <p>The only way I can see to add the text is in one go via the <code>textFrame.contents</code> variable:</p>
  
  <pre><code>allContent = "";
  headingParagraphs = []; // keep track of which paragraphs are headings
  paragraph = 0;
  for (var i = 0; i &lt; data.length; i++) {
    allContent += data.heading + "\r"; // Use a newline to split the paragraph
    headingParagraphs.push(paragraph);
    paragraph++;
    for (var j = 0; j &lt; data.content.length; j++) {
      allContent += data.content[j] + "\r"; // Use a newline to split the paragraph
      paragraph++;
    }
  }
  textFrame.contents = allContent; // all data is in, but all text is styled the same
  </code></pre>
  
  <p>Then once the data is in, I iterate the paragraphs and add some style to the headings:</p>
  
  <pre><code>for (var i = 0; i &lt; textFrame.paragraphs.count(); i++) {
    if (headingParagraphs.indexOf(i) != -1) { // this is a heading paragraph
      textFrame.paragraphs[i].pointSize = 20;
    }
  }
  </code></pre>
  
  <p>This works fine for small data sets that fit on one page, but once the contents gets bigger than the frame, <code>paragraphs</code> only returns visible paragraphs.  And if I follow on to a new textFrame, paragraphs get split and the <code>headingParagraphs[]</code> array no longer lines up.</p>
  
  <p>Ideally I'd like to <em>append</em> to the contents and set styles before I append the next content - but the API docs aren't very clear on how you might do that (if at all) </p>
  
  <pre><code>// Pseudo code:
  for all sections:
    append the heading to the frame, split to next page if needed
    style all the *new* paragraphs as headings
    for all section contents
      append the content to the frame, split to next page if needed
      style any *new* paragraphs as normal content
  </code></pre>
  
  <p>Is there a way to achieve this using either an append function or some other way to assign headings to the right place after content has been added?  Perhaps special characters in the content to define style?  </p>