0

END GOAL

Ultimately, I need to export my InDesign file to HTML such that each HTML block-level element has a unique ID of some kind (it doesn't matter what it is as long as it's unique), a unique ID that also stays in the InDesign file and does not change, regardless of user modifications to the InDesign content, and gets exported in the HTML with each element. Something like this:

<p data-id="4j8w53">My first paragraph</p>
<p data-id="k39dh2">My second paragraph</p>

And then when the InDesign user changes the content and re-exports to HTML, the existing IDs remain, like this:

<p data-id="4j8w53">My first paragraph is awesome!</p>
<p data-id="lq09cf">My inserted paragraph</p>
<p data-id="k39dh2">My second paragraph</p>

If someone knows an easy way to do this, I'd love to hear it.

PLAN NUMBER ONE

Trying to figure this out, what I've decided to do is map everything in the InDesign file to XML, then add my ID attributes to the XML items through a JS user script, then export the XML as HTML through another JS user script. Since the XML items are actually mapped to InDesign items, the IDs will stay with them as the text changes.

Here's the problem: I can't seem to get all of my InDesign content mapped to XML using the mapStylesToXMLTags() function. I have a bunch of stories in the file and one of them never shows up in the XML when I export it. It is simply missing.

If I could just get all the content to show up in the exported XML when I use mapStylesToXMLTags(), that would probably solve my problem.

Or, if there is a different mapping mechanism that would actually map all my items to XML and keep the mappings, that might work as well.

PLAN NUMBER TWO

If I recursively loop through allPageItems in app.documents[0] and manually create the XML instead of using mapStylesToXMLTags(), then the missing object shows up just like everything else.

However, then the IDs that I give the XML elements don't attach to the InDesign elements, since those elements are not actually officially "mapped" to XML elements. They don't automatically update when changed. I would have to overwrite all the XML when changes are made to the InDesign file, eliminating the consistency of the data-id values.

Also, when I do it this way, everything is all out of order and disconnected from the way it was laid out in the InDesign file.

Plan number one still seems to have the most promise, if I could just get all the content to map.

PLEASE GO EASY ON ME

I hope this is all clear. FWIW, I am a JS / HTML guy, not an InDesign guy. I am new to InDesign and am just trying to figure out how to make this process happen for the InDesign users at my organization.

gcdev
  • 1,406
  • 3
  • 17
  • 30

2 Answers2

0

If texts are discarded, it could be that either mapping is incomplete or that text is not taggable such as foot notes, possibly text variables or captions. To get them into the loop, you would need to convert them as regular text prior to export. But just in case, here is a simple script. You can give a try and see if it catches the problematic texts.

//MAIN ROUTINE
var main = function() {
 
 var doc = app.properties.activeDocument,
 fgp = app.findGrepPreferences.properties,
 fcgo = app.findChangeGrepOptions.properties,
 found, n = 0, text, pTag, xe;
 
 //Exit if no documents open
 if ( !doc ) {
  alert("You need an open document" );
  return;
 }
 
 //Setting F/R Grep
 app.findGrepPreferences = app.findChangeGrepOptions = null;
 
 app.findGrepPreferences.properties = {
  findWhat : "^.+",
 }
 app.findChangeGrepOptions.properties = {
  includeFootnotes:false,
  includeHiddenLayers:true,
  includeLockedLayersForFind:true,
  includeLockedStoriesForFind:true,
  includeMasterPages:true,
 }
 
 //Adding "p" tag if needed
 pTag = doc.xmlTags.itemByName("p");
 !pTag.isValid && pTag = doc.xmlTags.add({name:"p"});
 
 //Getting paragraphs occurences
 found = doc.findGrep();
 n = found.length;
 while ( n-- ) {
  text = found[n];
  xe = text.associatedXMLElements;
  //Adding "p" tags with ids if needed
  if ( !xe.length || xe[0].markupTag.name!="p") { 
   doc.xmlElements[0].xmlElements.add( pTag, text ).xmlAttributes.add('id', guid () );
  }
 }
 
 //Reverting initial F/R settings
 app.findGrepPreferences.properties = fgp;
 app.findChangeGrepOptions.properties = fcgo;
}

//Returns unique ID
function guid() {
  function s4() {
    return Math.floor((1 + Math.random()) * 0x10000)
      .toString(16)
      .substring(1);
  }
  return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}

var u;

//Run
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
Loic
  • 2,173
  • 10
  • 13
  • Every time I try to export the XML generated from this it crashes InDesign. – gcdev Apr 04 '18 at 15:28
  • Might be a bug. I can export just fine. Which version are you using ? Are you up to date ? Maybe the contents is too large. Try setting a dummy attribute instead of UUID and see if InDesign exports without crashing. This is to see if UUID is the guilty one. – Loic Apr 05 '18 at 16:25
0

Apparently the object that was not mapping to XML was a linked InCopy file, which was locked against any editing. In order to map it to XML, I had to click inside it, then select Edit > InCopy > Check Out. After that, it mapped with no difficulty at all using my "Plan Number One" above.

gcdev
  • 1,406
  • 3
  • 17
  • 30