Edit Markdown documents

This example demonstrates the standard open-edit-save pipeline with Markdown (MD) documents, using different options on every step.

Introduction

Markdown is a lightweight markup language, which became popular last time. Markdown files with an *.md extension are actually plain text files, contain special syntax, support text formatting, tables, lists, images and so on. There are actually several dialects of markdown, including GFM, CommonMark, Multi-Markdown and so on.

Starting from the version 23.2, GroupDocs.Editor for Java fully supports the Markdown format on both import, export, and also its auto-detection.

As for the version 23.2, GroupDocs.Editor for Java supports the following Markdown features, which mostly follow the CommonMark specification and are represented as appropriate styles or direct formatting:

  • Headings
  • Blockquotes
  • Code blocks
  • Horizontal rules
  • Bold emphasis
  • Italic emphasis
  • StrikeThrough formatting
  • Numbered and bulleted lists
  • Tables
  • Internal images, stored with base64 encoding
  • External images

Loading

Loading of the Markdown documents to the Editor class is usual and the same as for other formats. There are no dedicated load options for the Markdown format, it is enough to specify the file itself through file path or InputStream.

Code example below shows loading the same Markdown file to the two Editor instances through file path and a byte stream, and then checking how the GroupDocs.Editor will detect the format of specified file.

String filename = "sample.md";
String inputPath = System.IO.Path.Combine("markdown folder", filename);

Editor fromStream = new Editor(new FileInputStream(inputPath));
Editor fromPath = new Editor(inputPath);

com.groupdocs.editor.metadata.IDocumentInfo info = fromPath.getDocumentInfo(null);
Assert.assertEquals(com.groupdocs.editor.formats.TextualFormats.Md, info.getFormat());

Editing

There is a special class MarkdownEditOptions for editing the Markdown files. As always, it is not mandatory when editing a document, so the Editor.edit() overload without parameter may be used — GroupDocs.Editor will automatically detect the format and apply the default options.

However, specifying the custom MarkdownEditOptions may be vital when an input Markdown file has external images; “external” means that images are stored somewhere else, and in the Markdown code there are links to these images. When MarkdownEditOptions are not specified and correctly tuned (explained below), the GroupDocs.Editor will not be able to locate such images. In order to point all external images, the instance of MarkdownEditOptions should be created, and an getImageLoadCallback() property should be correctly specified.

For doing this the user must create its own type, that implements a IMarkdownImageLoadCallback interface. This interface defines a single method processImage(), which obtains a MarkdownImageLoadArgs type and must return a value of MarkdownImageLoadingAction enumeration.

Working mechanism is the next. GroupDocs.Editor parses an input Markdown file line by line, character by character. When it encounters the link to the external image, it creates an instance of MarkdownImageLoadArgs with all data about this image — its filename (or relative path, or URL) and a boolean flag, that indicates whether it is a local link (in the filesystem) or a web link. Then, when MarkdownEditOptions have the setImageLoadCallback property specified with the user implementation of IMarkdownImageLoadCallback, then GroupDocs.Editor invokes the processImage() method by passing a prepared MarkdownImageLoadArgs into it. Then GroupDocs.Editor “waits” until the user code makes a decision — a return value of the MarkdownImageLoadingAction enumeration.

If user implementation returns a MarkdownImageLoadingAction.Skip value, then the image will be skipped — the resultant document will have an “empty area”, where the image should be.

If user implementation returns a MarkdownImageLoadingAction.Default value, then the GroupDocs.Editor will try to load an image by itself — this is possible, if, for example, a link to image is an absolute path or URL.

If user implementation returns a MarkdownImageLoadingAction.UserProvided, then this means that user code must provide the image data by itself specifying it in the setData(byte[] data). In that case GroupDocs.Editor will process the specified binary data for this image.

Code example below shows exactly the last scenario, where the end-user creates his own implementation of the IMarkdownImageLoadCallback interface.

Let’s say we have the next file and folder structure:

  • root/input.md
  • root/images/image1.png
  • root/images/image2.jpeg

The input.md is a Markdown file we want to open and edit, that is located in the “root” folder, and it uses (references) two raster images image1.png and image2.jpeg, located in the “images” subfolder.

public void EditingMarkdown()
{
	String inputMdPath = Path.Combine("root", "input.md");
	String imagesFolder = Path.Combine("root", "images");

	// Creating the edit options
	MarkdownEditOptions editOptions = new MarkdownEditOptions();
	editOptions.setImageLoadCallback(new MdImageLoader(imagesFolder));

	Editor editor = new Editor(inputMdPath);

	EditableDocument beforeEdit = editor.edit(editOptions);

	// Make sure there are 2 images here
	Assert.assertEquals(2, beforeEdit.getImages().size());
	Assert.assertEquals("png", beforeEdit.getImages().get(0).getType().getFileExtension());
	Assert.assertEquals("jpeg", beforeEdit.getImages().get(1).getType().getFileExtension());

	String originalHtmlContent = beforeEdit.getEmbeddedHtml();

	// Send the 'originalHtmlContent' to the client-side WYSIWYG-editor,
	// obtain the edited version and create a new EditableDocument from it

	EditableDocument afterEdit = EditableDocument.fromMarkup(originalHtmlContent, null);

	// Make sure 2 images are still here
	Assert.assertEquals(2, afterEdit.getImages().size());
	Assert.assertEquals("png", afterEdit.getImages().get(0).getType().getFileExtension());
	Assert.assertEquals("jpeg", afterEdit.getImages().get(1).getType().getFileExtension());

	// Save to the DOCX, for example
	WordProcessingSaveOptions saveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Docx);
	String outputDocxPath = Path.Combine("root", "Output."+ saveOptions.getOutputFormat().getExtension());

	editor.save(afterEdit, outputDocxPath, saveOptions);

}

static class MdImageLoader implements IMarkdownImageLoadCallback
{
	private final String _imagesFolder;

	public MdImageLoader(String imagesFolder)
	{
		this._imagesFolder = imagesFolder;
	}

	public final byte processImage(MarkdownImageLoadArgs args)
	{
		String filePath = new java.io.File(this._imagesFolder, Paths.get(args.getImageFileName()).getFileName().toString()).getPath();

		byte[] data = Files.readAllBytes(filePath);
		args.setData(data);

		return MarkdownImageLoadingAction.UserProvided;
	}
}

In this example there is a user-created MdImageLoader class. It is initialized with the images folder path, and in the processImage method file content is read and pushed to the MarkdownImageLoadArgs class through the setData(byte[] data) method.

Saving

GroupDocs.Editor also supports saving into the Markdown format. Like for any other format, for saving into markdown the user must create an instance of MarkdownSaveOptions class and specify it in the com.groupDocs.editor.Editor.save() method.

If the document, destined for the saving in markdown format, has images, they should be “resolved” in a way similar to described above. But there is no callback for saving the images here. Instead of it, user has 3 choices:

  1. Ignore images - they will be absent.
  2. Save images inside the Markdown code, when they will be stored in base64 encoding.
  3. Save images as files separately in the specified folder, and in the markdown code there will be references to these image files.

For doing this the MarkdownSaveOptions class has several properties. getExportImagesAsBase64() is a boolean flag, by default set to false. If setted to true, the content of the images will be injected inside output Markdown as base64. Also, if this flag is set to true, it has the highest priority, and the getImagesFolder() property is ignored.

getImagesFolder() property, in turn, works when setExportImagesAsBase64(boolean) is set to false. This property, if specified, should contain a valid full path to the existing folder, where GroupDocs.Editor should save images.

The example below shows opening an input DOCX file for editing and saving it into 3 different Markdown output files, each of one has its own saving properties.

String filename = "SampleDoc.docx";
String inputPath = Path.Combine("some-path", filename);

String outputFolder = "Some-full-path-to-output-folder";

String outputMdEmbeddedFilePath = Path.Combine(outputFolder, "Output_Markdown_Embedded.md");
String outputMdExternalFilePath = Path.Combine(outputFolder, "Output_Markdown_External.md");
String outputMdAbsentFilePath = Path.Combine(outputFolder, "Output_Markdown_Absent.md");

Editor editor = new Editor(inputPath);
EditableDocument beforeEdit = editor.edit();
// Send content to client-side WYSIWYG-editor, edit it there, send back to the server-side
EditableDocument afterEdit = EditableDocument.fromMarkup(beforeEdit.getEmbeddedHtml(), null);

{// Saving to "embedded" version, where all images are injected inside MD with base64 encoding
	MarkdownSaveOptions mdSaveOptionsEmbedded = new MarkdownSaveOptions();
	mdSaveOptionsEmbedded.setExportImagesAsBase64(true);
	editor.save(afterEdit, outputMdEmbeddedFilePath, mdSaveOptionsEmbedded);
}

{// Saving to "external" version, where all images are stored in distinct files, while MD contains links to these files as paths in file system
	MarkdownSaveOptions mdSaveOptionsExternal = new MarkdownSaveOptions();
	mdSaveOptionsExternal.setImagesFolder(outputFolder);
	editor.save(afterEdit, outputMdExternalFilePath, mdSaveOptionsExternal);
}

{// Saving to "absent" version, where all images are skipped and are not present in MD
	MarkdownSaveOptions mdSaveOptionsAbsent = new MarkdownSaveOptions();
	ByteArrayOutputStream outputMdAbsentTemp = new ByteArrayOutputStream();
	editor.save(afterEdit, outputMdAbsentTemp, mdSaveOptionsAbsent);
	OutputStream outputMdAbsentStream = new FileOutputStream(outputMdAbsentFilePath);
	outputMdAbsentTemp.writeTo(outputMdAbsentStream);
	outputMdAbsentStream.close();
	outputMdAbsentTemp.close();
}
	
// Disposing all resources
beforeEdit.dispose();
afterEdit.dispose();
editor.dispose();

In this example you can see a little trick — for the “absent” version the document content is saved to the ByteArrayOutputStream, and then copied to the FileOutputStream. This is done, because GroupDocs.Editor has a trick: if both getExportImagesAsBase64() is set to false and setImagesFolder(String) is not set, then GroupDocs.Editor tries to analyze the specified stream. If this stream is a FileOutputStream, the GroupDocs.Editor obtains a path to the folder and saves images in it. So, in order to “deceive” the GroupDocs.Editor in this example, the ByteArrayOutputStream was used.

Roundtrip

Because Markdown format is supported on import and export, it is possible to perform a roundtrip scenario with it — open a markdown file for editing, edit it and then save the edited version to the Markdown format too. The example below demonstrates such a scenario.

public void MarkdownRoundtrip()
{
	String inputFolderPath = "Some-full-path-to-input-folder";
	String outputFolder = "Some-full-path-to-output-folder";
	String outputMdPath = Path.Combine(outputFolder, "Output.md");

	String filename = "ComplexImage.md";
	String inputPath = Path.Combine(inputFolderPath, filename);	

	MarkdownEditOptions editOptions = new MarkdownEditOptions();
	editOptions.setImageLoadCallback(new MdImageLoader(inputFolderPath);

	MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
	saveOptions.setTableContentAlignment(MarkdownTableContentAlignment.Center);
	saveOptions.setImagesFolder(outputFolder);

	Editor editor = new Editor(inputPath);
	{
		EditableDocument doc = editor.edit(editOptions);
		{
			Assert.assertEquals(3, doc.getImages().size());
			// edit "doc" in WYSIWYG-editor and obtain its edited version

			editor.save(doc, outputMdPath, saveOptions);
		}
	}
}

static class MdImageLoader implements IMarkdownImageLoadCallback
{
	private final String _imagesFolder;

	public MdImageLoader(String imagesFolder)
	{
		this._imagesFolder = imagesFolder;
	}

	public final byte processImage(MarkdownImageLoadArgs args)
	{
		String filePath = new java.io.File(this._imagesFolder, Paths.get(args.getImageFileName()).getFileName().toString()).getPath();
		byte[] data = Files.readAllBytes(filePath);
		args.setData(data);

		return MarkdownImageLoadingAction.UserProvided;
	}
}