Skip to content

Instantly share code, notes, and snippets.

@theconsultant
Forked from timpulver/pandoc_watch.js
Created November 9, 2022 03:35
Show Gist options
  • Save theconsultant/a4b351c508b0e4333caff4daf509a9af to your computer and use it in GitHub Desktop.
Save theconsultant/a4b351c508b0e4333caff4daf509a9af to your computer and use it in GitHub Desktop.
Watches a markdown-folder for changes and re-generates .icml (InCopy / Adobe InDesign CC) files on every change
var exec = require('child_process').exec;
var chokidar = require('chokidar');
/*
* Watches a folder for changes in markdown (*.md) files and compiles them to ICML (Adobe InDesign CC / InCopy format)
*
* Dependencies:
* - Install Pandoc: pandoc.org
* - Run "npm install chokidar"
*
* Infos on how to use the files in InDesign: http://networkcultures.org/digitalpublishing/2014/10/08/markdown-to-indesign-with-pandoc-via-icml/
*
* Run this script like this:
* - Navigate to the folder containing the script, e.g. "cd /Users/me/scripts"
* - Run the script: "node pandoc_watch.js"
*/
// folder to watch, relative to working directory, change it to point
// to your markdown files, can be absolute as well
var folder = '../../Buch/Text';
console.log('Watching folder ' + folder + ' for changes in markdown (*.md) files...');
chokidar.watch(folder)
.on('change', handleFileChange);
function handleFileChange(file){
var iExt = file.indexOf(".md");
if(iExt === -1) { return; }
console.log('File ' + file + ' has changed, compiling to ICML...');
var outputFile = file.substring(0, iExt) + ".icml";
var cmd = 'pandoc -s -f markdown -t icml -o ' + outputFile + " " + file;
exec(cmd, function(error, stdout, stderr) {
if(error) {
console.log('Error converting document: ' + error);
} else {
console.log("Done.");
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment