Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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!!
Copy link to clipboard
Copied
Sorry, Adam Barnes, did not come across such a script. I will try to put together a sample to demonstrate the idea.
Nioolai
Copy link to clipboard
Copied
Many thanks Nicolaik!! thats what I love about the Adobe community, the best in people and willingness to help!!
Thank you!
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Thank you Nicolaik, and @vamitul I'm out of the office today, but will look tonight, thanks for all you help!!!
Copy link to clipboard
Copied
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!! ![]()
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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!!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Hello,
This might help.
https://www.rorohiko.com/wordpress/2014/12/23/indesign-markdown-export-crawler-id2md/
Copy link to clipboard
Copied
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!!!
Copy link to clipboard
Copied
This might be what you need melchiorb/indesign-scripts · GitHub
(haven't tested it)
Copy link to clipboard
Copied
No, that works the other way 'round.
... Nice dialog, but mine does a better job. ![]()
Find more inspiration, events, and resources on the new Adobe Community
Explore Now