17 Replies Latest reply on Apr 30, 2015 6:22 AM by nicolaik

    Markdown script...?

    Adam Barnes

      I'm looking for a way to convert the content in a Indesign file to markdown, is there a way to map this in a script?

       

      The Indesign file is simple, with just 4 levels of heading, paragraphs and some bullets, italic and bold, but I am looking for a way to automate the process.

       

      Any help would be greatly appreciated.

        • 1. Re: Markdown script...?
          nicolaik Level 1

          You can go through the file using the script and write out everything in Markdown. You would have to map them all in your script

           

          If you tag all elements, you can also use  XML export with XSLT.

          • 2. Re: Markdown script...?
            Adam Barnes Level 1

            Hi Nicolaik, when you say the script, is there one that will help me make a start that you know of?

             

            Thanks for you help!!

            • 4. Re: Markdown script...?
              Adam Barnes Level 1

              Hi,

               

              Tried it, but it missed out random paragraphs..? just after a table or image, 3 to 6 paragraphs just are missing, so it could be a bit dangerous to use, without knowing what and why its doing that!!!

              • 5. Re: Markdown script...?
                nicolaik Level 1

                Sorry, Adam Barnes, did not come across such a script. I will try to put together a sample to demonstrate the idea.

                 

                Nioolai

                • 6. Re: Markdown script...?
                  Adam Barnes Level 1

                  Many thanks Nicolaik!! thats what I love about the Adobe community, the best in people and willingness to help!!

                   

                  Thank you!

                  • 7. Re: Markdown script...?
                    nicolaik Level 1

                    Adam,

                     

                    As promised, I put the script together. It grabs a text frame, loops through the paragraphs and writes them to a text file on the desktop. The resulting file should be in markdown.

                    The next step would be adding the character styles and do the mapping with your existing styles. My InDesign file has only one text frame and seven paragraphs' styles. the styles are : Header1, Header2, Header3, italic, bold, blockquote and highlight

                     

                    
                     #target indesign
                        
                    var myFile= new File( '~/Desktop/markdown_test.txt');
                        
                    myFile.open("w");
                            
                            myTextFarme = app.activeDocument.textFrames[0];
                            
                            myParagraphs = myTextFarme.paragraphs;
                    
                            totalparagraphs = myParagraphs.count();
                            
                            var i , tempContent ='', tempStyle='';
                            
                            for ( i= 0 ; i< totalparagraphs; i++)
                             {
                                    try{
                                          tempContent = myParagraphs.item(i).contents;
                                          
                                          if (myParagraphs.item(i).contents != "\r") 
                                          {
                                               tempStyle = myParagraphs.item(i).appliedParagraphStyle.name;
                                              }
                                          else if (myParagraphs.item(i).contents != "\n") 
                                          {
                                              }
                                          else
                                          {
                                              tempStyle ='';
                                              }
                                          
                                          //$.write(tempContent);
                                          //$.write(tempStyle);
                                          
                                          switch(tempStyle) {
                                                    case "Header1":
                                                         tempContent= "#" + tempContent;
                                                        break;
                                                    case "Header2":
                                                        tempContent= "##" + tempContent;
                                                        break;
                                                     case "Header3":
                                                        tempContent= "###" + tempContent;
                                                        break;  
                                                   case "bold":
                                                        tempContent= "**" + tempContent + "**";
                                                        break;  
                                                  case "italic":
                                                        tempContent= "*" + tempContent + "*";
                                                        break;     
                                                 case "highlight":
                                                        tempContent= "```" + tempContent + "```";
                                                        break;  
                                                  case "blockquote":
                                                        tempContent= ">" + tempContent;
                                                        break;        
                                                    default:
                                                        tempContent=tempContent;
                                                }
                                          
                                          myFile.write( tempContent);
                    
                    
                                        }
                                    catch(err) {
                                        $.write( err.message);
                                        }
                                    
                                   tempContent = '';
                                   tempStyle = '';
                                
                                }
                    
                    
                      myFile.close();
                    

                     

                     

                    Nicolai

                    JS, ID5.5, MAC

                    • 8. Re: Markdown script...?
                      Vamitul Level 4

                      This might be what you need melchiorb/indesign-scripts · GitHub
                      (haven't tested it)

                      • 9. Re: Markdown script...?
                        Adam Barnes Level 1

                        Thank you Nicolaik, and @vamitul I'm out of the office today, but will look tonight, thanks for all you help!!!

                        • 10. Re: Markdown script...?
                          [Jongware] Most Valuable Participant

                          No, that works the other way 'round.

                           

                          ... Nice dialog, but mine does a better job.

                          • 11. Re: Markdown script...?
                            Adam Barnes Level 1

                            Hi, looking good but its not getting the normal paragraph style, para, do I need a bit of code for them, as the done have any marks, I have tried adding this:

                             

                            case "pars": 
                            tempContent= " " + tempContent; 
                            break; 
                            

                             

                            Getting this:

                            ## Second

                            ###Third

                            ###Third

                             

                            Should be:

                            ## Second

                            ###Third

                            para

                            para

                            ###Third

                            para

                            para

                             

                            Can you help? I might be thick!!

                            • 12. Re: Markdown script...?
                              nicolaik Level 1

                              Hi Adam,

                               

                              I assume you are talking about my sample code (sorry if I am wrong). The "default" in switch should pickup anything not mapped by switch, but your suggestion should also work. it works for me on my sample file. You do not need to add space at the beginning of the tempContent, use the same statement as default.

                               

                              Nicolai

                              • 13. Re: Markdown script...?
                                Adam Barnes Level 1

                                Hi

                                 

                                All working now, it was the para had a number at the start, I don't need that anyway, but the character styles and not being tagged with day *xxxx* or **xxxx** with in the paragraph, am I missing something?

                                 

                                Many thanks,

                                 

                                adam

                                • 14. Re: Markdown script...?
                                  nicolaik Level 1

                                  Adam,

                                   

                                  I did mention it is more of an idea and not a complete code. [Jongware] script has 2000 lines and takes everything into account, mine has 75 (half of them blank).

                                   

                                  I would probably run a grep replacement before starting writing file out, search for character styles and replace the text with text plus markdown tags. Obviously you would have an issue with some of the tags, e.g. * for italic, but that's the next stage. Do you want me to put together another sample to demonstrate?

                                   

                                  BTW, any advice on submitting sample files on this forum?

                                   

                                  Nicolai

                                  • 15. Re: Markdown script...?
                                    Adam Barnes Level 1

                                    Goodness I am sorry for the questions, I know it was just an pointer, and feel bad for asking, but also it will only do one page, I am a little lost as how to get it to go through all pages?

                                     

                                    So sorry to ask, and really don't want to take advantage! or "take the......" or anything else!

                                     

                                    As for posting demo files, I am unsure, maybe an external link? and any demo you can send my way will surly help!!

                                     

                                     

                                    And thank you!!

                                    • 16. Re: Markdown script...?
                                      nicolaik Level 1

                                      No problem, I will see what I can do and pm file to you.

                                       

                                      You will need to loop through the pages and text frames.

                                      • 17. Re: Markdown script...?
                                        nicolaik Level 1

                                        OK, the short version of the idea:

                                         

                                        Run GREP to insert the markdown tags for the character styles:

                                         

                                            app.findGrepPreferences = NothingEnum.NOTHING;
                                            app.changeGrepPreferences = NothingEnum.NOTHING;
                                            // Set the grep options
                                                 var myCharStyle = app.activeDocument.characterStyles.item("chBold"); 
                                                 app.findGrepPreferences.appliedCharacterStyle=myCharStyle;
                                                 app.findGrepPreferences.findWhat = "(.+)";
                                                 app.changeGrepPreferences.changeTo="**$1**";
                                            //Replace bold
                                                 app.activeDocument.changeGrep();
                                        
                                           // Set the grep options for italic
                                            myCharStyle = app.activeDocument.characterStyles.item("chItalic"); 
                                            app.findGrepPreferences.appliedCharacterStyle=myCharStyle; 
                                            app.findGrepPreferences.findWhat = "(.+)";
                                            app.changeGrepPreferences.changeTo="*$1*";
                                            //Replace italic
                                                 app.activeDocument.changeGrep();
                                        
                                            // Clear the find/change preferences after the search
                                            app.findGrepPreferences = NothingEnum.NOTHING;
                                            app.changeGrepPreferences = NothingEnum.NOTHING;
                                        

                                         

                                        Add code to loop through all text frames and export one paragraph at a time as before. Finally, add two undos at the end to reverse grep changes.

                                         

                                                app.activeDocument.undo();
                                                app.activeDocument.undo();