Will Rowe
Will Rowe

Reputation: 71

Using blob to make .docx file from md input?

I am trying to create a custom Word document output using Hogan and Blob. It works as expected for .md files but I am trying to generalize to .docx or other file types.

From following an online tutorial this is what I have so far:

app.markdown = app.template.render(data);
updateLink(
    app.markdown,
    "text/plain",
    "example.md",
    document.getElementById("mdLink")
  );

app.markdown is valid markdown as expected. And this is the updateLink function:

function updateLink(content, contentType, filename, link) {
  const blob = new Blob([content], { type: contentType });
  const url = window.URL.createObjectURL(blob);
  window.URL.revokeObjectURL(link.href);
  link.href = url;
  link.download = filename;
}

So, the above works for .md files, but when I try changing "example.md" to "example.docx" and "text/plain" to "application/msword", and clicking the link results in a .docx file being downloaded but Word is not able to open the content. So what format do I need to convert my markdown to so that it works natively with Word? (Ideally I would also be able to preserve some of the markdown styling into the Word styling). Thanks!

Upvotes: 1

Views: 993

Answers (0)

Related Questions